Java Elasticsearch 查询中的 OR 和 AND 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25552321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OR and AND Operators in Elasticsearch query
提问by Udit Bhatia
I have few json document with the following format :-
我有几个具有以下格式的 json 文档:-
_source: {
userId: "A1A1",
customerId: "C1",
component: "comp_1",
timestamp: 1408986553,
}
I want to query the document based on the following :-
我想根据以下内容查询文档:-
(( userId == currentUserId) OR ( customerId== currentCustomerId) OR (currentRole ==ADMIN) ) AND component= currentComponent)
I tried using the SearchSourceBuilder and QueryBuilders.matchQuery, but I wasnt able to put multiple sub queries with AND and OR operators.
我尝试使用 SearchSourceBuilder 和 QueryBuilders.matchQuery,但我无法使用 AND 和 OR 运算符放置多个子查询。
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("userId",userId)).sort("timestamp", SortOrder.DESC).size(count);
How we query elasticsearch using OR and AND operators?
我们如何使用 OR 和 AND 运算符查询 elasticsearch?
采纳答案by xlecoustillier
I think in this case the Bool queryis the best shot.
我认为在这种情况下Bool 查询是最好的选择。
Something like :
就像是 :
{
"bool" : {
"must" : { "term" : { "component" : "comp_1" } },
"should" : [
{ "term" : { "userId" : "A1A1" } },
{ "term" : { "customerId" : "C1" } },
{ "term" : { "currentRole" : "ADMIN" } }
],
"minimum_should_match" : 1
}
}
Which gives in Java:
在 Java 中给出:
QueryBuilder qb = QueryBuilders
.boolQuery()
.must(termQuery("component", currentComponent))
.should(termQuery("userId", currentUserId))
.should(termQuery("customerId", currentCustomerId))
.should(termQuery("currentRole", ADMIN))
.minimumNumberShouldMatch(1)
The must
parts are AND
s, the should
parts are more or less OR
s, except that you can specify a minimum number of should
s to match (using minimum_should_match
), this minimum being 1 by default I think (but you could set it to 0, meaning that a document matching no should
condition would be returned as well).
该must
部分是AND
S,该should
部分都或多或少OR
S,除了你可以指定的最小数量should
(用s到比赛minimum_should_match
),这个最低默认为1,我认为(但你可以将其设置为0,这意味着文件不匹配任何should
条件也将返回)。
If you want to do more complex queries involving nested AND
s and OR
s, simply nest other bool queries inside must
or should
parts.
如果您想执行涉及嵌套AND
s 和OR
s 的更复杂查询,只需将其他 bool 查询嵌套在must
或should
部分中即可。
Also, as you're looking for exact values (ids and so on), maybe you can use term queries instead of match queries, which spare you the analysis phase (if those fields are analyzed at all, which doesn't necessarily make sense for ids). If they are analyzed, you still can do that, but only if you know exactly how your terms are stored (standard analyzer stores them lower cased for instance).
此外,当您正在寻找确切的值(id 等)时,也许您可以使用term 查询而不是 match 查询,这样您就可以省去分析阶段(如果对这些字段进行了分析,则不一定有意义)对于 ID)。如果它们被分析,你仍然可以这样做,但前提是你确切地知道你的术语是如何存储的(例如,标准分析器将它们存储为小写)。
回答by Alix Martin
If you use a query_string
query, your ANDs and ORs will be interpreted as such by the Lucene library.
如果您使用query_string
query,Lucene 库将解释您的 AND 和 OR 。
This allows you to search for
这允许您搜索
(currentUserId OR currentCustomerId) AND currentComponent
for instance. By default, the values will be searched for in all fields.
例如。默认情况下,将在所有字段中搜索值。