【问题标题】:ElasticSearch, how to order aggregations by other field?ElasticSearch,如何按其他字段排序聚合?
【发布时间】:2017-11-04 05:46:12
【问题描述】:

我有一个问答项目,我想在一个名为 Feed 的部分中实现 Elasticsearch。

这部分是一种最后的活动提要。

这是饲料表:

id | question_id | user_id | action_type  | date_added
---------------------------------------------------------------
26 | 29          | 32      | new_answer   | 2017-04-22 18:34:56
36 | 38          | 35      | new_answer   | 2017-04-24 19:42:40
5  | 52          | 25      | new_question | 2017-04-03 16:28:43
2  | 52          | 20      | new_answer   | 2017-05-05 13:22:41

因此,使用 Elasticsearch,我不想让数据按 question_id 分组并按 id DESC 排序。

所以我这样做了:

{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "questions": {
      "terms": {
        "field": "question.id",
        "order": {
          "_term": "desc"
        }
      }
    }
  }
}

我得到了这个结果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 41,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "questions" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 10,
      "buckets" : [ {
        "key" : "64",
        "doc_count" : 4
      }, {
        "key" : "63",
        "doc_count" : 5
      }, {
        "key" : "62",
        "doc_count" : 4
      }, {
        "key" : "61",
        "doc_count" : 5
      }, {
        "key" : "60",
        "doc_count" : 1
      }, {
        "key" : "59",
        "doc_count" : 1
      }, {
        "key" : "58",
        "doc_count" : 3
      }, {
        "key" : "57",
        "doc_count" : 3
      }, {
        "key" : "56",
        "doc_count" : 3
      }, {
        "key" : "55",
        "doc_count" : 2
      } ]
    }
  }
}

我该怎么做才能获得由iddate_added 订购的questions

谢谢

【问题讨论】:

    标签: php mysql elasticsearch


    【解决方案1】:

    您可以将您的文档按question_id 分组到存储桶中,并使用top hits 子聚合在每个存储桶中按iddate_added 排序。

    这是一个基于您的聚合并按id 降序对每个存储桶中的文档进行排序的示例:

    {
      "size": 0,
      "aggs": {
        "questions": {
          "terms": {
            "field": "question_id",
            "order": {
              "_term": "desc"
            }
          },
          "aggs": {
            "question_docs": {
              "top_hits": {
                "size": 10,
                "sort": [
                  {
                    "id": {
                      "order": "desc"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
    

    假设您的 date_added 映射指定了 date 字段数据类型,那么您也可以在 top_hits 聚合中将 date_added 替换为 id。如果您让 Elasticsearch 为您确定映射,则您的日期可能存储为 text(对于 Elasticsearch 5.x)或 string(任何在 5.x 之前的版本)。我使用带有动态映射的 Elasticsearch 5.4 为您的问题中的示例数据编制了索引;它将日期的映射设置为text(用于全文搜索,使用date_added 访问)和keyword(用于排序和聚合,使用date_added.keyword 访问)。

    您可以使用get mapping API 查看检查索引的映射。例如,要查看索引 <index_name> 的映射,请使用以下命令:

    curl -XGET "http://localhost:9200/<index_name>/_mapping"
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 2016-01-22
      • 2020-06-17
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      相关资源
      最近更新 更多