# How to query count of nested elasticsearch inner\_hits document

**URL:** <https://discuss.elastic.co/t/how-to-query-count-of-nested-elasticsearch-inner-hits-document/115622>\
**Category:** Elasticsearch\
**Created:** [January 16, 2018, 2:37am UTC](https://discuss.elastic.co/t/how-to-query-count-of-nested-elasticsearch-inner-hits-document/115622 "2018-01-16T02:37:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![smithyj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smithyj/32/26592_2.png) [@smithyj](https://discuss.elastic.co/u/smithyj)\
**Post date:** [January 16, 2018, 2:37am UTC](https://discuss.elastic.co/t/how-to-query-count-of-nested-elasticsearch-inner-hits-document/115622/1 "2018-01-16T02:37:27Z")

</div>

How to count of nested elasticsearch inner\_hits document

This is Mappings and Data

```auto
PUT /xxx_index
{
  "mappings": {
    "doc": {
      "properties": {
        "interaction": {
          "type": "object",
          "properties": {
            "activity": {
              "type": "nested",
              "properties": {
                "start_time": {
                  "type": "integer"
                }
              }
            }
          }
        }
      }
    }
  }
}

POST /xxx_index/doc
{
  "interaction": {
    "activity": [
      {
        "start_time": 1
      },
     {
        "start_time": 1
      },
      {
        "start_time": 2
      }
    ]
  }
}

POST /xxx_index/doc
{
  "interaction": {
    "activity": [
      {
        "start_time": 1
      },
     {
        "start_time": 2
      },
      {
        "start_time": 2
      }
    ]
  }
}

```

I want to query `interaction.activity.start_time == 2` and `interaction.activity` the `nested` document `count > 1`

but, i don't know how to write query

next , here's my query synatx, it is return null result

```auto
GET zentrust_cn_test:label_user/_search
{
  "query": {
    "nested": {
      "path": "interaction.activity",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "interaction.activity.start_time": {
                  "value": "1"
                }
              }
            },
            {
              "script": {
                "script": "doc['interaction.activity.start_time'].values.size() > 1"
              }
            }
          ]
        }
      }
    }
  }
}

```

Return Result

```auto
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

```

I very thank you much!

---

<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 13, 2018, 2:37am UTC](https://discuss.elastic.co/t/how-to-query-count-of-nested-elasticsearch-inner-hits-document/115622/2 "2018-02-13T02:37:36Z")

</div>

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