具有多个必须条款

时间:2017-08-17 13:40:55

标签: elasticsearch jest

我有一个类似以下的查询 -

 {
    "query": {
        "bool": {
            "must": {
                "bool" : { "should": [
                      { "match": { "camp_id": "Elasticsearch" }},
                      { "match": { "camp_id": "Solr" }} ] }
            },
            "must": { 
                "bool" : { "should": [
                      { "match": { "ad_id": "Elastic" }},
                      { "match": { "ad_id": "dummy" }} ] }
            },
            "must_not": { "match": {"authors": "radu gheorge" }},
            .....
            .....     
        }
    }
    }

简而言之,(camp_id ='elasticsearch'或camp_id ='solr')AND(ad_id ='elasticsearch'或ad_id ='solr')....

经过大量研究,我编写了以下java代码 -

    final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    final BoolQueryBuilder finalBoolQuery = new BoolQueryBuilder();

    BoolQueryBuilder campaignBoolQuery = null;
    if (campaignIds != null) {
        campaignBoolQuery = QueryBuilders.boolQuery();
        for (int campaignId : campaignIds) {
            campaignBoolQuery.should(QueryBuilders.matchQuery("camp_id", campaignId));
        }
    }

    BoolQueryBuilder creativeBoolQuery = null;
    if (creativeIds != null) {
        creativeBoolQuery = QueryBuilders.boolQuery();
        for (int creativeId : creativeIds) {
            creativeBoolQuery.should(QueryBuilders.matchQuery("ad_id", creativeId));
        }
    }

    finalBoolQuery.must(campaignBoolQuery);
    finalBoolQuery.must(creativeBoolQuery);
    searchSourceBuilder.query(finalBoolQuery).size(9999);

    System.out.println(searchSourceBuilder.toString());

使用上面的代码,我预计我会为'camp_id'设置1个必须条款,为'ad_id'设置另外1个,但以下是我得到的 -

    {
  "size" : 9999,
  "query" : {
    "bool" : {
      "must" : [
        {
          "bool" : {
            "should" : [
              {
                "match" : {
                  "camp_id" : {
                    "query" : 1,
                    "operator" : "OR",
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "fuzzy_transpositions" : true,
                    "lenient" : false,
                    "zero_terms_query" : "NONE",
                    "boost" : 1.0
                  }
                }
              },
              {
                "match" : {
                  "camp_id" : {
                    "query" : 2,
                    "operator" : "OR",
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "fuzzy_transpositions" : true,
                    "lenient" : false,
                    "zero_terms_query" : "NONE",
                    "boost" : 1.0
                  }
                }
              }
            ],
            "disable_coord" : false,
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        },
        {
          "bool" : {
            "should" : [
              {
                "match" : {
                  "ad_id" : {
                    "query" : 1,
                    "operator" : "OR",
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "fuzzy_transpositions" : true,
                    "lenient" : false,
                    "zero_terms_query" : "NONE",
                    "boost" : 1.0
                  }
                }
              }
            ],
            "disable_coord" : false,
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "disable_coord" : false,
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  }
}

只有一个must子句包含camp_id和ad_id。有人可以指出我错过了什么吗?我使用弹性搜索版本 - 5.5.0和jest - 2.4.0作为我的java客户端。

1 个答案:

答案 0 :(得分:0)

您的外部bool示例查询包含两个must子句,但这必须是包含对象数组的单个must子句。我想你在调用must()两次时会用第二个覆盖第一个必须条款。