Adding additional fields to ElasticSearch terms aggregation

后端 未结 2 2001
执笔经年
执笔经年 2021-02-02 09:06

Indexed documents are like:

{
  id: 1, 
  title: \'Blah\',
  ...
  platform: {id: 84, url: \'http://facebook.com\', title: \'Facebook\'}
  ...
}
<
相关标签:
2条回答
  • 2021-02-02 09:17

    If you don't necessarily need to get the value of platform.id, you could get away with a single aggregation instead using a script that concatenates the two fields name and url:

    aggs: {
      platforms: {
        terms: {script: 'doc["platform.name"].value + "," + doc["platform.url"].value'}
      }
    }
    
    0 讨论(0)
  • 2021-02-02 09:25

    It seems the best way to show intentions is top hits aggregation: "from each aggregated group select only one document", and then extract platform from it:

    aggs: {
      platforms: {
        terms: {field: 'platform.id'},
        aggs: {
          platform: {top_hits: {size: 1, _source: {include: ['platform']}}}
      }
    }
    

    This way, each bucked will look like:

    {"key": 7,
      "doc_count": 529939,
      "platform": {
        "hits": {
          "hits": [{
           "_source": {
            "platform": 
              {"id": 7, "name": "Facebook", "url": "http://facebook.com"}
            }
          }]
        }
      },
    }
    

    Which is kinda too deeep (as usual with ES), but clean: bucket.platform.hits.hits.first._source.platform

    0 讨论(0)
提交回复
热议问题