user3953989
user3953989

Reputation: 1929

SQL IN equivalent with ElasticSearch

I'm trying to figure out how to do the equivalent of a SQL IN clause with ElasticSearch.

The query below will work but when I change it to "query": ["1589", "1590"] it does not. I believe it's doing an AND on these 2 values for the same field and I would like it to do an OR or an WHERE IN.

Works

{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "userId": {
              "query": ["1589"]
            }
          }
        }
      ]
    }
  }
}

Fails

{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "userId": {
              "query": ["1589", "1590"]
            }
          }
        }
      ]
    }
  }
}

Upvotes: 0

Views: 579

Answers (2)

NikoNyrh
NikoNyrh

Reputation: 4138

You didn't mention which version you use, 1.7 has terms filter and in 2.x it is replaced by terms query. It seems that userId would be not_analyzed and match is "intended" for analyzed fields. They support various ways of combining the array of terms, at least the filter does.

Upvotes: 0

Vineeth Mohan
Vineeth Mohan

Reputation: 19253

Well the below should work -

{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "userId": {
              "query": ["1589"]
            }
          }
        },
        {
          "match": {
            "userId": {
              "query": ["1590"]
            }
          }
        }

      ]
    }
  }
}

Remember must is something like AND and should can be used ( alone ) to get something like OR

Upvotes: 1

Related Questions