Search code examples
elasticsearchelasticsearch-dsl

Check against list field in elastic search


I have field with value as list. For example

_id = 1
tags = ["IT", "mobile", "OS"]

_id = 2
tags = ["Mac", "fast", "laptop"]

_id = 3
tags = ["IT", "android", "OS"]

I got a list to check against the tag field.

sample = ["OS", "opera", "mobile"]

So docs with id 1 and id 3 should match when i query the tags using sample set.(Cause id 1 contains "OS" and "mobile" and id 3 conatins "OS".)

How can i do this on elastic search ?


Solution

  • Try the below boolean query with the "should" keyword.

    GET /_search
    {
      "query": {
        "bool": {
          "should": [
            { "match": { "tags": "OS" }},
            { "match": { "tags": "opera" }},
            { "match": { "tags": "mobile" }} 
          ]
        }
      }
    }
    

    Maybe an important note for further queries: If you plan to search and Object that contains ALL 3 values (but not 2 or just 1 tag) you should definitely read about nested objects in Elasticsearch first.