Elasticsearch的索引仅包含post_id
和created_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/