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"
            }
        }
    }
}
11
  • can you provide your mapping ? Commented Apr 10, 2020 at 13:39
  • i am not sure about the mapping as i am new to elastic search. Commented Apr 10, 2020 at 13:44
  • I used your mapping which you commented earlier and can you provide your proper search query, one which you gave have issues Commented Apr 10, 2020 at 13:46
  • { "aggs": { "genres": { "terms": { "field": "events.ecommerceData.comments.recommendationType" } } } } Commented Apr 10, 2020 at 13:49
  • it is already present in the question Commented Apr 10, 2020 at 13:49

1 Answer 1

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"
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Hi jzzfs..Thanks for the right solution.. and the mapping you shown is not working for existing index... and i have re posted my original document..
Gotta drop the index & resync.
Also, looks like you changed ecommerceData to recommendationData so make sure you change it in the mapping too.
i did that it is throwing error object mapping [events] can't be changed from non-nested to nested
Nope -- dropping the index means deleting it. curl -XDELETE ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.