title: ElasticSearch max score
body: <p>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 ).</p>
  
  <p>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.</p>
  
  <p>What I'd like to do is move this logic into ElasticSearch using custom scoring ( or well, anything that works ): <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score" rel="noreferrer">https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score</a></p>
  
  <p>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.</p>
  
  <pre><code>"script_score" : {
      "script" : "(_score / max(_score) + doc['some_normalized_field'].value)/2"
  }
  </code></pre>
  
  <p>Any ideas are welcome.</p>