gharabat
gharabat

Reputation: 177

How to rank ElasticSearch documents based on scores

I have an Elastic search index that contain thousands of documents, each document represent a user.

each document has set of fields (is_verified: boolean, country: string, is_creator: boolean), also i have another service that call ES search to lookup for documents, how i can rank the retrieved documents based on those fields? for example a verified user with match should come first than un verified one.

is there some kind of document scoring while indexing the documents ? if yes can i modify it based on my criteria ?

what shall i read/look to understand how to rank in elastic search.

thanks

Upvotes: 0

Views: 1754

Answers (2)

Chules
Chules

Reputation: 426

I guess the sorting function mentioned by Mikael is pretty straight forward and should cover your use cases. Check Elastic Doc for more information on that.

But in case you want to do really fancy sorting, maybe you could use a bool query and different boost values to set your desired relevancy for each matched field. It tried to come up with a real life example, but honestly didn't find one. For the sake of completeness, he following snippet should give you an idea how to achieve similar results as with the sort API (but still, i would prefer using sort).

GET /yourindexname/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "Monica"
          }
        }
      ],
      "should": [
        {
          "term": {
            "is_verified": {
              "value": true,
              "boost": 2
            }
          }
        },
        {
          "term": {
            "is_creator": {
              "value": true,
              "boost": 2
            }
          }
        }
      ]
    }
  }
}

is there some kind of document scoring while indexing the documents ? if yes can i modify it based on my criteria ?

I wouldn't assign a fixed score to a document while indexing, as the score should be dependent on the query. However, if you insist to have a predefined relevancy for each document, theoretically you could add a field relevancy having that value for ordering and use it later in the query:

GET /yourindexname/_search
{
    "query" : {
        "match" : {
            "name": "Monica"
        }
    },
    "sort" : [
      {
        "relevancy": {
          "order": "desc"
        },
        "_score"
      }
    ]
}

Upvotes: 1

Lunatic
Lunatic

Reputation: 1906

You can consider using the Sort Api inside your search queries ,In example below we used the search on the field country and sorted the result with respect of Boolean field (is_verified) , You can also add the other Boolean field inside Sort brackets .

GET /yourindexname/_search
{
    "query" : {
        "match" : {
            "country": "Iceland"
        }
    },
    "sort" : [
      {
      "is_verified": {
        "order": "desc"
      }
    }
    ]
}

Upvotes: 1

Related Questions