Home > Software engineering >  Elasticsearch match element in array
Elasticsearch match element in array

Time:11-24

Given this data:

[
  {
    "tags": [
      "food"
    ]
  },
  {
    "tags": [
      "food features"
    ]
  }
]

And index mapping

{
  "mappings": {
    "properties": {
      "tags": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

How can I build a query that returns matched tags [food] and [food features] when I searched for [food] tag, but when I search for food features tag, only [food features] tag is returned? Thanks.

CodePudding user response:

A prefix query on the tags.keyword field could do the job:

{
  "query": {
    "prefix": {
      "tags.keyword": "food"
    }
  }
}
  • Related