Different number of documents between stats and count

Hi all,
I get a different number of documents stored in an index invoking the endpoints /_stats and /_count. The same difference with v.6.5.0 and 6.7.0.

Let me an example.

With: GET /index1/_count
I get:

{
 "count": 4,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  }
}

And 4 is the correct number of documents I posted on ES.

With: GET /index1/_stats
I get:

   {
      "_shards": {
        "total": 6,
        "successful": 3,
        "failed": 0
      },
      "_all": {
        "primaries": {
          "docs": {
            "count": 6,
            "deleted": 0
          },
          ...
          "indexing": {
            "index_total": 4,
            "index_time_in_millis": 148,
            "index_current": 0,
            "index_failed": 0,
            "delete_total": 0,
            "delete_time_in_millis": 0,
            "delete_current": 0,
            "noop_update_total": 0,
            "is_throttled": false,
            "throttle_time_in_millis": 0
          },
     ...
    },
    "total": {
      "docs": {
        "count": 6,
        "deleted": 0
      },
     ...
   }
 ...
}

A total of 6 documents instead of 4.

Does anyone know the reason?
Thanks in advance.
Andrea

This type of difference can come from nested documents. For a simple example:

PUT /test/
{
  "mappings": {
   "_doc": {
      "properties": {
        "foo": {
          "type": "nested"
        }
      }
    }
  }
}
PUT /test/_doc/1
{
  "foo": {
    "bar": "baz"
  }
}
GET /test/_count
GET /test/_stats

You'll see _count returns 1 while _stats returns 2. This is because:

Internally, nested objects index each object in the array as a separate hidden document, meaning that each nested object can be queried independently of the others, with the nested query

And note that:

Because nested documents are indexed as separate documents, they can only be accessed within the scope of the nested query, the nested / reverse_nested aggregations, or nested inner hits.

Thanks!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.