我有下面這樣的查詢 -Elasticsearch布爾查詢形成具有多個必備子句
{
"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());
與上面的代碼,我預計我會爲「1項必備條款camp_id'和另外1個f或「ad_id」,但下面是我得到 -
{
"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
}
}
}
只有這兩種包裝和camp_id一個ad_id必須條款。有人能指出我錯過了什麼嗎?我使用的彈性搜索版本 - 5.5.0和笑話 - 2.4.0我的Java客戶端。
我使用腦作爲UI訪問elasticsearch集羣。當我試圖從腦提供的其他客戶端執行查詢,我發現,它給了我一個錯誤,說明 - 複製必須條款。難道我們不能有多個必須條款嗎?如果不是,應該是處理象SELECT * FROM運動的一種途徑WHERE ID IN(1,2,3)和名稱(「A」,「B」,「C」)。有人可以幫我嗎? – user1305398