# How do I use match query inside a nested aggregation?

**URL:** <https://discuss.elastic.co/t/how-do-i-use-match-query-inside-a-nested-aggregation/295811>\
**Category:** Elasticsearch\
**Created:** [January 31, 2022, 9:39am UTC](https://discuss.elastic.co/t/how-do-i-use-match-query-inside-a-nested-aggregation/295811 "2022-01-31T09:39:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dotel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dotel/32/77606_2.png) [@dotel](https://discuss.elastic.co/u/dotel)\
**Post date:** [January 31, 2022, 9:39am UTC](https://discuss.elastic.co/t/how-do-i-use-match-query-inside-a-nested-aggregation/295811/1 "2022-01-31T09:39:49Z")

</div>

Is there a way to do a match query inside nested aggregation? The query below works fine for my use case but it uses a term filter instead of a match. So searching for "nursery" will result in 0 buckets.

```auto
GET sushant-tenrollment/_search
{
  "size": 0, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "course.id": "61f790158380bf0029b7298b"
          }
        },
        {
          "match": {
            "user.id": "61f79075d3662e002f49e0dc"
          }
        },
        {
          "nested": {
            "path": "units",
            "query": {
              "match": {
                "units.title": "nursery rhymes"
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "units": {
      "nested": {
        "path": "units"
      },
      "aggs": {
        "filtered_by_unit_title": {
          "filter": {
            "term": {
              "units.title.keyword": "nursery rhymes"
            }
          },
          "aggs": {
            "units": {
              "terms": {
                "field": "units.id.keyword",
                "size": 100000
              },
              "aggs": {
                "details": {
                  "top_hits": {
                    "size": 1,
                    "_source": {
                      "includes": [
                        "units.id",
                        "units.status",
                        "units.lastAccessedAt",
                        "units.url",
                        "units.title"
                      ]
                    }
                  }
                },
                "lastAccessedAt": {
                  "max": {
                    "field": "units.lastAccessedAt"
                  }
                },
                "lastAccessedAtSort": {
                  "bucket_sort": {
                    "sort": [
                      {
                        "lastAccessedAt": {
                          "order": "asc"
                        }
                      }
                    ],
                    "size": 100
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

This is what my data looks like

```auto
{
  "id": "61f79076437d97002c71eb03",
  "course": {
    "id": "61f790158380bf0029b7298b",
    "title": "another course",
  },
  "user": {
    "id": "61f79075d3662e002f49e0dc",
    "lastName": "test user"
  },
  "units": [
    {
      "id": "eLrSnueJJh8wTED3rBw6Zk",
      "title": "Integration testing with test containers",
      "status": "completed",
      "url": "http://google.com",
      "lastAccessedAt": "1970-01-01T00:00:00.000Z"
    },
    {
      "id": "fkcoYWDQ7yVsvaRbuFNsSi",
      "title": "Unit testing",
      "status": "enrolled",
      "url": "http://test.com",
      "lastAccessedAt": "1970-01-01T00:00:00.000Z"
    }
  ],
  "status": "experienced",
  "isDeleted": false,
  "lastAccessedAt": "2022-01-31T07:32:33.102Z"
}

```

What I'd like to retrieve is a list of units with their lastAccessedDate and completion status for a particular course and user. To do just that I didn't need aggregations but I'd also like to search based on unit title so I don't say a way to achieve this without aggregation. But if I use aggregation I can't use the match query(?)

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 31, 2022, 10:48am UTC](https://discuss.elastic.co/t/how-do-i-use-match-query-inside-a-nested-aggregation/295811/2 "2022-01-31T10:48:12Z")

</div>

What is the reason that you don't use match query in your filter aggregation?  
What will happen if you use

```auto
"filtered_by_unit_title": {
          "filter": {
            "match": {
              "units.title": "nursery rhymes"
            }
          },

```

---

<div class="post-metadata">

**Author:** ![dotel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dotel/32/77606_2.png) [@dotel](https://discuss.elastic.co/u/dotel)\
**Post date:** [January 31, 2022, 11:17am UTC](https://discuss.elastic.co/t/how-do-i-use-match-query-inside-a-nested-aggregation/295811/3 "2022-01-31T11:17:06Z")

</div>

Thank you for the reply. I forgot to mark this as complete, but that's pretty much what I had to do.

---

<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 28, 2022, 11:17am UTC](https://discuss.elastic.co/t/how-do-i-use-match-query-inside-a-nested-aggregation/295811/4 "2022-02-28T11:17:13Z")

</div>

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