How to "highlight" the request and not the response in multivalue query

Hi guys,

I would first thank you for your help.

Here is my problem : I have an index of products and I request elasticsearch to retreive me all products who match with an array of queries. But I would know for each products which phrase of the array query matched for the product. I can highlight what matched in the product, but I don't want to highlight the product. I want to "highlight" the phrase of the query who matched.

There is my ES query :

> curl -XGET "http://localhost:9200/elastic/products/_search?pretty" -d'
> {
>   "size" : 20,
>   "query" : {
>     "filtered" : {
>       "query" : {
>         "bool" : {
>           "should" : [ {
>             "match" : {
>               "name" : {
>                 "query" : "[plombier, voiture occasion, téléphone, ordinateur, dentiste]",
>                 "type" : "boolean"
>               }
>             }
>           }, {
>             "match" : {
>               "description" : {
>                 "query" : "[plombier, voiture occasion, téléphone, ordinateur, dentiste]",
>                 "type" : "boolean"
>               }
>             }
>           } ]
>         }
>       },
>       "filter" : {
>         "geo_distance" : {
>           "geolocatedPoint" : [ 5.1753719, 50.6366569 ],
>           "distance" : "200.0km"
>         }
>       }
>     }
>   }
> }'

So I would like to know for each product if this is the 1st element of the array who matched or the 4th or etc...

Thanks a lot, I hope you'll can help me.

PS : (I'm using ES with spring-data-elasticsearch but it's not really relevant I think).

I'm not 100% sure what you are asking for, but when folks have questions that sounds like yours usually the thing to look at is named queries and highlight_query.

Hi Nik, thank you for your answer. I just tried named queries and it's nice if I want to know which "field" matched but it's not what I would like. I would try to be clearer.

So my query is composed of several "phrase" like : [shop, tailor, computer, phone] and I would light to know for each product who matched for this array of queries if the product is the result of the march with shop, or tailor, or computer, or phone. Or simply the position in the array (0, 1, 2 or 3).

I don't know if the good way is the highlight in this case, I'm pretty newbie in ES.

I don't care to highlight for example the word in the description of the product n°3 like "new computer with 4Gbyte RAM". I only want that ES say me : result = product n°3, matching query = "computer".

I don't know if it's possible.

Thanks a lot for your help.

You can do that with named queries. Rather than do a single match with all those clauses do one match per clause.

"Highlight" is the name of a feature that extracts snippets from large chunks of text based on the query. It is a bit confusing to use that word when you don't mean that feature - though I understand why you used it in this context.

Ok nice, thanks for your help Nik. I'm going to try to do that! :slight_smile:

And sorry for my mistake, like I said I'm newbie, but it's really cool to have support and help of people like you. :slight_smile: