Nested字段中的嵌套聚合?

huangapple go评论77阅读模式
英文:

Nested aggregation in nested field?

问题

You can use Elasticsearch aggregations to achieve this grouping. Here's how you can get all documents grouped by areas, which are then grouped by countries, and retrieve the full documents:

{
  "size": 0,
  "aggs": {
    "areas": {
      "nested": {
        "path": "areas"
      },
      "aggs": {
        "countries": {
          "nested": {
            "path": "countries"
          },
          "aggs": {
            "full_document": {
              "reverse_nested": {},
              "aggs": {
                "documents": {
                  "top_hits": {
                    "size": 10 // Set the desired size for each group
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

This query first groups documents by areas, then within each area, it further groups them by countries, and finally, it retrieves the full documents using the top_hits aggregation.

Please adjust the size parameter in the top_hits aggregation to control the number of documents returned within each group.

英文:

I am new to elasticsearch and don't know a lot about aggregations but I have this ES6 mapping:

{
    "mappings": {
        "test": {
            "properties": {
                "id": {
                    "type": "integer"
                }
                "countries": {
                    "type": "nested",
                    "properties": {
                        "global_id": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "raw": {
                                    "type": "keyword"
                                }
                            }
                        }
                    }
                },
                "areas": {
                    "type": "nested",
                    "properties": {
                        "global_id": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "raw": {
                                    "type": "keyword"
                                }
                            }
                        },
                        "parent_global_id": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
}

How can I get all documents grouped by areas which is then grouped by countries. Also the document has to be returned in full, not just the nested document. Is this even possible ?

答案1

得分: 1

  1. 聚合 _search 查询:

首先按区域进行聚合,路径为嵌套结构。然后返回到根文档并在嵌套结构中进行国家聚合。

{
  "size": 0,
  "aggs": {
    "agg_areas": {
      "nested": {
        "path": "areas"
      },
      "aggs": {
        "areas_name": {
          "terms": {
            "field": "areas.name"
          },
          "aggs": {
            "agg_reverse": {
              "reverse_nested": {},
              "aggs": {
                "agg_countries": {
                  "nested": {
                    "path": "countries"
                  },
                  "aggs": {
                    "countries_name": {
                      "terms": {
                        "field": "countries.name"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
  1. 获取文档:

在您的聚合中添加 top_hits:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

top_hits 较慢,所以您需要阅读文档并根据上下文调整大小和排序。

...
"terms": {
  "field": "areas.name"
},
"aggregations": {
  "hits": {
    "top_hits": {
      "size": 100
    }
  }
},
...
英文:
  1. Aggregation _search query:

first agg by area, with the path as this is nested. Then reverse to the root document and nested agg to country.

{
  "size": 0,
  "aggs": {
    "agg_areas": {
      "nested": {
        "path": "areas"
      },
      "aggs": {
        "areas_name": {
          "terms": {
            "field": "areas.name"
          },
          "aggs": {
            "agg_reverse": {
              "reverse_nested": {},
              "aggs": {
                "agg_countries": {
                  "nested": {
                    "path": "countries"
                  },
                  "aggs": {
                    "countries_name": {
                      "terms": {
                        "field": "countries.name"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
  1. retrieve documents:

add a tophits inside your aggregation:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

top_hits is slow so you will have to read documentation and adjust size and sort to your context.

...
"terms": {
            "field": "areas.name"
          },
    "aggregations": {
                    "hits": {
                        "top_hits": { "size": 100}
                    }
                },
    ...

huangapple
  • 本文由 发表于 2020年1月6日 22:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定