elasticsearch的多条件查询及其在elasticsearch-rest-high-level-client的使用

在kibana中的使用


GET /leyou/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "all": "华为"
          }
        },
        {
          "match": {
            "specs.品牌": "哦哦哦哦哦"
          }
        }
      ]
    }
  }
}

在elasticsearch-rest-high-level-client中的使用

 // 1 查询商品
            // 1.1 设置查询条件
            MatchQueryBuilder mqb = QueryBuilders.matchQuery("all", search.getKey()).operator(Operator.AND);
            // 1.1.1  设置过滤字段
            Map filter = search.getFilter();
            BoolQueryBuilder bqb = null;
            if(!CollectionUtils.isEmpty(filter)){
                bqb = QueryBuilders.boolQuery();
                bqb.must(mqb);
                for(String i : filter.keySet()){
                    MatchQueryBuilder termQuery = null;
                    if(!i.equals("brandId") && !i.equals("cid3")) {
                        termQuery = QueryBuilders.matchQuery("specs." + i +".keyword" , filter.get(i)).operator(Operator.AND);
                    }
                    else
                        termQuery =  QueryBuilders.matchQuery(i , filter.get(i)).operator(Operator.AND);
                    bqb.must(termQuery);
                }
            }
            SearchSourceBuilder ssb = new SearchSourceBuilder();
            String [] includField = new String[]{"subTitle","skus","id"};
            ssb.fetchSource(includField,null);
            ssb.from((search.getPageIndex()-1)*Search.PAGE_SIZE);
            ssb.size(Search.PAGE_SIZE);
            SearchResponse response = esUtil.matchQuery(ssb, bqb != null ? bqb:mqb, "leyou");

注意点:如某些字段为es自动创建,那么如果不写.keyword将按照分词来查找,导致结果不准确,所以确定不需要分词时需要加上

你可能感兴趣的:(elasticsearch的多条件查询及其在elasticsearch-rest-high-level-client的使用)