Home > Mobile >  Elasticsearch return all field from nested agragation
Elasticsearch return all field from nested agragation

Time:11-08

I query elasticsearch nested filed and in resource get only nested field categories. This code work with fields witch not nested.

aggs: {
        categories: {
            nested: {
                path: "categories"
            },
            aggs: {
                category_name: {
                    terms: {
                        field: "categories.categoryId.keyword",
                        min_doc_count: 1,
                        size: 500,
                        order: {
                            _count: "desc"
                        },
                    },
                    aggs: {
                        product: {
                            top_hits: {
                                size: 1,
                                _source: {
                                    include: [
                                        "id", "assets", "categories"
                                    ]
                                }
                            }
                        }

                    }
                }
            }
        }
    }

CodePudding user response:

Nested context cannot access parent fields. You need to use reverse_nested aggregation.

GET index65/_search
{
  "size": 0,
  "aggs": {
    "categories": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "category_name": {
          "terms": {
            "field": "categories.categoryId.keyword",
            "min_doc_count": 1,
            "size": 500,
            "order": {
              "_count": "desc"
            }
          },
          "aggs": {
            "product": {
              "reverse_nested": {},
              "aggs": {
                "NAME": {
                  "top_hits": {
                    "size": 1,
                    "_source": {
                      "include": [
                       "id", "assets", "categories"
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
  • Related