Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort Elastic Search query results based on relevancy?

I am trying to do a search in elastic search server.

Below is my case:

Given a term "Hello World" to the search API, return me all the documents with:

  1. exact pattern "Hello World"
  2. "Hello" AND "world"
  3. "Hello" OR "world"

I want to do the above in a single query. I am aware that individually all of them can be done using match_phrase, and default_operator for OR/AND. But I want all the three to be done in a single query.

I want the results to be sorted based on relevancy. So if a document contains the exact phrase, it is most relevant.If the document contains both the words (AND) some where in it, it is moderately relevant. And if the document contains at least one of the word(OR), it is least relevant.

Is it possible in elastic search as of now ?

like image 351
Vipul Kumar Avatar asked Oct 20 '25 17:10

Vipul Kumar


2 Answers

This is something you might require:

Query String

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

{
  "query": 
   {
    "query_string" : 
       {
        "query" : "Hello World"
       }
   }
}

This will fetch you all combinations :

  • Hello, World, Hello World, World Hello.
like image 50
Divya Sriram Avatar answered Oct 22 '25 05:10

Divya Sriram


You want to check Bool Query and possibly Boosting Query Clauses.

For your example, you can do

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "FIELDNAME": {
                            "query": "Hello",
                            "boost": 1
                        }
                    }
                },
                {
                    "match": {
                        "FIELDNAME": {
                            "query": "World",
                            "boost": 1
                        }
                    }
                },
                {
                    "match_phrase": {
                        "FIELDNAME": {
                            "query": "Hello World",
                            "boost": 2
                        }
                    }
                }
            ],
            "minimum_should_match" : 1
        }
    }
}

This is asking that at least 1 between your three conditions is true (minimum_should_match). The more conditions are satisfied, the higher score the document will have (so it will be returned first), like you wished. You can boost the importance of a condition, here I went for doubling the importance of whole phrase match, but it's just an example.

like image 34
Mario Trucco Avatar answered Oct 22 '25 06:10

Mario Trucco