21、ES实战:实现组合查询

组合查询
使用boolQuery 实现组合查询

public class EsBoolQuery throws UnknownHostException{
   
     
    public void BoolQuery(TransportClient client){
   
     
       //bool实现组合查询,must表示必须满足,mustNot表示必须不满足,should表示可以满足,filter表示过滤
       Querybuilder build = QueryBuilders.boolQuery()
						       .must(QueryBuilders.matchQuery("interests", "changge"))
						       .mustNot(QueryBuilders.,matchQuery("interests", "lvyou"))
						       .should(QueryBuilders.matchQuery("address", "beijing"))
						       .filter(QueryBuilders.rangeQuery("birthday").gte("1990-01-01").format("yyyy-MM-dd"));

       SearchResponse response = client.prepareSearch("lib3")
                                       .setQuery(build)
                                       .get();
       SearchHits hits = response.getHits();
       for(SearchHit hit:hits){
   
     
           System.out.println(hit.getSourceAsString());
       }
    }
}

constant score query
另一个查询和查询,包裹查询只返回一个常数分数等于提高每个文档的查询
实现组合查询

public class EsConstantScoreQuery throws UnknownHostException{
   
     
    public void ConstantScoreQuery(TransportClient client){
   
     
       // 过滤查询 Using with Filters
       Querybuilder build = QueryBuilders.constantScoreQuery(FilterBuilders.termFilter("name", "kimchy")).boost(2.0f);*/

       // 普通查询 With Queries
       Querybuilder build = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name", "葫芦3033娃")).boost(2.0f);

       SearchResponse response = client.prepareSearch("lib3")
                                       .setQuery(build)
                                       .get();
       SearchHits hits = response.getHits();
       for(SearchHit hit:hits){
   
     
           System.out.println(hit.getSourceAsString());
       }
    }
}

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: