简体   繁体   中英

ElasticSearch NodeJS - Aggregation term return more than one source property

I need to get a unique list of things, with some of the properties that are attached. As of now this just returns a unique list of names, yet if I wanted to include the id of the aggregates doc's, what do I do?

I'm using the elasticsearch npm module with the .search() method

Any help would be greatly appreciated.

params.body.aggs = {
    uniqueCoolThings: {
      terms: {
        field: 'cool_thing.name.keyword'
      }
    }
}

This will return a list of { key, doc_count } I want { key, id, doc_count }

That works! Thank you Technocrat Sid!

So what if 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 hit. For example this is the working query.

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

Yet this will return

...hits._source: {
    uniqueCoolThings: [
        {
            "id": 500
        },
        {
            "id": 501
        }
     ]
} ...

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.

At most you can use top hits aggregation as a sub aggregation which keeps the track of the aggregated documents.

Example:

A similar terms aggregation query:

"aggs": {
"uniqueCoolThings": {
  "terms": {
    "field": "cool_thing.name.keyword"
  }
 }
}

will return the following results:

"aggregations": {
"uniqueCoolThings": {
  "doc_count_error_upper_bound": 0,
  "sum_other_doc_count": 0,
  "buckets": [
    {
      "key": "XYZ",
      "doc_count": 2
    },
    {
      "key": "ABC",
      "doc_count": 1
    }
  ]
 }
}

And if you add top hits aggregation as a sub aggregation to the above query:

"aggs": {
"uniqueCoolThings": {
  "terms": {
    "field": "cool_thing.name.keyword"
  },
  "aggs": {
    "value": {
      "top_hits": {
        "_source": "false"
      }
    }
  }
 }
}

You'll get the following result:

"aggregations": {
"uniqueCoolThings": {
  "doc_count_error_upper_bound": 0,
  "sum_other_doc_count": 0,
  "buckets": [
    {
      "key": "XYZ",
      "doc_count": 2,
      "value": {
        "hits": {
          "total": 2,
          "max_score": 1,
          "hits": [
            {
              "_index": "product",
              "_type": "_doc",
              "_id": "BqGhPGgBOkyOnpPCsRPX",
              "_score": 1,
              "_source": {}
            },
            {
              "_index": "product",
              "_type": "_doc",
              "_id": "BaGhPGgBOkyOnpPCfxOx",
              "_score": 1,
              "_source": {}
            }
          ]
        }
      }
    }
    ....
    .... excluding output for brevity !! 

Notice in the above result you have the aggregated documents _id( value.hits.hits._id ) within your terms bucket.

Not sure of the syntax but something like this should work for you:

params.body.aggs = {
uniqueCoolThings: {
  terms: {
    field: 'cool_thing.name.keyword'
  }, 
   aggs: {
   value: {
    top_hits: {
      _source: 'false'      
    }
   }
  }
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM