0

My index looks like this:

"_source": {
  "ProductName": "Random Product Name",
  "Views": {
    "Washington": [
      { "4nce5bbszjfppltvc": "2018-04-07T18:25:16.160Z" },
      { "4nce5bba8jfpowm4i": "2018-04-07T18:05:39.714Z" },
      { "4nce5bbszjfppltvc": "2018-04-07T18:36:23.928Z" },
    ]
  }
}

I am trying to count the number of unique objects in Views.Washington. In this case, the result would be 2, since two objects have the same key names. ( first and third object in the array ).

Obviously, my first thought was to use aggregations, but I am not sure how to use them with nested objects, like these.

Can this be done with normal aggregations?
Will I need to use a script?

Grim Reaper
  • 561
  • 4
  • 6
  • 22
  • What is your mapping? What is the query you have tried and what is the response/error if any – sramalingam24 Apr 08 '18 at 13:06
  • I haven't created a mapping, I add these fields and ES creates the mappings automatically. I have tried many queries, too many to list here, and none of them seem to have worked. I tried aggregations and even scripts, but I am still not sure how to count them. Every query I try gives me either 0 as a count, or doesn't work. Obviously I'm still pretty new to ES. – Grim Reaper Apr 08 '18 at 13:16
  • Well keep trying that is how you learn. If you are letting ES automap then nested aggregation won't work – sramalingam24 Apr 08 '18 at 13:26
  • Oh, that is good to know! So, I should create my mappings manually first, and then import data? – Grim Reaper Apr 08 '18 at 13:43
  • Yes see here for details https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html – sramalingam24 Apr 08 '18 at 14:00
  • Alright, thank you! :) – Grim Reaper Apr 08 '18 at 14:34

1 Answers1

0

Yes this can be done with Aggregations: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html

core
  • 851
  • 1
  • 8
  • 28
  • Thank you for your answer. I found that piece of documentation but I can't get it to work, hence this post. Could you maybe provide an example of how to implement it in my case? – Grim Reaper Apr 07 '18 at 23:09