1

I am doing term aggregation based on field [type] like below but elastic is returning only 1 term count instead of 2 it is not doing nested object aggregation i.e under comments.data.comments[is a list] under this i have 2 type.

{
    "aggs": {
        "genres": {
            "terms": {
                "field": "comments.data.comments.type"
            }
        }
    }
}
Tirumalesh
  • 95
  • 1
  • 17

1 Answers1

2

Gotta utilize the nested field type:

PUT events
{
  "mappings": {
    "properties": {
      "events": {
        "type": "nested",
        "properties": {
          "ecommerceData": {
            "type": "nested",
            "properties": {
              "comments": {
                "type": "nested",
                "properties": {
                  "recommendationType": {
                    "type": "keyword"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

POST events/_doc
{
  "events": [
    {
      "eventId": "1",
      "ecommerceData": [
        {
          "comments": [
            {
              "rank": 1,
              "recommendationType": "abc"
            },
            {
              "rank": 1,
              "recommendationType": "abc"
            }
          ]
        }
      ]
    }
  ]
}

GET events/_search
{
  "size": 0,
  "aggs": {
    "genres": {
      "nested": {
        "path": "events.ecommerceData.comments"
      },
      "aggs": {
        "nested_comments_recomms": {
          "terms": {
            "field": "events.ecommerceData.comments.recommendationType"
          }
        }
      }
    }
  }
}
Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68