0

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.

1
  • 1
    I'm facing the same problem. Commented Nov 22, 2021 at 9:03

1 Answer 1

1

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

{
  "query": {
    "prefix": {
      "tags.keyword": "food"
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But in case of the index with a document with tags: [food features A], this solution doesn't work.
Not sure what you mean. Can you update your question with a sample document and the query you think won't work?

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.