0

I am trying to understand how to dynamically build the aggregations with nested and regular filters for regular or nested fields, so, my final query looks like code below and would be fine if it can be simplified to two levels (first - level of filters, second - level of a terms query)

{
  "aggs": {
    "city": {
      "nested": {
        "path": "locations"
      },
      "aggs": {
        "city": {
          "filter": {
            "bool": {
              "must": [
                {
                  "terms": {
                    "locations.country.id": [
                      "4"
                    ]
                  }
                }
              ]
            }
          },
          "aggs": {
            "city": {
              "reverse_nested": {},
              "aggs": {
                "city": {
                  "filter": {
                    "bool": {
                      "must": [
                        {
                          "terms": {
                            "category.id": [
                              "24"
                            ]
                          }
                        }
                      ]
                    }
                  },
                  "aggs": {
                    "city": {
                      "nested": {
                        "path": "locations"
                      },
                      "aggs": {
                        "city": {
                          "terms": {
                            "field": "locations.city.id",
                            "size": 1000
                          },
                          "aggs": {
                            "source": {
                              "top_hits": {
                                "size": 1,
                                "_source": {
                                  "includes": [
                                    "locations.city.id",
                                    "locations.city.names"
                                  ]
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

I am trying to simplify dificult query to Elasticsearch

1 Answer 1

0

According the Elasticsearch documentation

To limit the documents on which all aggregations in a search run, use a top-level query. This is faster than a single filter aggregation with sub-aggregations.

So your simplified query is as following

GET /locations/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "locations",
                        "query": {
                            "terms": {
                                "locations.country.id": [
                                    "4"
                                ]
                            }
                        }
                    }
                },
                {
                    "terms": {
                        "category.id": [
                            "24"
                        ]
                    }
                }
            ]
        }
    },
    "aggs": {
        "city": {
            "nested": {
                "path": "locations"
            },
            "aggs": {
                "by_city_id": {
                    "terms": {
                        "field": "locations.city.id",
                        "size": 1000
                    },
                    "aggs": {
                        "sources": {
                            "top_hits": {
                                "size": 1,
                                "_source": {
                                    "includes": [
                                        "locations.city.id",
                                        "locations.city.names"
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

I don’t know your mapping. So this query could require correction

Sign up to request clarification or add additional context in comments.

Comments

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.