ElasticSearch - sorting search results by relevance and custom field (date)

For example, I have entities with two fields - text and date. I want the search by objects to be sorted by date. But if I do it simply, the result will be unexpected.
For the search query “Iphone 6”, there are the latest texts with only “6” at the top of the results, and not with “iphone 6”. Without sorting, the results seem nice, but not sorted by date as I want.
How to write a custom sort function that takes into account both relevance and date? Or maybe there is a way to give weight in the Date field, which will be taken into account when calculating?

Also, maybe I want to suppress search results only with "6". How to set up a search to find results only by biram, for example?

+6
source share
5 answers

You tried using a bool request like

{ "query": { "bool": { "must": { "match": { "field": "iphone 6" } } } }, "sort": { "date": { "order": "desc" } } } 

or with your request you can also do this in a more appropriate way, I think ..

just add this as sort

 "sort": [ { "date": { "order": "desc" }}, { "_score": { "order": "desc" }} ] 

All matching results are sorted first by date, then by relevance.

+11
source

You can definitely use an expression to match a phrase . This is a comparison with positioning, so documents will be considered a coincidence for your request only if both iphone and 6 are present in the search fields, and that their appearance matches this order, iphone is displayed up to 6.

0
source

it looks like you want to sort first by relevance and then by date. this request will do it.

  { "query" : { "match" : { "my_field" : "my query" } }, "sort": { "pubDate": { "order": "desc", "mode": "min" } } } 

When sorting by fields with more than one value, remember that the values ​​have no internal order; a multi-valued field is just a bag of values. Which one to choose? For numbers and dates, you can reduce a multi-valued field to a single value using the sort modes min, max, avg or sum. For example, you can sort the earliest date in each date field using the query above.

elasticsearch sort

0
source

The solution is to use _score and the date field in the sort. _score as the first sort order and the date field as the secondary sort order.

You can use a simple match query to match by relevance.

Try it.

Data setting:

 POST ecom/prod { "name":"iphone 6", "date":"2019-02-10" } POST ecom/prod { "name":"iphone 5", "date":"2019-01-10" } POST ecom/prod { "name":"iphone 6", "date":"2019-02-28" } POST ecom/prod { "name":"6", "date":"2019-03-01" } 

Request for relevance and sorting by date:

 POST ecommerce/prododuct/_search { "query": { "match": { "name": "iphone 6" } }, "sort": [ { "_score": { "order": "desc" } }, { "date": { "order": "desc" } } ] } 
0
source

I think your relevance is violated. You must use two different analyzers, one to set up your index and one to search. like this:

 PUT /my_index/my_type/_mapping { "my_type": { "properties": { "name": { "type": "string", "analyzer": "autocomplete", "search_analyzer": "standard" } } } 

You can also read more about it here: https://www.elastic.co/guide/en/elasticsearch/guide/master/_index_time_search_as_you_type.html

Once you correct the relevance, sorting should work correctly.

0
source

Source: https://habr.com/ru/post/1203140/


All Articles