0

I am trying to do a doubly nested aggregation on a doubly nested object. That is, I have the root document, a child property, and a grand-child property. To be more precise, I have the following mapping:

{
  "mappings": {
    "root": {
      "properties": {
        "fields": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "keyword"
            },
            "selections": {
              "type": "nested",
              "properties": {
                "value": {
                  "type": "keyword"
                }
              }
            }
          }
        }
      }
    }
  }
}

I am trying to aggregate selection value counts per field, or in other words, to count the number of occurrences of each value for each field name, accross all root objects.

I have this:

{
  "query": {
      ...
  },
  "aggregations": {
    "fields": {
      "nested": {
        "path": "fields"
      },
      "aggregations": {
        "name": {
          "terms": {
            "field": "fields.name"
          },
          "aggregations": {
            "values": {
              "nested": {
                "path": "selections"
              },
              "aggregations": {
                "value": {
                  "terms": {
                    "field": "selections.value"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

which gets the field names as I want but for each of them I get no doc counts for the values.

What am I doing wrong?

1 Answer 1

1

You need to give full name for inner nested field, Change "path":"selections" to "path":"fields.selections"

{
  "size": 0, 
  "aggregations": {
    "fields": {
      "nested": {
        "path": "fields"
      },
      "aggregations": {
        "name": {
          "terms": {
            "field": "fields.name"
          },
          "aggregations": {
            "values": {
              "nested": {
                "path": "fields.selections"
              },
              "aggregations": {
                "value": {
                  "terms": {
                    "field": "fields.selections.value"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Result:

"aggregations" : {
    "fields" : {
      "doc_count" : 2,
      "name" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "abc",
            "doc_count" : 2,
            "values" : {
              "doc_count" : 2,
              "value" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "1",
                    "doc_count" : 2
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thanks fixed my issue, thanks a lot! To anyone who comes across this, I also made the mistake of including the second aggregation level outside of name, whereas it should be inside, just after terms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.