<p dir="auto">It looks like the actual document data indexed for the field "nums" is a list of 4 floats in descending order, but when the aggregation is run to collect the data, only 3 floats are returned and they are in ascending order.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#! /bin/sh

# Delete the index
curl -XDELETE &quot;http://localhost:9200/test&quot;

# Insert a document with one field -- a list of floats
curl -XPUT &quot;http://localhost:9200/test/doc/1?refresh=true&quot; -d '{
  &quot;nums&quot;: [5.0, 3.5, 3.5, 1.0]
}'

# Expected:
# aggregations.foobar.value == [5.0, 3.5, 3.5, 1.0]

# Actual:
# aggregations.foobar.value == [1.0, 3.5, 5.0]
curl -XGET &quot;http://localhost:9200/test/_search&quot; -d '{
    &quot;query&quot;: {
        &quot;match_all&quot;: {}
    },
    &quot;filter&quot;: {},
    &quot;aggregations&quot;: {
        &quot;foobar&quot;: {
            &quot;scripted_metric&quot;: {
                &quot;init_script&quot;: &quot;_agg[\&quot;numList\&quot;] = []&quot;,
                &quot;map_script&quot;: &quot;if (doc != null) { _agg.numList.add(doc[\&quot;nums\&quot;].getValues()) }&quot;,
                &quot;combine_script&quot;: &quot;return _agg.numList&quot;,
                &quot;reduce_script&quot;: &quot;return _aggs.flatten()&quot;
            }
        }
    }
}'
"><pre class="notranslate"><span class="pl-c"><span class="pl-c">#!</span> /bin/sh</span>

<span class="pl-c"><span class="pl-c">#</span> Delete the index</span>
curl -XDELETE <span class="pl-s"><span class="pl-pds">"</span>http://localhost:9200/test<span class="pl-pds">"</span></span>

<span class="pl-c"><span class="pl-c">#</span> Insert a document with one field -- a list of floats</span>
curl -XPUT <span class="pl-s"><span class="pl-pds">"</span>http://localhost:9200/test/doc/1?refresh=true<span class="pl-pds">"</span></span> -d <span class="pl-s"><span class="pl-pds">'</span>{</span>
<span class="pl-s">  "nums": [5.0, 3.5, 3.5, 1.0]</span>
<span class="pl-s">}<span class="pl-pds">'</span></span>

<span class="pl-c"><span class="pl-c">#</span> Expected:</span>
<span class="pl-c"><span class="pl-c">#</span> aggregations.foobar.value == [5.0, 3.5, 3.5, 1.0]</span>

<span class="pl-c"><span class="pl-c">#</span> Actual:</span>
<span class="pl-c"><span class="pl-c">#</span> aggregations.foobar.value == [1.0, 3.5, 5.0]</span>
curl -XGET <span class="pl-s"><span class="pl-pds">"</span>http://localhost:9200/test/_search<span class="pl-pds">"</span></span> -d <span class="pl-s"><span class="pl-pds">'</span>{</span>
<span class="pl-s">    "query": {</span>
<span class="pl-s">        "match_all": {}</span>
<span class="pl-s">    },</span>
<span class="pl-s">    "filter": {},</span>
<span class="pl-s">    "aggregations": {</span>
<span class="pl-s">        "foobar": {</span>
<span class="pl-s">            "scripted_metric": {</span>
<span class="pl-s">                "init_script": "_agg[\"numList\"] = []",</span>
<span class="pl-s">                "map_script": "if (doc != null) { _agg.numList.add(doc[\"nums\"].getValues()) }",</span>
<span class="pl-s">                "combine_script": "return _agg.numList",</span>
<span class="pl-s">                "reduce_script": "return _aggs.flatten()"</span>
<span class="pl-s">            }</span>
<span class="pl-s">        }</span>
<span class="pl-s">    }</span>
<span class="pl-s">}<span class="pl-pds">'</span></span>
</pre></div>
<p dir="auto">We are updating some data in our els store. The data has a creation timestamp so we fetch buckets of 1 hour length from the database. Therefore we need to fetch all items from this date bucket. Since we don´t know how many items could be in the bucket, we se the size of search to Integer.MAX_VALUE.</p>
<p dir="auto">We´re seeing a dramatic drop in performance when setting the hit size that high. When we set the size closer to the actual size, the performance of the request is again expectably fast.</p>
<p dir="auto"><strong>Scroll to my <a href="https://github.com/elastic/elasticsearch/issues/13125#issuecomment-136964243" data-hovercard-type="issue" data-hovercard-url="/elastic/elasticsearch/issues/13125/hovercard">comment</a> for a solution using the scroll API</strong></p>
<p dir="auto">we tried to reproduce the problem as well using the REST API directly:<br>
With Integer.MAX_VALUE (POST {{elastic_search_host}}/resolve_log_v1/entry/_search?pretty)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
    &quot;size&quot; : 2147483647,
    &quot;query&quot; : {
        &quot;bool&quot; : {
            &quot;must&quot; : [
                { &quot;range&quot; : { &quot;trigger.timestamp&quot; : { &quot;gte&quot;: &quot;2015-07-26T16:00:00.000Z&quot;, &quot;lte&quot; : &quot;2015-07-26T17:00:00.000Z&quot; } } }
            ]
        }
    }
}"><pre class="notranslate"><code class="notranslate">{
    "size" : 2147483647,
    "query" : {
        "bool" : {
            "must" : [
                { "range" : { "trigger.timestamp" : { "gte": "2015-07-26T16:00:00.000Z", "lte" : "2015-07-26T17:00:00.000Z" } } }
            ]
        }
    }
}
</code></pre></div>
<p dir="auto">Result:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;took&quot;: 22856,
  &quot;timed_out&quot;: false,
  &quot;_shards&quot;: {
    &quot;total&quot;: 5,
    &quot;successful&quot;: 5,
    &quot;failed&quot;: 0
  },
  &quot;hits&quot;: {
    &quot;total&quot;: 21,
    &quot;max_score&quot;: 1,
    &quot;hits&quot;: ["><pre class="notranslate"><code class="notranslate">{
  "took": 22856,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 21,
    "max_score": 1,
    "hits": [
</code></pre></div>
<p dir="auto">same request with size set to 21</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;took&quot;: 16,
  &quot;timed_out&quot;: false,
  &quot;_shards&quot;: {
    &quot;total&quot;: 5,
    &quot;successful&quot;: 5,
    &quot;failed&quot;: 0
  },
  &quot;hits&quot;: {
    &quot;total&quot;: 21,
    &quot;max_score&quot;: 1,
    &quot;hits&quot;: [
      {
        &quot;_index&quot;: &quot;resolve_log_v1&quot;,
        &quot;_type&quot;: &quot;entry&quot;,
        &quot;_id&quot;: &quot;4851a61c-3a17-494d-b72e-9bce172d45e6&quot;,"><pre class="notranslate"><code class="notranslate">{
  "took": 16,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 21,
    "max_score": 1,
    "hits": [
      {
        "_index": "resolve_log_v1",
        "_type": "entry",
        "_id": "4851a61c-3a17-494d-b72e-9bce172d45e6",
</code></pre></div>
<p dir="auto">when setting the size to 0 we still get a quick response.</p>
<p dir="auto">The total count of entry objects (:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="curl -X POST -H &quot;Content-Type: application/json&quot; -d '{    &quot;size&quot; : 0 }' 'http://localhost:9222/resolve_log_v1/entry/_search?pretty':
{
  &quot;took&quot;: 26,
  &quot;timed_out&quot;: false,
  &quot;_shards&quot;: {
    &quot;total&quot;: 5,
    &quot;successful&quot;: 5,
    &quot;failed&quot;: 0
  },
  &quot;hits&quot;: {
    &quot;total&quot;: 1022824,
    &quot;max_score&quot;: 0,
    &quot;hits&quot;: []
  }
}"><pre class="notranslate"><code class="notranslate">curl -X POST -H "Content-Type: application/json" -d '{    "size" : 0 }' 'http://localhost:9222/resolve_log_v1/entry/_search?pretty':
{
  "took": 26,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1022824,
    "max_score": 0,
    "hits": []
  }
}
</code></pre></div>
<p dir="auto">To optimize, we now do two queries, 1 count, one to geht the hits:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(rangeQuery(&quot;trigger.timestamp&quot;).gte(fromJoda.toString()).lte(toJoda.toString()));
SearchResponse sizeResponse = client.prepareSearch(SearchProvider.INDEX)
                    .setTypes(TYPE)
                    .setSearchType(SearchType.COUNT)
                    .setQuery(queryBuilder)
                    .execute()
                    .actionGet();

SearchResponse response = client.prepareSearch(SearchProvider.INDEX)
                    .setTypes(TYPE)
                    .setQuery(queryBuilder)
                    .setSize((int) sizeResponse.getHits().getTotalHits())
                    .execute()
                    .actionGet();"><pre class="notranslate"><code class="notranslate">BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(rangeQuery("trigger.timestamp").gte(fromJoda.toString()).lte(toJoda.toString()));
SearchResponse sizeResponse = client.prepareSearch(SearchProvider.INDEX)
                    .setTypes(TYPE)
                    .setSearchType(SearchType.COUNT)
                    .setQuery(queryBuilder)
                    .execute()
                    .actionGet();

SearchResponse response = client.prepareSearch(SearchProvider.INDEX)
                    .setTypes(TYPE)
                    .setQuery(queryBuilder)
                    .setSize((int) sizeResponse.getHits().getTotalHits())
                    .execute()
                    .actionGet();
</code></pre></div>
<p dir="auto">this is not ideal, as the bucket theoretically could change.</p>