ElasticSearch : 在 ElasticSearch 中等效运算符 [英] ElasticSearch : IN equivalent operator in ElasticSearch
问题描述
我正在尝试在 SQL 中找到等效于 IN
NOT
的 ElasticSearch 查询.
I am trying to find ElasticSearch query equivalent to IN
NOT
in SQL.
我知道我们可以使用带有多个 OR 的 QueryString 查询来获得相同的答案,但最终会出现很多 OR.
I know we can use QueryString query with multiple OR to get the same answer, but that ends up with lot of OR's.
谁能分享一下这个例子?
Can anyone share the example?
推荐答案
与 Chris 建议的评论类似,IN
的类似替代是 terms
filter(查询意味着评分,这可能会改善返回的订单).
Similar to what Chris suggested as a comment, the analogous replacement for IN
is the terms
filter (queries imply scoring, which may improve the returned order).
SELECT * FROM table WHERE id IN (1, 2, 3);
等效的 Elasticsearch 1.x 过滤器是:
The equivalent Elasticsearch 1.x filter would be:
{
"query" : {
"filtered" : {
"filter" : {
"terms" : {
"id" : [1, 2, 3]
}
}
}
}
}
等效的 Elasticsearch 2.x+ 过滤器是:
The equivalent Elasticsearch 2.x+ filter would be:
{
"query" : {
"bool" : {
"filter" : {
"terms" : {
"id" : [1, 2, 3]
}
}
}
}
}
重要的一点是 terms
过滤器(和查询)适用于完全匹配.它隐含地是一个 or
操作,类似于 IN
.
The important takeaway is that the terms
filter (and query for that matter) work on exact matches. It is implicitly an or
operation, similar to IN
.
如果你想反转它,你可以使用 not 过滤器,但我建议使用稍微冗长的 bool
/must_not
过滤器(为了养成习惯也使用 bool
/must
和 bool
).
If you wanted to invert it, you could use the not filter, but I would suggest using the slightly more verbose bool
/must_not
filter (to get in the habit of also using bool
/must
and bool
).
{
"query" : {
"bool" : {
"must_not" : {
"terms" : {
"id" : [1, 2, 3]
}
}
}
}
}
总的来说,bool
复合查询语法是 Elasticsearch 中最重要的过滤器之一,还有 term
(单数)和 terms
过滤器(复数,如显示).
Overall, the bool
compound query syntax is one of the most important filters in Elasticsearch, as are the term
(singular) and terms
filters (plural, as shown).
这篇关于ElasticSearch : 在 ElasticSearch 中等效运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!