elasticsearch

inner_hits aggregations in ElasticSearch


I am trying to do an aggregation of the {"wildcard": {"data.addresses.ces.cp": "maria*"}, {"match": { "data.addresses.ces.direction": "rodriguez"}} fields, but it does not return the results of the query.

{ 
  "_source": "created_at",
  "size": 1, 
  "sort": [{
    "created_at.keyword": {
      "order": "desc"
     }
   }], 
   "query": {
     "nested": {
       "path": "data.addresses",
       "inner_hits": {
       },
       "query": {
         "nested": {
           "path": "data.addresses.ces",
           "query": 
             {"wildcard": {"data.addresses.ces.cp": "maria*"},
             {"match": { "data.addresses.ces.direction": "rodriguez"}}
         }
       }
     }
   }
 }

How can I perform an aggregation that returns the values ​​of the query, and not all the values ​​of the JSON? In case the aggregations don't support inner_hits, how could I get wildcard and match in aggs?


Solution

  • You need to repeat the filter conditions in the aggregation part so that the aggregation only runs on the selected nested documents:

    {
      "_source": "created_at",
      "size": 1,
      "sort": [
        {
          "created_at.keyword": {
            "order": "desc"
          }
        }
      ],
      "query": {
        "nested": {
          "path": "data.addresses",
          "inner_hits": {},
          "query": {
            "nested": {
              "path": "data.addresses.ces",
              "query": {
                "bool": {
                  "filter": [
                    {
                      "wildcard": {
                        "data.addresses.ces.cp": "maria*"
                      }
                    },
                    {
                      "match": {
                        "data.addresses.ces.direction": "rodriguez"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      "aggs": {
        "addresses": {
          "nested": {
            "path": "data.addresses"
          },
          "aggs": {
            "ces": {
              "nested": {
                "path": "data.addresses.ces"
              },
              "aggs": {
                "query": {
                  "filter": {
                    "bool": {
                      "filter": [
                        {
                          "wildcard": {
                            "data.addresses.ces.cp": "maria*"
                          }
                        },
                        {
                          "match": {
                            "data.addresses.ces.direction": "rodriguez"
                          }
                        }
                      ]
                    }
                  },
                  "aggs": {
                    "cp": {
                      "terms": {
                        "field": "data.addresses.ces.cp"
                      }
                    },
                    "direction": {
                      "terms": {
                        "field": "data.addresses.ces.direction"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }