0

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"]
            }
          }
        }
      ]
    }
  }
}

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I guess I need to post another question specific to the C# library for ES. I would like to avoid looping through all of my users to generate an enormous query. Is there anyway to do this using an array style?
Try terms query , it works for your example but will work differently for normal strings.
Isn't this incorrect, he was asking to do OR not AND? This works if you use should instead of must.
0

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.

Comments

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.