Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch - sort search results by relevance and custom field (Date)

For example, I have entities with two fields - Text and Date. I want search by entities with results sorted by Date. But if I do it simply, then the result is unexpected.
For search query "Iphone 6" there are the newest texts only with "6" in top of еру results, not with "iphone 6". Without sorting the results seem nice, but not ordered by Date as I want.
How write custom sort function which will consider both relevance and Date? Or may be exist way to give weight to field Date which will be consider in scoring?

In addition, may be I shall want to suppress search results only with "6". How to customize search to find results only by bigrams for example?

like image 451
razon Avatar asked Sep 22 '14 13:09

razon


People also ask

How do I sort in Elasticsearch query?

Sort mode optioneditPick the lowest value. Pick the highest value. Use the sum of all values as sort value.

How does Elasticsearch sort work?

In order to sort by relevance, we need to represent relevance as a value. In Elasticsearch, the relevance score is represented by the floating-point number returned in the search results as the _score, so the default sort order is _score descending. In the previous example, we searched for movies from 1962.

What is _score in Elasticsearch?

The _score in Elasticsearch is a way of determining how relevant a match is to the query. The default scoring function used by Elasticsearch is actually the default built in to Lucene which is what Elasticsearch runs under the hood.


2 Answers

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

You can use simple match query to perform relevance match.

Try it out.

Data setup:

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"
}

Query for relevance and date based sorting:

POST ecommerce/prododuct/_search
{
    "query": {
        "match": {
           "name": "iphone 6"
        }
    },
    "sort": [
       {
          "_score": {
             "order": "desc"
          }
       },
       {
           "date": {
             "order": "desc"
          }
       }
    ]
}
like image 129
Vishal Shukla Avatar answered Oct 19 '22 00:10

Vishal Shukla


Did you tried with bool query like this

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

or with your query you can also do this with is more appropriate way of doing i guess ..

just add this as sort

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

all matching results sorted first by date, then by relevance.

like image 19
Akash Yadav Avatar answered Oct 18 '22 23:10

Akash Yadav