ElasticSearch:El等效运算符在ElasticSearch中 [英] ElasticSearch : IN equivalent operator in ElasticSearch

查看:334
本文介绍了ElasticSearch:El等效运算符在ElasticSearch中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SQL中找到相当于 IN \ NOT 的ElasticSearch查询。



我知道我们可以使用具有多个OR的QueryString查询获得相同的答案,但是最终会出现很多OR。



有人可以分享这个例子吗?

解决方案

类似于Chris建议作为评论,类似于 IN 条款 过滤器 (查询意味着评分,这可能会改善退货顺序)。

  SELECT * FROM table WHERE id IN(1,2,3); 

等效的Elasticsearch 1.x过滤器将是:



$ $ $ $ $ $$ {
查询:{
过滤:{
过滤器:{

id:[1,2,3]
}
}
}
}
}

等效的Elasticsearch 2.x +过滤器将是:

 查询:{
bool:{
filter:{
terms:{
id:[1 ,2,3]
}
}
}
}
}

重要的外包是,术语过滤器(和查询此事)可以用于完全匹配。它隐含地是操作,类似于 IN



如果你想反转它,你可以使用非过滤器,但我建议使用稍微更详细的 bool / must_not 过滤器(要习惯于使用 bool / 必须 bool )。

  {
query:{
bool :{
must_not:{
terms:{
id:[1,2,3]
}
}
}
}
}

总体而言, bool 复合查询语法是 Elasticsearch 中最重要的过滤器之一,以及术语(单数)和术语过滤器(复数,如图所示)。


I am trying to find ElasticSearch query equivalent to IN \ NOT in SQL.

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?

解决方案

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);

The equivalent Elasticsearch 1.x filter would be:

{
  "query" : {
    "filtered" : {
      "filter" : {
        "terms" : {
          "id" : [1, 2, 3]
        }
      }
    }
  }
}

The equivalent Elasticsearch 2.x+ filter would be:

{
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "id" : [1, 2, 3]
        }
      }
    }
  }
}

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.

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]
        }
      }
    }
  }
}

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:El等效运算符在ElasticSearch中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