gpt4 book ai didi

elasticsearch - 带有多个must子句的Elasticsearch Bool查询形成

转载 作者:行者123 更新时间:2023-12-03 00:04:56 25 4
gpt4 key购买 nike

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

 {
"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个must子句,对于“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。有人可以指出我在想什么吗?我正在使用 flex 搜索版本-5.5.0和jest-2.4.0作为我的Java客户端。

最佳答案

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

关于elasticsearch - 带有多个must子句的Elasticsearch Bool查询形成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45736764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com