Is there something akin to highlighting for `terms` queries in ElasticSearch?

Viewed 122

Let's say I have a document that represents a tagged resource.

{
    "name": "Foo",
    "tags": [1, 2, 3]
},
{
    "name": "Bar",
    "tags": [3, 4, 5]
}

Now let's say I fire a query like this:

{
  "query": {
    "terms": {
      "tags": [3, 5]
    }
  }
}

I want to know that it returns Foo because it's tagged with tag 3 and Bar because it's tagged with both 3 and 5. I know it seems obvious but I'm building a very complex query full of filters and it's important that I display proper highlighting so the user understands why those specific results are being returned. This is easily solved with highlighting for fields on which I am performing a full-text search, but it's complicated on terms queries like this one.

Is there any solution to this problem? I've even started thinking of storing the tag ids in a predefined way as text and performing a full-text search on this field, like this:

{
    "name": "Foo",
    "tags": "1 2 3"
},
{
    "name": "Bar",
    "tags": "3 4 5"
}

And then performing a search like:

{
  "query": {
    "match": {
      "tags": "3 5"
    }
  },
  "highlight": {
    "fields": {
      "tags": {}
    }
  }
}

But I would expect that there's a more elegant solution out there. Any ideas?

Thanks

0 Answers
Related