I'm doing a ElasticSearch query with multiple terms constructed dynamically, so it looks like this:
...
must: [
{ terms: { tags_slug: ['term_a', 'term_b'] } },
...
]
...
everything works fine, but I'd like to add to each result, the term that it had match with, so, if for instance the result #1 matched term_a
, I'd like somehow to be able to get that term from the current result, something like this:
Model.search(...).results[0].matched_term # => 'term_a'
Model.search(...).results[1].matched_term # => 'term_b'
As an example, is there a possibility to do this with Elasticsearch? I could do it with Ruby by mapping the results, but maybe there's another way to do it.
Under the current constellation, no. But since the
terms
query is effectively a bunch of bool-shoulds we can take advantage of named queries like so:yielding