1
votes

I see a few posts around nested fields and aggregation, but none of them seem to answer my question. So, pardon me if this is a repeated question and any help would be greatly appreciated.

We've built an index of lectures, and lectures have the following qualities:

  • A lecture can either be in-person (live) or pre-recorded (online)
  • Each lecture can have multiple chapters
  • Each of these chapters can be covered by different lecturers (example: chapter 1 of quantum physics can be covered by five different lecturers, and three of them may be live and the other two may be online)
  • An online lecture will always have one entry per lecturer per chapter per quality

Roughly the structure is as follows:

{
  "topics": [
    {
      "id": "TOP1",
      "chapters": [
        {
          "chapterId": 12345,
          "availability": [
            {
              "type": "LIVE",
              "lecturer": "Dr. Abraham Fisher",
              "lectureChapterId": "861731",
              "availableFrom": "2017-09-11 13:00:00",
              "expiresAt": "2017-09-11 15:00:00",
              "lecturerIds": [
                "MON121",
                "MEL122"
              ]
            },
            {
              "type": "LIVE",
              "lecturer": "Dr. Bob Fisher",
              "lectureChapterId": "181751",
              "availableFrom": "2017-09-11 20:00:00",
              "expiresAt": "2017-09-11 22:00:00",
              "lecturerIds": [
                "MON122",
                "MEL123"
              ]
            },
            {
              "type": "LIVE",
              "lecturer": "Dr. Bob Fisher",
              "lectureChapterId": "181751",
              "availableFrom": "2017-09-17 20:00:00",
              "expiresAt": "2017-09-17 22:00:00",
              "lecturerIds": [
                "MON122",
                "MEL123"
              ]
            },
            {
              "type": "LIVE",
              "lecturer": "Dr. Abraham Fisher",
              "lectureChapterId": "861731",
              "availableFrom": "2017-09-17 13:00:00",
              "expiresAt": "2017-09-17 15:00:00",
              "lecturerIds": [
                "MON121",
                "MEL122"
              ]
            },
            {
              "type": "ONLINE",
              "quality" : "HD",
              "price" : 19.99,
              "lecturer": "Dr. Catherine Fisher",
              "lectureChapterId": "9127312",
              "availableFrom": "2017-01-17 00:00:00",
              "expiresAt": "2017-12-31 23:59:59",
              "lecturerIds": [
                "MON120",
                "MEL120"
              ]
            },
           {
              "type": "ONLINE",
              "quality" : "SD",
              "price" : 10.99,
              "lecturer": "Dr. Catherine Fisher",
              "lectureChapterId": "9127312",
              "availableFrom": "2017-01-17 00:00:00",
              "expiresAt": "2017-12-31 23:59:59",
              "lecturerIds": [
                "MON120",
                "MEL120"
              ]
            }
          ]
        }
      ]
    }
  ]
}

Now if the requirement is to return only the details first available lecture grouped by chapter, lecturer for LIVE lectures and return all online lectures (along with other metadata for the lecture topic), what is the best way to do that? In the example above, lectures by Dr. Abraham Fisher and Dr. Bob Fisher on the 11th of September should be returned.

I tried using inner_hits, but apparently, it doesn't allow aggregations (I get the following error).

"[nested] query does not support [aggs]"

P.S: The aggregation needs to be at a chapter level and not at the lecture topic (root) level.

1

1 Answers

1
votes

Can you post your query? For an aggregation of a nested field, you always need to specify the nested path. See https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html

I do not know your full data model, but if you have control over the document structure and you only need it for search, you could try to flatten it a bit more by denormalization.