【问题标题】:ElasticSearch NodeJS - Aggregation term return more than one source propertyElasticSearch NodeJS - 聚合项返回多个源属性
【发布时间】:2019-06-06 21:48:07
【问题描述】:

我需要获得一个独特的事物列表,其中包含一些附加的属性。到目前为止,这只是返回一个唯一的名称列表,但是如果我想包含聚合文档的 id,我该怎么办?

我正在使用带有 .search() 方法的 elasticsearch npm 模块

任何帮助将不胜感激。

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

这将返回 { key, doc_count } 我想要的 { key, id, doc_count } 的列表

这行得通!谢谢技术专家席德!

如果我的文档看起来像这样呢

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

我如何找到我当前热门的人的 id。例如,这是工作查询。

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

然而这会返回

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

我想知道如何做一个 where 条件,以便它只返回与它当前所在的唯一 cool_things.name.keyword 匹配的 ID。

【问题讨论】:

    标签: node.js elasticsearch elasticsearch-aggregation


    【解决方案1】:

    您最多可以使用top hits aggregation 作为子聚合来跟踪聚合文档。

    示例:

    相似词聚合查询:

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

    将返回以下结果:

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

    如果您将热门点击聚合作为子聚合添加到上述查询中:

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

    你会得到以下结果:

    "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 !! 
    

    请注意,在上述结果中,您的条款存储桶中有聚合文档 _id(value.hits.hits._id)。

    不确定语法,但这样的东西应该适合你:

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

    【讨论】:

    • 我还有一个问题,如果不是太麻烦,我更新了原来的问题。
    猜你喜欢
    • 1970-01-01
    • 2023-01-08
    • 2016-03-13
    • 2018-11-28
    • 2018-06-13
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多