2017-05-08 152 views
1

这是基本的SQL查询我想转换成elasticsearch查询,结合过滤器在ElasticSearch

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

这是一个查询在elasticsearch,我曾尝试:

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。 :)

回答

2

好开始!你只需要改变mustshould,你是好:

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} 
       } 
      } 
     } 
     } 
    } 
} 
+0

什么“minimum_should_match”是说明什么? –

+0

“应该”子句中至少有一个子句应该匹配,这正是“OR”所关心的 – Val