Elasticsearch equivalent SQL In Query with multiple fields

Viewed 58

I am looking for a Elasticsearch equivalent SQL In query like below

SELECT * from table
WHERE (section , class) in (('a','1'), ('b', '2'))

I know how to use In query for single fields in elasticsearch

SELECT * FROM table WHERE class IN ('1', '2');

Elasticsearch query -

{
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "class" : ['1', '2']
        }
      }
    }
  }
}

My actual problem statement -

Sample index data :

[
{
    "_index" : "some_index",
    "_type" : "_doc",
    "_id" : "41",
    "_score" : 1.0,
    "_source" : {
      "class" : "1",
      "section" : "a",
      "attribute_3" : "hello world"
},
{
    "_index" : "some_index",
    "_type" : "_doc",
    "_id" : "42",
    "_score" : 1.0,
    "_source" : {
      "class" : "2",
      "section" : "a",
      "attribute_3" : "hello world"
},
{
    "_index" : "some_index",
    "_type" : "_doc",
    "_id" : "43",
    "_score" : 1.0,
    "_source" : {
      "class" : "1",
      "section" : "b",
      "attribute_3" : "hello world"
},
{
    "_index" : "some_index",
    "_type" : "_doc",
    "_id" : "44",
    "_score" : 1.0,
    "_source" : {
      "class" : "2",
      "section" : "b",
      "attribute_3" : "hello world"
},
{
    "_index" : "some_index",
    "_type" : "_doc",
    "_id" : "45",
    "_score" : 1.0,
    "_source" : {
      "class" : "3",
      "section" : "b",
      "attribute_3" : "hello world"
}
]

I want to use a filter on data where (class is 1 AND section is a) OR (class is 2 AND section is b) Note : I'm preparing this 'OR' combination dynamically and its going to be more than two combinations.

My expected search result should be -

[{
    "_index" : "some_index",
    "_type" : "_doc",
    "_id" : "41",
    "_score" : 1.0,
    "_source" : {
      "class" : "1",
      "section" : "a",
      "attribute_3" : "hello world"
},
{
    "_index" : "some_index",
    "_type" : "_doc",
    "_id" : "44",
    "_score" : 1.0,
    "_source" : {
      "class" : "2",
      "section" : "b",
      "attribute_3" : "hello world"
}]
1 Answers

This would translate to:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "a": 0
                }
              },
              {
                "term": {
                  "b": 9
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "a": 0
                }
              },
              {
                "term": {
                  "b": 4
                }
              }
            ]
          }
        }
      ]
    }
  }
}

but if a is always 0 as you mentioned in the example the query can be rephrased to:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "a": 0
          }
        },
        {
          "terms": {
            "b": [
              9,
              4
            ]
          }
        }
      ]
    }
  }
}
Related