0

my docs looks like this

{ cool_things: [{ name, id }, { name, id }] }

How would I find the id of the one I'm currently in the aggregate. For example this is the query I'm working with.

params.body.aggs = {
    uniqueCoolThings: {
      terms: {
        field: 'cool_things.name.keyword'
      },
      aggs: {
        value: {
          top_hits: {
            size: 1,
            _source: {
              includes: ['cool_things.id', 'cool_things.name.keyword']
            }
          }
        }
      }
    }
  }
}

Yet this will return

...hits._source: {
    uniqueCoolThings: [
        {
            "id": 500,
            "name": "Bob Sagget"
        },
        {
            "id": 501,
            "name": "Matt Damon"
        }
     ]
} ...

I'm wondering how to do a where condition so that it will only return the ID that matches the unique cool_things.name.keyword it is currently on.

I need to get a list of all unique_cool things, and then have their ID's come back with that response (if that makes sense)

Any help would be GREATLY appreciated!!!!

  • 1
    If you make `cool_things` of type `nested` then this would work – Val Jan 16 '19 at 18:18
  • Could you give me a sudo code example? Sry, just started learning ES :) – Dallas Lones Jan 16 '19 at 21:25
  • I think Val is referring to a [nested aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html). In order to do that, you would need to update the field mapping to be `nested` as well, which is covered in that docs page I linked to. – dmbaughman Jan 17 '19 at 00:41

0 Answers0