简体   繁体   中英

Getting all documents in Elasticsearch where a field value matches any of array element

What I would like to do is to retrieve all documents where a field value matches any of the values in an array.

So from an array of ids: [id1, id2, id3, ..., idn] , find all documents which has any of these ids.

I can currently achieve that by repeating a match inside of a should , but it does not seem like the right approach.

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "activityOwnerId": "id1"
          }
        },
        {
          "match": {
            "activityOwnerId": "id2"
          }
        },
        ... and so on
      ]
    }
  }
}

How can I get all documents which matches one of the elements in an array?

EDIT

Using terms query as suggested by @Val does not return any results in Kibana:

No results when querying with Terms : 没有条件的结果

Results found (with the same id), when using should : 结果与 should 并列出每个 id

If you're looking at exact matches, you can leverage the terms query instead, which takes an array as input:

{
  "query": {
    "terms": {
      "activityOwnerId.keyword": [id1, id2, id3, ..., idn]
    }
  }
}

There are two types of queries in elasicsearch

  1. Term Queries - which searches the exact terms mentioned in the value
  2. Full-Text queries - Which first analyses the query values, by the analyser mentioned in the field in the mapping

If you want to go form term query, then Answer mentioned by @Val, is perfect,

if you want to write a full-text query, you can use this

GET /_search
{
  "query": {
    "query_string": {
      "query": "activityOwnerId:(id1 OR id2 OR id3...)"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM