[英]Elasticsearch Java High Level Rest Client constructing a boolean query with multiple match values and OR condition
I am trying to construct a query via the java high level rest client that implements taking a list of ids and returning all those documents that match a given id akin to a WHERE clause with an OR operator.我正在尝试通过 java 高级 rest 客户端构建查询,该客户端实现获取 id 列表并返回与给定 id 匹配的所有文档,类似于带有 OR 运算符的 WHERE 子句。
For this reason I have been going with bool query, and trying to iterate the list and must match for each value with operator set to OR出于这个原因,我一直在使用 bool 查询,并尝试迭代列表,并且必须匹配每个值,并将运算符设置为 OR
BoolQueryBuilder builder = QueryBuilders.boolQuery();
ids.forEach(i -> {
bool.must(QueryBuilders.matchQuery("_id", i).operator(Operator.OR));
});
return bool;
// this constructs the dsl this way // 这样构造 dsl
{
"bool" : {
"must" : [
{
"match" : {
"_id" : {
"query" : "0025370c-baea-4dcc-af48-56c4bdb86854",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
},
{
"match" : {
"_id" : {
"query" : "013fedef-6b04-4520-8458-fca8b0366833",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
},
{
"match" : {
"_id" : {
"query" : "01c44ce4-0e87-4dc9-8a29-1f24679d335f",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
which is constructed fine only it doesn't work because I think the OR is nested to low, and doesn't get applied across the multiple matches.它构造得很好,只是它不起作用,因为我认为 OR 嵌套在低位,并且不会应用于多个匹配项。 So I assume that there needs to be a nested type and I tried this:所以我假设需要有一个嵌套类型,我尝试了这个:
BoolQueryBuilder bool = QueryBuilders.boolQuery();
BoolQueryBuilder subBool = QueryBuilders.boolQuery();
ids.forEach(i -> {
subBool.must(QueryBuilders.matchQuery("_id", i).operator(Operator.OR));
});
bool.must(subBool);
return bool;
// it would make more sense to me to place the operator condition on bool instead of subBool but it is not available and I am sure I am going at that wrong // 将运算符条件放在 bool 而不是 subBool 上对我来说更有意义,但它不可用,我确信我做错了
{
"bool" : {
"must" : [
{
"bool" : {
"must" : [
{
"match" : {
"_id" : {
"query" : "0025370c-baea-4dcc-af48-56c4bdb86854",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
},
{
"match" : {
"_id" : {
"query" : "013fedef-6b04-4520-8458-fca8b0366833",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
},
{
"match" : {
"_id" : {
"query" : "01c44ce4-0e87-4dc9-8a29-1f24679d335f",
"operator" : "OR",
"prefix_length" : 0,
"max_expansions" : 50,
"fuzzy_transpositions" : true,
"lenient" : false,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
This seems to work if I reduce it to single value in the nested match (again 1 id instead of the lot)...so I still think I am implementing the OR condition wrong.如果我将它减少到嵌套匹配中的单个值(再次是 1 个 id 而不是很多),这似乎可行......所以我仍然认为我执行 OR 条件是错误的。
Filters within the bool query instead of must matches yield the same result.布尔查询中的过滤器而不是必须匹配产生相同的结果。 Appreciate the help.感谢帮助。
The OR
-Operator in the match
-Queries means that only one term of each query-string of that particular sub-query has to match the document in order for the sub-query to match, so that's not what you're aiming for. match
-Queries 中的OR
-Operator 意味着该特定子查询的每个查询字符串中只有一个术语必须与文档匹配才能使子查询匹配,因此这不是您的目标。 To compound the sub-queries with OR
, you have to use should
instead of must
in your root bool-query.要使用OR
复合子查询,您必须在根布尔查询中使用should
而不是must
。 must
is the ElasticSearch-equivalent of the AND
-operator, while should
means OR
. must
是AND
运算符的 ElasticSearch 等效项,而should
表示OR
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.