I have a question about field aggregation

Category is a nested field, there are two fields, one is category_id, one is name., I now use aggregate statement, according to the ID polymerization, then the polymerization results inside can only see the key value is category_id, if I want to show the other word segment under key, such as name fields. How to write?

"category": [
              {
                 "category_id": 1,
                 "name": "category_name_1"
              },
              {
                 "category_id": 2,
                 "name": "category_name_2"
              },
              {
                 "category_id": 2,
                 "name": "category_name_3"
              }
           ]

The results:

"category": {
     "doc_count": 675,
     "category_id": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
           {
              "key": 2,
              "doc_count": 450
           },
           {
              "key": 1,
              "doc_count": 225
           }
        ]
     }
  }

How to read simultaneously to category name?

Assuming that id and category name will have a 1 to 1 mapping, just do a sub aggregation on category name under id. It will give count of unique list of category under that id. For this to work, I believe that category name will have to be defined as keyword instead of text. Just check that once.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.