0

In ElasticSearch is possible transform this SQL in one query?

SELECT * FROM table WHERE state IN ('NY', 'CA', 'FL') AND grade IN ('GOOD', 'BAD') AND active = 'YES' AND quantity > 100 AND quantity < 200
0

1 Answer 1

1

You could use a filtered query with a bool filter for this

{
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : {}
      },
      "filter" : {
        "bool" : {
          "must" : [{
              "terms" : {
                "state" : [
                  "NY",
                  "CA",
                  "FL"
                ]
              }
            }, 
            {
              "terms" : {
                "grade" : [
                  "GOOD",
                  "BAD"
                ]
              }
            }, 
            {
              "term" : {
                "active" : "YES"
              }
            }, 
            {
              "range" : {
                "quantity" : {
                  "gt" : 100,
                  "lt" : 200
                }
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

And if quantity is just quantity > 100?
just remove the lt property on the range filter i.e. { "range" : { "quantity" :{ "gt" : 100 } } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.