# Terms aggregations cannot get accurate number of record

**URL:** <https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497>\
**Category:** Elasticsearch\
**Created:** [January 23, 2017, 3:04pm UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497 "2017-01-23T15:04:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![111151](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111151/32/14819_2.png) [@111151](https://discuss.elastic.co/u/111151)\
**Post date:** [January 23, 2017, 3:04pm UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/1 "2017-01-23T15:04:43Z")

</div>

my aggregation query like this :

```
   {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "month_id": {
                  "value": 154,
                  "boost": 1
                }
              }
            }
          ]
        }
      },
      "aggregations": {
        "group_by_thread_id": {
          "terms": {
            "field": "thread_id",
            "size": 50,
            "shard_size": -1,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_term": "asc"
              }
            ]
          }
        }
      },
      "highlight": {},
      "ext": {}
    }

```

response like this as fellow:

```
      {
    "took": 498,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
    },
    "hits": {
    "total": 7140222,
    "max_score": 0,
    "hits": []
    },
    "aggregations": {
    "group_by_thread_id": {
    "doc_count_error_upper_bound": 10,
    "sum_other_doc_count": 7140093,
    "buckets": [
    {
    "key": 4028841712554024,
    "doc_count": 7
    }
    ,
    {
    "key": 4032481949343890,
    "doc_count": 6
    }
    ,
    {
    "key": 4027269313757139,
    "doc_count": 3
    }........

```

but when I query by term the key 4028841712554024 ，query like this :

```
    {
    "size":0,
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "thread_id": {
                  "value": "4028841712554024",
                  "boost": 1
                }
              }
            }
          ]
        }
      },
      "highlight": {},
      "ext": {}
    }

```

response like this :

```
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 55,
"successful": 55,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 0,
"hits": []
}
}

```

my question is : aggregations how can I get accurate value for the record ?

thanks and regards

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 23, 2017, 3:59pm UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/2 "2017-01-23T15:59:44Z")

</div>

> [@111151](#):
>
> but when I query by term the key 4028841712554024 ，query like this :

Is missing the month\_id constraint from the original query?

---

<div class="post-metadata">

**Author:** ![111151](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111151/32/14819_2.png) [@111151](https://discuss.elastic.co/u/111151)\
**Post date:** [January 24, 2017, 1:10am UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/3 "2017-01-24T01:10:56Z")

</div>

hi @Mark_Harwood morning, I add new filter month\_id like this

```
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "month_id": {
              "value": 154,
              "boost": 1
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "thread_id": {
                    "value": "4028841712554024",
                    "boost": 1
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

but the result still same

```
      {
    "took": 2,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
    },
    "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
    }
    }
