Child documents and aggregations


(Craig Longman) #1

So, I finally figured out how to get buckets for the children documents, using this:

"aggs": {
  "to-children": {
    "children": {
      "type": "folder"
    },
    "aggs": {
      "the-filter": {
        "filters": {
          "filters": {
            "2828": { "term": { "folder.folder_id_fi": 2828 } },
            "2829": { "term": { "folder.folder_id_fi": 2829 } },
            "2830": { "term": { "folder.folder_id_fi": 2830 } }
          }
        },
        "aggs": {
          "metrics" : {
            "sum": {
              "field": "file_size_2_fi"
            }
          }
        }
      }
    }
  }
}

But, the sum isn't working properly, it just contains 0 all the time, typical it seems when a field name isn't found. I'm guessing it's because I'm at the child (folder) level, and the file_size_2_fi field is in the parent document.

Any ideas on how to get this working?

As an side note, I find a LOT of questions about parent/children relationships and aggregations, and almost all of them have no answers (here and SO also), I'm thinking that the support for parent/child is pretty limited and I'm probably trying to do things that are too complicated?

The issue is our folder choices can change more rapidly than anything else, and it also needs to be very quick when clicked on the front end. Knowing that the whole document needs to be reindexed when a field changes, it seemed a good choice to move the folder information into a child document, speeding up changes to the folder information.

CraigL


(Dan Tuffery) #2

You could move the metrics aggregation to the same level as the to-children aggregation so that it is no longer at the child folder level.

"aggs": {
    "metrics": {
        "sum": {
            "field": "file_size_2_fi"
        }
    },
    "to-children": {
        "children": {
            "type": "folder"
        },
        "aggs": {
            "the-filter": {
                "filters": {
                    "filters": {
                        "2828": {
                            "term": {
                                "folder.folder_id_fi": 2828
                            }
                        },
                        "2829": {
                            "term": {
                                "folder.folder_id_fi": 2829
                            }
                        },
                        "2830": {
                            "term": {
                                "folder.folder_id_fi": 2830
                            }
                        }
                    }
                }
            }
        }
    }
}

If that is not what you want to do can you give us a bit more information on what you're trying to achieve?


(Craig Longman) #3

Sorry, I should have mentioned my previous question, it had more info in it. We have two doc types, a document, and then a child called folder.

What I'm trying to do is get the sum of file_size_2_fi field, grouped by each of the three folders, a response something like this:

"aggregations": {
   "to-children": {
      "doc_count": 16177447,
      "the-filter": {
         "buckets": {
            "2828": {
               "doc_count": 688,
               "metrics": {
                  "value": 54963138
               }
            },
            "2829": {
               "doc_count": 170,
               "metrics": {
                  "value": 6649486
               }
            },
            "2830": {
               "doc_count": 94,
               "metrics": {
                  "value": 3984103
               }
            }
         }
      }
   }
}

Not including the "metrics" component in the child portion gives me a value, but just a single value that appears to be all the documents combined.

CraigL


(system) #4