Skip to content
Advertisement

Convert a SQL query to the ElasticSearch query

I wrote this query in SQL and now I needed it in the elastic search. How can I do that?

select * from listings where condition1 = true or (condition2 = 1 and condition3 = false)

Advertisement

Answer

Here you go:

POST listings/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "condition1": {
              "value": "true"
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "condition2": {
                    "value": "1"
                  }
                }
              },
              {
                "term": {
                  "condition3": {
                    "value": "false"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

You need to use should clause for or and must clause for and.

You need to use term or match query based on your requirement.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement