ElasticSearch default scoring mechanism
Asked Answered
I

3

14

What I am looking for, is plain, clear explanation, of how default scoring mechanism of ElasticSearch (Lucene) really works. I mean, does it use Lucene scoring, or maybe it uses scoring of its own?

For example, I want to search for document by, for example, "Name" field. I use .NET NEST client to write my queries. Let's consider this type of query:

IQueryResponse<SomeEntity> queryResult = client.Search<SomeEntity>(s =>
    s.From(0)
   .Size(300)
   .Explain()
   .Query(q => q.Match(a => a.OnField(q.Resolve(f => f.Name)).QueryString("ExampleName")))
);

which is translated to such JSON query:

{
 "from": 0,
 "size": 300,
 "explain": true,
 "query": {
   "match": {
     "Name": {
       "query": "ExampleName"
      }
    }
  }
}

There is about 1.1 million documents that search is performed on. What I get in return, is (that is only part of the result, formatted on my own):

650   "ExampleName" 7,313398

651   "ExampleName" 7,313398

652   "ExampleName" 7,313398

653   "ExampleName" 7,239194

654   "ExampleName" 7,239194

860   "ExampleName of Something" 4,5708737  

where first field is just an Id, second is Name field on which ElasticSearch performed it's searching, and third is score.

As you can see, there are many duplicates in ES index. As some of found documents have diffrent score, despite that they are exactly the same (with only diffrent Id), I concluded that diffrent shards performed searching on diffrent parts of whole dataset, which leads me to trail that the score is somewhat based on overall data in given shard, not exclusively on document that is actually considered by search engine.

The question is, how exactly does this scoring work? I mean, could you tell me/show me/point me to exact formula to calculate score for each document found by ES? And eventually, how this scoring mechanism can be changed?

Ionosphere answered 8/7, 2013 at 8:40 Comment(0)
N
15

The default scoring is the DefaultSimilarity algorithm in core Lucene, largely documented here. You can customize scoring by configuring your own Similarity, or using something like a custom_score query.

The odd score variation in the first five results shown seems small enough that it doesn't concern me much, as far as the validity of the query results and their ordering, but if you want to understand the cause of it, the explain api can show you exactly what is going on there.

Nammu answered 8/7, 2013 at 16:55 Comment(2)
OK, that pretty much covers what I wanted to know. The information that ES is purely using Lucene's Similarity is very helpful. Thanks!Door
More detail about factors in Lucene's scoring algorithm: lucenetutorial.com/advanced-topics/scoring.htmlZymometer
L
3

The score variation is based on the data in a given shard (like you suspected). By default ES uses a search type called 'query then fetch' which, sends the query to each shard, finds all the matching documents with scores using local TDIFs (this will vary based on data on a given shard - here's your problem).

You can change this by using 'dfs query then fetch' search type - prequery each shard asking about term and document frequencies and then sends a query to each shard etc..

You can set it in the url

$ curl -XGET '/index/type/search?pretty=true&search_type=dfs_query_then_fetch' -d '{
  "from": 0,
  "size": 300,
  "explain": true,
  "query": {
    "match": {
      "Name": {
        "query": "ExampleName"
      }
    }
  }
}' 
Lupus answered 10/2, 2015 at 19:32 Comment(0)
S
1

Great explanation in ElasticSearch documentation:

Squall answered 28/8, 2015 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.