【问题标题】:Elasticsearch Aggregation: How to Sort Bucket OrderElasticsearch 聚合:如何对存储桶顺序进行排序
【发布时间】:2016-03-15 16:51:12
【问题描述】:

ES 版本:1.5 (Amazon Elasticsearch)

我的目标:在某个字段上获得带有重复数据删除的搜索结果。我目前正在对处理重复数据删除的聚合进行一些研究。所以,我的结果是一个包含 1 个大小的桶的列表桶。但是,我找不到对存储桶列表进行排序的方法。

当前查询:

curl -XGET "http://localhost:9200/myidx/product/_search?search_type=count" -d '{
   "size": 2, 
   "query": {
      "function_score": {
         "field_value_factor": {
           "field": "relevance",
           "factor": 2.0
         },
         "query":  { "term": { "title": "abcd" } },
         "score_mode": "multiply",
         "boost_mode": "multiply"
      }
   },
   "aggs": {
      "unique": {
         "terms": {
           "field": "groupid",
           "size": 2
         },
         "aggs": {
           "sample": {
             "top_hits": {
               "size": 1
             }
           }
         }
      }
   }
}'

结果:

{ ...
"aggregations": {
    "unique": {
      "doc_count_error_upper_bound": 1,
      "sum_other_doc_count": 39,
      "buckets": [
        {
          "key": 717878424,
          "doc_count": 14,
          "sample": {
            "hits": {
              "total": 14,
              "max_score": 45.856163,
              "hits": [
                {
                  "_index": "myidx",
                  "_type": "product",
                  "_id": "89531",
                  "_score": 45.856163,
                  "_source": { ... }
                }
              ]
            }
          }
        },
        {
          "key": 717878423,
          "doc_count": 8,
          "sample": {
            "hits": {
              "total": 8,
              "max_score": 68.78424,
              "hits": [
                {
                  "_index": "myidx",
                  "_type": "product",
                  "_id": "89517",
                  "_score": 68.78424,
                  "_source": { ... }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

我希望看到 max_score=68.78424 作为第一个存储桶的第二个存储桶。这可能吗?

如果聚合不是推荐的解决方案,请告知。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    是的,您可以通过在最大文档分数上添加另一个子聚合并按该分数对 unique terms 聚合进行排序来做到这一点。

    curl -XGET "http://localhost:9200/myidx/product/_search?search_type=count" -d '{
       "size": 2, 
       "query": {
          "function_score": {
             "field_value_factor": {
               "field": "relevance",
               "factor": 2.0
             },
             "query":  { "term": { "title": "abcd" } },
             "score_mode": "multiply",
             "boost_mode": "multiply"
          }
       },
       "aggs": {
          "unique": {
             "terms": {
               "field": "groupid",
               "size": 2,
               "order": {
                  "max_score": "desc"
               }
             },
             "aggs": {
               "max_score": {
                 "max": {
                   "script": "doc.score"
                 }
               },
               "sample": {
                 "top_hits": {
                   "size": 1
                 }
               }
             }
          }
       }
    }'
    

    【讨论】:

    • 没有更明确地提到:我在 AWS 上使用 ES。亚马逊不允许“脚本”。您知道不使用“脚本”是否有解决方法吗?答案仍然可以帮助我理解聚合,所以谢谢。
    • “ES on AWS”是指您使用的是新的 Amazon Elasticsearch 服务,而不是 AWS EC2 上的自定义安装,对吧?
    • 恐怕,我没有看到任何解决方案。您必须在客户端应用程序中执行此操作。
    • 我接受这个作为这个问题的答案。因此,我可能会采用的解决方案是使用额外的字段“group-head”对文档进行索引,然后对其进行过滤以处理重复数据删除,并且顺序应该与往常一样。
    猜你喜欢
    • 2016-09-08
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2019-01-27
    • 2018-09-14
    相关资源
    最近更新 更多