【问题标题】:Combining Filters in ElasticSearch在 ElasticSearch 中组合过滤器
【发布时间】:2017-05-08 13:28:29
【问题描述】:

这是我想要转换为 elasticsearch 查询的基本 SQL 查询

SELECT product
FROM   products
WHERE  (price = 200 OR ID = "101")
  AND  (price != 300)

这是我尝试过的弹性搜索中的查询

GET /my_store/my_product_items/_search

{
   "query" : {
      "constant_score" : { 
         "filter" : {
            "bool" : {
              "must" : [
                 { "term" : {"price" : 200}}, 
                 { "term" : {"productID" : "101"}} 
              ],
              "must_not" : {
                 "term" : {"price" : 300} 
              }
           }
         }
      }
   }
}

但它没有提供与 SQL 查询相同的输出。支持。

TIA。 :)

【问题讨论】:

标签: elasticsearch filter


【解决方案1】:

好的开始!您只需将must 更改为should 即可:

GET /my_store/my_product_items/_search

{
   "query" : {
      "constant_score" : { 
         "filter" : {
            "bool" : {
              "minimum_should_match": 1,         <--- add this
              "should" : [                       <--- change this
                 { "term" : {"price" : 200}}, 
                 { "term" : {"productID" : "101"}} 
              ],
              "must_not" : {
                 "term" : {"price" : 300} 
              }
           }
         }
      }
   }
}

【讨论】:

  • 什么是“minimum_should_match”?
  • should 子句中至少有一个子句应该匹配,这正是 OR 的全部意义所在
猜你喜欢
  • 1970-01-01
  • 2015-09-17
  • 2014-02-02
  • 2020-12-02
  • 1970-01-01
  • 2015-09-22
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多