2

This is the basic SQL query which I want to convert into elasticsearch query,

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

This is a query in elasticsearch that I have tried:

GET /my_store/my_product_items/_search

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

But it didn't give the same output as per SQL query. Do support.

TIA. :)

1

1 Answer 1

2

Good start! You simply need to change must with should and you're good:

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} 
              }
           }
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What "minimum_should_match" is indicate ?
That at least one clause of the should clauses should match, which is exactly what an OR is all about

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.