ThePumpkinMaster
ThePumpkinMaster

Reputation: 2351

Is there a way to see if a query matches any element in an array in Elasticsearch?

I have this query (e.g. 'hello') and this id (e.g. '12345') and I want to search for something that matches both the query in a 'text' field and the id in a 'thread' field. But the given ids are in an array, so the logic is something like:

function runThisQuery(query, ids) {
    client.search({        
    index: '_all',
    type: 'text',
    body: {
      query: {
        bool: {
          must: { 
            match: { text: query } 
          }, 
          should: [
            { match: { thread: { query: ids[0], operator: 'AND'} } },
            { match: { thread: { query: ids[1], operator: 'AND'} } }
          ],
          minimum_should_match: 1
        }
      }
    }
  })
}

Is there like an $in operator (like in MongoDB) that matches the thread if it's in the 'ids' array? Thanks!

Upvotes: 0

Views: 80

Answers (1)

sanurah
sanurah

Reputation: 1122

You can use an ids query like this

{
  "filter": {
    "ids": {
      "type": "my_type",
      "values": [
        "12345","67891","12346"
      ]
    }
  }
}

Upvotes: 1

Related Questions