【问题标题】:Get one document per bucket in elasticsearch aggregation在弹性搜索聚合中每个桶获取一个文档
【发布时间】:2018-08-13 16:38:59
【问题描述】:

我在 elasticsearch 中有一个聚合,它给出这样的响应:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1261,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "clusters": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 1073,
      "buckets": [
        {
          "key": 813058,
          "doc_count": 46
        },
        {
          "key": 220217,
          "doc_count": 29
        },
        {
          "key": 287763,
          "doc_count": 23
        },
        {
          "key": 527217,
          "doc_count": 20
        },
        {
          "key": 881778,
          "doc_count": 15
        },
        {
          "key": 700725,
          "doc_count": 14
        },
        {
          "key": 757602,
          "doc_count": 13
        },
        {
          "key": 467496,
          "doc_count": 10
        },
        {
          "key": 128318,
          "doc_count": 9
        },
        {
          "key": 317261,
          "doc_count": 9
        }
      ]
    }
  }
}

我想为聚合中的每个存储桶获取一个文档(通过最高分或随机 - 任何方法都有效)。我该怎么做?

我用来获取聚合的查询是这样的:

GET myindex/_search
{
    "size": 0,
    "aggs": {
        "clusters": {
            "terms": {
                "field": "myfield",
                "size": 100000
            }
        }
    },
    "query": {
                "bool": {
                    "must": [
                        {
                            "query_string": { "default_field": "field1", "query": "val1" }
                        },
                        {
                            "query_string": { "default_field": "field2", "query": "val2" }
                        }
                    ]
                }
            }
}

我正在尝试实现一个基于集群的句子相似性系统,因此我需要这个。我从每个集群中挑选一个句子并检查与给定句子的相似性。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我能够通过使用此处给出的热门点击聚合来解决它:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

    下面的示例查询:

    GET myindex/_search
    {
        "size": 0,
        "aggs": {
            "clusters": {
                "terms": {
                    "field": "myfield",
                    "size": 100000
                },
            "aggs": {
                "mydoc": {
                    "top_hits": {
                        "size" : 1
                    }
                }
            }
            }
        },
        "query": {
                    "bool": {
                        "must": [
                            {
                                "query_string": { "default_field": "field1", "query": "val1" }
                            },
                            {
                                "query_string": { "default_field": "field2", "query": "val2" }
                            }
                        ]
                    }
                }
    }
    

    【讨论】:

    • 嗨 Vignesh,有没有办法对“myfield”上的结果进行排序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2017-09-25
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多