Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do two nested aggregations in elasticsearch?

City and home type are two nested objects in the following document mapping:

"mappings" : {
  "home_index_doc" : {
    "properties" : {
      "city" : {
        "type" : "nested",
        "properties" : {
          "country" : {
            "type" : "nested",
            "properties" : {
              "name" : {
                "type" : "text"
              }
            }
          },
          "name" : {
            "type" : "keyword"
          }
        }
      },
      "home_type" : {
        "type" : "nested",
        "properties" : {
          "name" : {
            "type" : "keyword"
          }
        }
      },
      ...
    }
  }
}

I am trying to do the following aggregation: Take all present documents and show all home_types per city.

I imagine it should look similar to:

"aggregations": {
  "all_cities": {
    "buckets": [
      {
        "key": "Tokyo",
         "doc_count": 12,
         "home_types": {
            "buckets": [
               {
                  "key": "apartment", 
                  "doc_count": 5
               },
               {
                  "key": "house",
                  "doc_count": 12
               }
            ]
         }
      },
      {
        "key": "New York",
         "doc_count": 1,
         "home_types": {
            "buckets": [
               {
                  "key": "house", 
                  "doc_count": 1
               }
            ]
         }
      }
    ]
  }
}

After trying gazzilion aproaches and combinations, I've made it that far with Kibana:

GET home-index/home_index_doc/_search
{
  "size": 0,
  "aggs": {
    "all_cities": {
     "nested": {
         "path": "city"
      },
      "aggs": {
        "city_name": {
          "terms": {
            "field": "city.name"
          }
        }
      }
    },
    "aggs": {
      "all_home_types": {
        "nested": {
          "path": "home_type"
        },
        "aggs": {
          "home_type_name": {
            "terms": {
              "field": "home_type.name"
            }
          }
        }
      }
    }
  }
}

and I get the following exception:

    "type": "unknown_named_object_exception",
    "reason": "Unknown BaseAggregationBuilder [all_home_types]",
like image 888
katericata Avatar asked Nov 12 '17 22:11

katericata


People also ask

What is nested aggregation?

Nested aggregationeditA special single bucket aggregation that enables aggregating nested documents. For example, lets say we have an index of products, and each product holds the list of resellers - each having its own price for the product.

Can Kibana perform aggregation across fields that contain nested objects?

But visualizations in Kibana don't aggregate on nested fields like that, regardless of how you set your mappings -- if you want to run aggregations on the data in the items list, you aren't going to get the results you are looking for. Then doing the same sum aggregation should return the expected results.

What is nested type in Elasticsearch?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

What is a reverse nested aggregation?

Reverse nested aggregationedit Effectively this aggregation can break out of the nested block structure and link to other nested structures or the root document, which allows nesting other aggregations that aren't part of the nested object in a nested aggregation.


1 Answers

You need to use reverse_nested in order to jump out of the city nested type back at the root level and do another nested aggregation for the home_type nested type. Basically, like this:

{
  "size": 0,
  "aggs": {
    "all_cities": {
      "nested": {
        "path": "city"
      },
      "aggs": {
        "city_name": {
          "terms": {
            "field": "city.name"
          },
          "aggs": {
            "by_home_types": {
              "reverse_nested": {},
              "aggs": {
                "all_home_types": {
                  "nested": {
                    "path": "home_type"
                  },
                  "aggs": {
                    "home_type_name": {
                      "terms": {
                        "field": "home_type.name"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
like image 123
Val Avatar answered Sep 17 '22 01:09

Val