# Get total number of matched nested objects present in a elastcsearch index

**URL:** <https://discuss.elastic.co/t/get-total-number-of-matched-nested-objects-present-in-a-elastcsearch-index/328418>\
**Category:** Elasticsearch\
**Created:** [March 24, 2023, 4:57am UTC](https://discuss.elastic.co/t/get-total-number-of-matched-nested-objects-present-in-a-elastcsearch-index/328418 "2023-03-24T04:57:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudheesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sudheesh/32/99073_2.png) [@sudheesh](https://discuss.elastic.co/u/sudheesh)\
**Post date:** [March 24, 2023, 4:57am UTC](https://discuss.elastic.co/t/get-total-number-of-matched-nested-objects-present-in-a-elastcsearch-index/328418/1 "2023-03-24T04:57:31Z")

</div>

consider the following example where I have an index with a nested field

```json
{
  "mappings": {
    "properties": {
      "class": {
        "type": "text"
      },
      "subject": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          }
        }
      }
    }
  }
}

```

Then I indexed some documents to it

```json
PUT test_aggs/_doc/1
{
  "class": "first standard",
  "subject": [
    {
      "name": "english basics"
    },
    {
      "name": "maths basics"
    }
    ]
}

PUT test_aggs/_doc/2
{
  "class": "second standard",
  "subject": [
    {
      "name": "english advanced"
    },
    {
      "name": "maths advanced"
    }
    ]
}

PUT test_aggs/_doc/3
{
  "class": "THIRD standard",
  "subject": [
    {
      "name": "english"
    },
    {
      "name": "c programing"
    },
    {
      "name": "maths"
    }
    ]
}

PUT test_aggs/_doc/4
{
  "class": "fourth standard",
  "subject": [
    {
      "name": "social science"
    },
    {
      "name": "java"
    },
    {
      "name": "science"
    }
    ]
}

```

Now I want to get all the nested objects where the subject name(subject.name field) contains **english** in it.  
This is possible using aggregation query as follows

```json
GET test_aggs/_search
{
  "_source": ["class"], 
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "subject",
            "query": {
              "match": {
                "subject.name": "english"
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "subjects": {
      "nested": {
        "path": "subject"
      },
      "aggs": {
        "matched_subject": {
          "filter": {
            
            "match": {
              "subject.name": 
                "english"
            }
          }
        }
      }
    }
  }
}

```

But it is slower. So are there any other ways to achieve my use case?

My use case is I just want to get the total number of nested objects matched for a query.  
The above index is just an example.

Thank you!

---

<div class="post-metadata">

**Author:** ![sudheesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sudheesh/32/99073_2.png) [@sudheesh](https://discuss.elastic.co/u/sudheesh)\
**Post date:** [March 28, 2023, 6:13am UTC](https://discuss.elastic.co/t/get-total-number-of-matched-nested-objects-present-in-a-elastcsearch-index/328418/2 "2023-03-28T06:13:46Z")

</div>

I have tried to get the answer to my above question. But I didn't get any answer other than aggregation. So I think there is no other way to achieve this .

---

<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:** [April 25, 2023, 6:14am UTC](https://discuss.elastic.co/t/get-total-number-of-matched-nested-objects-present-in-a-elastcsearch-index/328418/3 "2023-04-25T06:14:11Z")

</div>

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