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

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

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

Then I indexed some documents to it

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

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!

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 .

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