Displaying the value of the property that best matches a given search

Hello!

I have a search box which I should use to search across multiple fields a given text. The problem is that I need to display the top ten values according to the property matching score.

My "restaurant_crew" index, for instance, has the following properties:

"restaurant_role"
"dish"
"name"
"email"
"phone"

Let's say that my user searched "ana":

(sudo elasticsearch query)
query: “ana”
match_all:
fields : ["restaurant_role","dish","name","email","phone"]

I have two hits but I don’t need here to pass to my front end the documents. I would need to Identify which property has “the greatest matching value”.

[first document]
{"restaurant_role":"chef", "name":"ana gonzales", "email":"ana@food.com", "phone":7657653245}

In this case, it was “ana gonzales”. So I need to pass this value to my front end.

[second document]
{"restaurant_role":"food analyst", "name":"jane smith", "email":"jane@food.com", "phone":098098098}

For this document the “match” was related to the restaurant_role value “food analyst”.
Consequently, I need to pass “food analyst” to my front end.

In the end, my desired result for this query would be something that looks like that:
{“hits”:
{“hits”:{
{"name":"ana gonzales"},
{"restaurant_role":"food analyst"}
}
}
}

In SQL, I could use nested queries ( some crazy select from select from select union select) to work this out but I don’t really know how to do this using Elasticsearch.

Thanks in advance!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.