```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [January 24, 2017, 1:39am UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/4 "2017-01-24T01:39:40Z")

</div>

Please format (and indent) your code using `</>` button at the top of the text editor; it makes reading it much easier, thanks!

---

<div class="post-metadata">

**Author:** ![kwu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kwu/32/14714_2.png) [@kwu](https://discuss.elastic.co/u/kwu)\
**Post date:** [January 24, 2017, 2:03am UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/5 "2017-01-24T02:03:39Z")

</div>

One possible reason is this thread\_id field is doc valued but _not indexed_ . Can you provide mapping for this index?

---

<div class="post-metadata">

**Author:** ![111151](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111151/32/14819_2.png) [@111151](https://discuss.elastic.co/u/111151)\
**Post date:** [January 24, 2017, 2:13am UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/6 "2017-01-24T02:13:04Z")

</div>

hi @kwu my index metadata info like this

```
  {
    "state": "open",
    "settings": {
    "index": {
    "number_of_shards": "5",
    "provided_name": "exp_index_1506",
    "max_result_window": "10000000",
    "creation_date": "1483496343921",
    "number_of_replicas": "1",
    "uuid": "4Bhxz-NJQg6oBmIAJAJiSA",
    "version": {
    "created": "5000099"
    }
    }
    },
    "mappings": {
    "ds_item": {
    "properties": {
    "sentiment": {
    "type": "long"
    },
    "date_of_post": {
    "type": "date"
    },
    "item_type_id": {
    "type": "integer"
    },
    "year": {
    "type": "integer"
    },
    "item_type": {
    "type": "keyword"
    },
    "month_id": {
    "type": "integer"
    },
    "subject": {
    "analyzer": "smartcn",
    "type": "text",
    "index_options": "offsets"
    },
    "item_key": {
    "type": "keyword"
    },
    "is_topic_post": {
    "type": "boolean"
    },
    "cicforum_id": {
    "type": "long"
    },
    "sys_site_id": {
    "type": "integer"
    },
    "source": {
    "type": "keyword"
    },
    "sys_thread_id": {
    "type": "long"
    },
    "day_of_month": {
    "type": "integer"
    },
    "cicsite_id": {
    "type": "long"
    },
    "content": {
    "analyzer": "smartcn",
    "type": "text",
    "index_options": "offsets"
    },
    "forum_id": {
    "type": "keyword"
    },
    "domain_id": {
    "type": "long"
    },
    "forum_url": {
    "type": "keyword"
    },
    "thread_id": {
    "type": "long"
    },
    "project_id": {
    "type": "integer"
    },
    "keyword_group_id": {
    "type": "integer"
    },
    "pkey": {
    "type": "keyword"
    },
    "keyword": {
    "type": "keyword"
    },
    "keyword_group": {
    "type": "keyword"
    },
    "sys_poster_id": {
    "type": "long"
    },
    "thread_month_id": {
    "type": "long"
    },
    "time_id": {
    "type": "integer"
    },
    "item_id": {
    "type": "long"
    },
    "week_id": {
    "type": "integer"
    },
    "domain_1": {
    "type": "keyword"
    },
    "driver_categories": {
    "type": "text"
    },
    "poster_url": {
    "type": "keyword"
    },
    "tm_site_name": {
    "type": "keyword"
    },
    "site_name": {
    "type": "keyword"
    },
    "month": {
    "type": "integer"
    },
    "forum_name": {
    "type": "keyword"
    },
    "product_categories": {
    "type": "text"
    },
    "domain": {
    "type": "keyword"
    },
    "item_url": {
    "type": "keyword"
    },
    "poster_month_id": {
    "type": "long"
    },
    "site_id": {
    "type": "integer"
    },
    "poster_id": {
    "type": "keyword"
    },
    "source_id": {
    "type": "integer"
    },
    "poster": {
    "type": "keyword"
    }
    }
    }
    },
    "aliases": [],
    "primary_terms": {
    "0": 1,
    "1": 1,
    "2": 1,
    "3": 1,
    "4": 1
    },
    "in_sync_allocations": {
    "0": [
    "1RroiCrGSAmmskf4PagBtg"
    ,
    "9zERaadWR3ucZb2GCg8cGQ"
    ],
    "1": [
    "TZk52uZbTDuVT9HNl0eL_Q"
    ,
    "XT_UGhyEQoKmJo113Us80w"
    ],
    "2": [
    "tjrsRCdsQdW6oRiggEU3WA"
    ,
    "OLAZ0IgnSPa3ppmTdReWZQ"
    ],
    "3": [
    "yZd4PsgoQ1aKkzkryImBlg"
    ,
    "jvNZGRt_RjKjVftSOQ2ytQ"
    ],
    "4": [
    "W87Nmg3mSx6GA0Y9-VGwzQ"
    ,
    "bS7HdGIlQKSlkBifU9WbOw"
    ]
    }
    }
```

---

<div class="post-metadata">

**Author:** ![kwu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kwu/32/14714_2.png) [@kwu](https://discuss.elastic.co/u/kwu)\
**Post date:** [January 24, 2017, 2:20am UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/7 "2017-01-24T02:20:39Z")

</div>

Are the two queries on the same index? The first response shows hitting on 5 shards while the second on 55 shards?

---

<div class="post-metadata">

**Author:** ![111151](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111151/32/14819_2.png) [@111151](https://discuss.elastic.co/u/111151)\
**Post date:** [January 24, 2017, 2:23am UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/8 "2017-01-24T02:23:19Z")

</div>

the second query because I forget the month\_id and the index name , may be you can reference the third one .

---

<div class="post-metadata">

**Author:** ![kwu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kwu/32/14714_2.png) [@kwu](https://discuss.elastic.co/u/kwu)\
**Post date:** [January 24, 2017, 3:56am UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/9 "2017-01-24T03:56:20Z")

</div>

From the response you can see there are 8 hits.total . The reason that hits array is empty is that you specified "size":0 in term query!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 21, 2017, 3:56am UTC](https://discuss.elastic.co/t/terms-aggregations-cannot-get-accurate-number-of-record/72497/10 "2017-02-21T03:56:37Z")

</div>

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