【问题标题】:Elasticsearch DSL query from an SQL statement来自 SQL 语句的 Elasticsearch DSL 查询
【发布时间】:2012-01-23 19:49:17
【问题描述】:

我是 Elasticsearch 的新手。我不认为我完全理解查询和过滤器的概念。就我而言,我只想使用过滤器,因为我不想使用评分等高级功能。

如何将以下 SQL 语句转换为 elasticsearch 查询?

SELECT * FROM advertiser 
WHERE company like '%com%' 
AND sales_rep IN (1,2) 

到目前为止我所拥有的:

curl -XGET 'localhost:9200/advertisers/advertiser/_search?pretty=true' -d ' 
 { 
     "query" : { 
         "bool" : { 
             "must" : { 
                 "wildcard" : { "company" : "*com*" } 
             } 
         } 
     }, 
     "size":1000000 

}' 

如何在 sales_rep 字段中添加 OR 过滤器?

谢谢

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    在你的 must 子句之后添加一个“should”子句。在 bool 查询中,一个或多个 should 子句默认必须匹配。实际上,您可以将“minimum_number_should_match”设置为任意数字,查看bool query docs

    对于您的情况,这应该可行。

        "should" : [
            {
                "term" : { "sales_rep_id" : "1" }
            },
            {
                "term" : { "sales_rep_id" : "2" }
            }
        ],
    

    同样的概念也适用于布尔过滤器。只需将“查询”更改为“过滤器”即可。布尔过滤器文档是here

    【讨论】:

      【解决方案2】:

      我晚了 4 年才看到这篇文章...

      不管怎样,下面的代码也许有用……

      {
        "query": {
          "filtered": {
            "query": {
              "wildcard": {
                "company": "*com*"
              }
            },
            "filter": {
              "bool": {
                "should": [
                  {
                    "terms": {
                      "sales_rep_id": [ "1", "2" ]
                    }
                  }
                ]
              }
            }
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-10-01
        • 1970-01-01
        • 2019-11-22
        • 2018-10-23
        • 1970-01-01
        • 2018-10-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多