How do I use match query inside a nested aggregation?

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.

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

{
  "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(?)

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

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

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

1 Like

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