python - 如何使用elasticsearch.helpers.scan获得聚合结果的分数

标签 python elasticsearch

Elasticsearch的索引仅包含post_idcreated_at
我想使用post_id作为键来group_by。
如果按以下方式使用search (),则可以获取每个post_id的分数。

res = elastic.search(index='play_post',
                      body={
                          "size": 0,
                          "query": {
                              "range": {
                                  "created_at": {
                                      "gte": start_date,
                                      "lte": end_date
                                  }
                              }
                          },
                          "aggs": {
                              "group_by_post_id": {
                                  "terms": {
                                      "field": "post_id"
                                  }
                              }
                          }
                      },
                      request_timeout=300)

结果如下。
{u'hits': {u'hits': [], u'total': 2606639, u'max_score': 0.0}, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}, u'took': 318, u'aggregations': {u'group_by_post_id': {u'buckets': [{u'key': 29062, u'doc_count': 136}, {u'key': 2499828, u'doc_count': 122}, {u'key': 2422738, u'doc_count': 66}, {u'key': 174648, u'doc_count': 65}, {u'key': 1928122, u'doc_count': 65}, {u'key': 2012556, u'doc_count': 62}, {u'key': 377819, u'doc_count': 56}, {u'key': 2856270, u'doc_count': 55}, {u'key': 1417120, u'doc_count': 48}, {u'key': 238278, u'doc_count': 47}], u'sum_other_doc_count': 2605917, u'doc_count_error_upper_bound': 32}}, u'timed_out': False}

现在,由于在Elasticsearch中存储了大量数据,我尝试使用elasticsearch.helpers.scan,尝试获取如下数据。
res = elasticsearch.helpers.scan(elastic,
                                 index='play_post',
                                 scroll='2m',
                                 query={
                                     "size": 0,
                                     "query": {
                                         "range": {
                                             "created_at": {
                                                 "gte": start_date,
                                                 "lte": end_date
                                             }
                                         },
                                     },
                                     "aggs": {
                                         "group_by_post_id": {
                                             "terms": {
                                                 "field": "post_id"
                                             }
                                         }
                                     }
                                 },
                                 request_timeout=300)

但是,结果无法获取如下的post_id的分数。
{u'sort': [0], u'_type': u'play_post', u'_source': {u'post_id': 1281625, u'created_at': u'2018-04-14T19:29:11', u'user_id': 377765}, u'_score': None, u'_index': u'play_post', u'_id': u'd45d181c-0d2f-4bc9-aaa8-46fa5c41b748'}
{u'sort': [0], u'_type': u'play_post', u'_source': {u'post_id': 1632815, u'created_at': u'2018-04-15T13:09:56', u'user_id': 78467}, u'_score': None, u'_index': u'play_post', u'_id': u'cd279f13-42ee-4981-97c7-c18668a9b624'}
{u'sort': [0], u'_type': u'play_post', u'_source': {u'post_id': 1135965, u'created_at': u'2018-04-15T11:58:54', u'user_id': 318212}, u'_score': None, u'_index': u'play_post', u'_id': u'475f7199-4b20-4484-959a-873c38660180'}
.....
...
..
.

请告诉我该怎么做。

最佳答案

Scroll查询没有得分,因为它可以扩展到如此大量的文档。同样在ES项目的github页面中,有人对此行为感到恼火:https://github.com/olivere/elastic/issues/661

要获得与带分数的滚动查询相同的结果,但要慢一点,可以使用search after查询-doc here。您只需要一个日期字段和另一个唯一标识文档的字段-足够_id字段或_uid字段。看看这个答案:Elastic search not giving data with big number for page size

关于python - 如何使用elasticsearch.helpers.scan获得聚合结果的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49977938/

相关文章:

elasticsearch - 如何将脚本字段附加到结果?

c# - 将 ElasticSearch 服务器上的映射与从 C# 类推断的映射进行比较?

python - 如何让记录器在再次写入之前删除现有的日志文件?

python - Python 中的正则表达式 findall()

elasticsearch - 使用 Kibana 6.0 或 7+ (v7.0.1) 从控制台创建索引模式

sorting - 如何在使用脚本对 Elasticsearch 中的结果进行排序时获取 _score 而不是 null

python - 获取数据框中以数字开头的所有值

python - 为什么 QGraphicsScene 的这种实现会导致应用程序在退出时崩溃?

python - 如何将来自不同列的所有相关值放在一起

sorting - Elasticsearch 7-在多字段属性的自定义字段上排序