<p dir="auto">When faceting on more than one index, each with one shard, no replicas, and containing the same data structure, TermsFacet will return the wrong counts. This can be easily verified by running a simple query with the facet value. Additionally, changing the size parameter will cause facet results to change (while we only expected to get a shorter/longer list , we actually see values change and terms disappear).</p>
<p dir="auto">Our faceting query:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
  &quot;from&quot; : 0,
  &quot;size&quot; : 0,
  &quot;query&quot; : {
    &quot;filtered&quot; : {
      &quot;query&quot; : {
        &quot;query_string&quot; : {
          &quot;query&quot; : &quot;test&quot;,
          &quot;fields&quot; : [ &quot;title&quot;, &quot;topic&quot; ]
        }
      },
      &quot;filter&quot; : {
        &quot;numeric_range&quot; : {
          &quot;topic_date&quot; : {
            &quot;from&quot; : &quot;2012-12-17T00:00:00.000+02:00&quot;,
            &quot;to&quot; : &quot;2013-03-17T23:59:59.999+02:00&quot;,
            &quot;include_lower&quot; : true,
            &quot;include_upper&quot; : true
          }
        }
      }
    }
  },
  &quot;facets&quot; : {
    &quot;termFacet&quot; : {
      &quot;terms&quot; : {
        &quot;fields&quot; : [ &quot;author&quot; ],
        &quot;size&quot; : 10
      },
      &quot;facet_filter&quot; : {
        &quot;numeric_range&quot; : {
          &quot;topic_date&quot; : {
            &quot;from&quot; : &quot;2012-12-17T00:00:00.000+02:00&quot;,
            &quot;to&quot; : &quot;2013-03-17T23:59:59.999+02:00&quot;,
            &quot;include_lower&quot; : true,
            &quot;include_upper&quot; : true
          }
        }
      }
    }
  }
}"><pre class="notranslate"><span class="pl-kos">{</span>
  <span class="pl-s">"from"</span> : <span class="pl-c1">0</span><span class="pl-kos">,</span>
  <span class="pl-s">"size"</span> : <span class="pl-c1">0</span><span class="pl-kos">,</span>
  <span class="pl-s">"query"</span> : <span class="pl-kos">{</span>
    <span class="pl-s">"filtered"</span> : <span class="pl-kos">{</span>
      <span class="pl-s">"query"</span> : <span class="pl-kos">{</span>
        <span class="pl-s">"query_string"</span> : <span class="pl-kos">{</span>
          <span class="pl-s">"query"</span> : <span class="pl-s">"test"</span><span class="pl-kos">,</span>
          <span class="pl-s">"fields"</span> : <span class="pl-kos">[</span> <span class="pl-s">"title"</span><span class="pl-kos">,</span> <span class="pl-s">"topic"</span> <span class="pl-kos">]</span>
        <span class="pl-kos">}</span>
      <span class="pl-kos">}</span><span class="pl-kos">,</span>
      <span class="pl-s">"filter"</span> : <span class="pl-kos">{</span>
        <span class="pl-s">"numeric_range"</span> : <span class="pl-kos">{</span>
          <span class="pl-s">"topic_date"</span> : <span class="pl-kos">{</span>
            <span class="pl-s">"from"</span> : <span class="pl-s">"2012-12-17T00:00:00.000+02:00"</span><span class="pl-kos">,</span>
            <span class="pl-s">"to"</span> : <span class="pl-s">"2013-03-17T23:59:59.999+02:00"</span><span class="pl-kos">,</span>
            <span class="pl-s">"include_lower"</span> : <span class="pl-c1">true</span><span class="pl-kos">,</span>
            <span class="pl-s">"include_upper"</span> : <span class="pl-c1">true</span>
          <span class="pl-kos">}</span>
        <span class="pl-kos">}</span>
      <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
  <span class="pl-kos">}</span><span class="pl-kos">,</span>
  <span class="pl-s">"facets"</span> : <span class="pl-kos">{</span>
    <span class="pl-s">"termFacet"</span> : <span class="pl-kos">{</span>
      <span class="pl-s">"terms"</span> : <span class="pl-kos">{</span>
        <span class="pl-s">"fields"</span> : <span class="pl-kos">[</span> <span class="pl-s">"author"</span> <span class="pl-kos">]</span><span class="pl-kos">,</span>
        <span class="pl-s">"size"</span> : <span class="pl-c1">10</span>
      <span class="pl-kos">}</span><span class="pl-kos">,</span>
      <span class="pl-s">"facet_filter"</span> : <span class="pl-kos">{</span>
        <span class="pl-s">"numeric_range"</span> : <span class="pl-kos">{</span>
          <span class="pl-s">"topic_date"</span> : <span class="pl-kos">{</span>
            <span class="pl-s">"from"</span> : <span class="pl-s">"2012-12-17T00:00:00.000+02:00"</span><span class="pl-kos">,</span>
            <span class="pl-s">"to"</span> : <span class="pl-s">"2013-03-17T23:59:59.999+02:00"</span><span class="pl-kos">,</span>
            <span class="pl-s">"include_lower"</span> : <span class="pl-c1">true</span><span class="pl-kos">,</span>
            <span class="pl-s">"include_upper"</span> : <span class="pl-c1">true</span>
          <span class="pl-kos">}</span>
        <span class="pl-kos">}</span>
      <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">I'm working with nested documents and have noticed that my faceted search interface is giving the wrong counts when I have more than one shard. To be more specific, I'm working with RDF triples (entity &gt; attribute &gt; value) and I'm nesting the attributes (called predicates in my example):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;_id&quot; : &quot;512a2c022f0b4e3daa341e6c8bcf6c2f&quot;,
  &quot;url&quot;: &quot;http://dbpedia.org/resource/Alan_Shepard&quot;,
  &quot;predicates&quot;: [
    {
      &quot;type&quot;: &quot;type&quot;,
      &quot;string_value&quot;: [&quot;thing&quot;, &quot;person&quot;, &quot;astronaut&quot;]
    }, {
      &quot;type&quot;: &quot;label&quot;,
      &quot;string_value&quot;: [&quot;Alan Shepard&quot;]
    }, {
      &quot;type&quot;: &quot;time in space&quot;,
      &quot;float_value&quot;: [216.950]
    },
    ... lots more
  ]
}"><pre class="notranslate"><code class="notranslate">{
  "_id" : "512a2c022f0b4e3daa341e6c8bcf6c2f",
  "url": "http://dbpedia.org/resource/Alan_Shepard",
  "predicates": [
    {
      "type": "type",
      "string_value": ["thing", "person", "astronaut"]
    }, {
      "type": "label",
      "string_value": ["Alan Shepard"]
    }, {
      "type": "time in space",
      "float_value": [216.950]
    },
    ... lots more
  ]
}
</code></pre></div>
<p dir="auto">I've created a shell script (<a href="https://gist.github.com/1196986">https://gist.github.com/1196986</a>) that recreates the problem with a fresh index. The created data set has these totals:</p>
<ul dir="auto">
<li>thing (30)</li>
<li>creative work (20)</li>
<li>video game (10)</li>
<li>tv show (10)</li>
<li>people (10)</li>
</ul>
<p dir="auto">With only <strong>one shard</strong> the following query gives the correct counts no matter what the size parameter is set to:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;size&quot;: 0,
  &quot;query&quot;: {
    &quot;match_all&quot;: {}
  },
  &quot;facets&quot;: {
    &quot;type_counts&quot;: {
      &quot;terms&quot;: {
        &quot;field&quot;: &quot;string_value&quot;,
        &quot;size&quot;: 5
      },
      &quot;nested&quot;: &quot;predicates&quot;,
      &quot;facet_filter&quot;: {
        &quot;term&quot;: {
          &quot;type&quot;: &quot;type&quot;
        }
      }
    }
  }
}"><pre class="notranslate"><code class="notranslate">{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "facets": {
    "type_counts": {
      "terms": {
        "field": "string_value",
        "size": 5
      },
      "nested": "predicates",
      "facet_filter": {
        "term": {
          "type": "type"
        }
      }
    }
  }
}
</code></pre></div>
<p dir="auto">However, with <strong>more than one shard</strong> the size parameter affects the accuracy of the counts. If it is equal to or greater than the number of terms returned by the facet query (5 in this case) then it works fine. However, the terms at the bottom of the list start to display low counts as you reduce the size parameter:</p>
<p dir="auto">With "size" : 4</p>
<ul dir="auto">
<li>thing (30)</li>
<li>creative work (20)</li>
<li>video game (10)</li>
<li><strong>tv show (9)</strong></li>
</ul>
<p dir="auto">With "size" : 3</p>
<ul dir="auto">
<li>thing (30)</li>
<li><strong>creative work (15)</strong></li>
<li><strong>video game (9)</strong></li>
</ul>
<p dir="auto">With "size" : 2</p>
<ul dir="auto">
<li>thing (30)</li>
<li><strong>creative work (15)</strong></li>
</ul>
<p dir="auto">So it looks like the sub-totals from some of the shards aren't being included for some reason. BTW I'm on ubuntu and the problem seems to affect all versions of ES I've tried (17.0, 17.1 and 17.6). Any ideas...?</p>
<p dir="auto">P.S. absolutely loving ES - it's made my life a lot easier :)</p>