ElasticSearch max score

elasticsearch score threshold
elasticsearch function score
elasticsearch score normalization
elasticsearch'' , script score
elasticsearch disable scoring
elasticsearch boost
elasticsearch function score with bool query
elasticsearch ranking

I'm trying to solve a performance issue we have when querying ElasticSearch for several thousand results. The basic idea is that we do some post-query processing and only show the Top X results ( Query may have ~100000 Results while we only need the top 100 according to our Score Mechanics ).

The basic mechanics are as follows: ElasticSearch Score is normalized between 0..1 ( score/max(score) ), we add our ranking score ( also normalized between 0..1 ) and divide by 2.

What I'd like to do is move this logic into ElasticSearch using custom scoring ( or well, anything that works ): https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score

The Problem I'm facing is that using Score Scripts / Score Functions I can't seem to find a way to do something like max(_score) to normalize the score between 0 and 1.

"script_score" : {
    "script" : "(_score / max(_score) + doc['some_normalized_field'].value)/2"
}

Any ideas are welcome.


You can not get max_score before you have actually generated the _score for all the matching documents. script_score query will first generate the _score for all the matching documents and then max_score will be displayed by elasticsearch.

According to what i can understand from your problem, You want to preserve the max_score that was generated by the original query, before you applied "script_score". You can get the required result if you do some computation at the front-end. In short apply your formula at the front end and then sort the results.

you can save your factor inside your results using script_fields query.

{
  "explain": true, 
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "total_goals": {
      "script": {
        "lang": "painless",
        "source": """
          int total = 0;
          for (int i = 0; i < doc['goals'].length; ++i) {
            total += doc['goals'][i];
          }
          return total;

        """,
        "params":{
          "last" : "any parameters required"
        }

      }
    }
  }
}

ElasticSearch max score, Once the results that match are retrieved, the score they receive will determine how they are rank ordered for relevancy. The scoring of a  Function score queryedit The function_score allows you to modify the score of documents that are retrieved by a query. This can be useful if, for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered set of documents.


I am not sure that I understand your question. do you want to limit the amount of results?

are you tried?

{
    "from" : 0, "size" : 10,
    "query" : {
        "term" : { "name" : "dennis" }
    }
}

you can use sort to define sort order by default it will sorted by main query.

you can also use aggregations ( with or without function_score )

{
  "query": {
    "function_score": {
      "functions": [
        {
          "gauss": {
            "date": {
              "scale": "3d",
              "offset": "7d",
              "decay": 0.1
            }
          }
        },
        {
          "gauss": {
            "priority": {
              "origin": "0",
              "scale": "100"
            }
          }
        }
      ],
      "query": {
        "match" : { "body" : "dennis" }
      }
    }
  },
  "aggs": {

        "hits": {
          "top_hits": {
            "size": 10
          }
        }
      }
}

How scoring works in Elasticsearch, sum. scores are summed. avg. scores are averaged. first. the first function that has a matching filter is applied. max. maximum score is used. min. minimum score  For eg: The field_a contains value from 1 to 100000. After the filters, if the max_score of the field is 5000, then by the above formula, the document contains the max_score & high relevancy will be on the top. and followed by the rest of the documents.


Based on this github ticket it is simply impossible to normalize score and they suggest to use boolean similarity as a workaround.

Function score query | Elasticsearch Reference [7.8], returned doc in the next step of the process. i.e.. if the max score is 2, I want to do something with the top 40 docs that are over a score of 1.5 (if  The maximum score is returned as part of the search response, is that what you are after? On Wednesday, February 2, 2011 at 1:08 PM, Arulkumar wrote: I need in my application to show to user some values based on this max


Getting the max theoretical score for a search query, May i know the maximum value of _score field? Arulkumar (Arulkumar) June 16​, 2015, 2:00pm #3. I need to calculate average score based on max_score and  The score is not on a fixed scale at all. Furthermore, score in different queries should not be compared together. If a document A gets a score of 2 when I query for "quick fox" and document B gets a score of 4 when I query for "brown horse" it does not mean that document A is more relevant to "quick fox" than document B is to "brown horse".


Customize relevance with Elasticsearch, Add the highest score to the multiplied scores. If the tie_breaker value is greater than 0.0 , all matching clauses count, but the clause with the highest score counts​  The script_score query is useful if, for example, a scoring function is expensive and you only need to calculate the score of a filtered set of documents. Example request edit The following script_score query assigns each returned document a score equal to the likes field value divided by 10 .


A Practical Guide on Elasticsearch Scoring and Relevancy, Is it possible to get the maximum _score from all search results inside the function of a function score query? For example:  At the moment the max (or min) aggregator is needed to make sure the buckets from the terms aggregator are ordered according to the score of the most relevant webpage per domain. Unfortunately the top_hits aggregator can’t be used in the order option of the terms aggregator yet.