Home > Mobile >  Elasticsearch aggregation for dynamic attiribute
Elasticsearch aggregation for dynamic attiribute

Time:01-08

In ES, I need aggs for dynamic attribute fields.

My ES document structure

"_source": {
        "id": 80682,
                ....,
        "product_characters": [
            {
                "id": 128024,
                "value_id": 14194,
                "characters": {
                    "id": 749,
                    "slug": "rang",
                                        ...                 
                },
                "product_id": 80682,
                "character_id": 749
            },
                        ...
                 ]
}

There product document structure, and product_characters for attributes of product. And value_id for value of product, character_id for attribute type.

I need to get aggs of, all character_id types with count, and inside value_id's count.

"aggs":[
    {
      "key":121,
      "count":3000,
      "value_id":[
          {
            "key":1402,
            "count":12,
          },
           ...
      ]

}
]

I have tried nested and filters, but not get disired result.

CodePudding user response:

I think Terms Aggs and Nested Aggs resolve your problem.

Ex:

{
  "size": 0,
  "aggs": {
    "agg_value": {
      "nested": {
        "path": "product_characters"
      },
      "aggs": {
        "agg_value_id": {
          "terms": {
            "field": "product_characters.value_id",
            "size": 10
          }
        },
         "agg_character_id": {
          "terms": {
            "field": "product_characters.character_id",
            "size": 10
          }
        }
      }
    }
  }
}
  • Related