【发布时间】:2019-09-04 21:10:34
【问题描述】:
对不起,如果我的问题可能重复,我只是没有找到类似的东西。
我通过 Python 向 Elasticsearch 发送请求。
这是我的代码:
import json
import requests
query = {
"size": 5,
"_source": ["UserId", "Name", "Status"],
"query": {
"match_all": {
}
}
}
query = json.dumps(query)
response = requests.get(f'{ES_URL}/{ES_INDEX}/_search',
headers={'Content-Type': 'application/json'},
data=query)
这是我的回应:
{'took': 16,
'timed_out': False,
'_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
'hits': {'total': 2069099,
'max_score': 1.0,
'hits': [{'_index': 'index2',
'_type': 'indexresult',
'_id': '8768768',
'_score': 1.0,
'_source': {'UserId': 4264151, 'Name': 'Victor', 'Status': 'High'}},
{'_index': 'index2',
'_type': 'indexresult',
'_id': '5463255',
'_score': 1.0,
'_source': {'UserId': 4264151, 'Name': 'Victor', 'Status': 'Medium'}},
{'_index': 'index2',
'_type': 'indexresult',
'_id': '2323564',
'_score': 1.0,
'_source': {'UserId': 4327653, 'Name': 'John', 'Status': 'Medium'}},
{'_index': 'index2',
'_type': 'indexresult',
'_id': '3564123',
'_score': 1.0,
'_source': {'UserId': 4327653, 'Name': 'John', 'Status': 'Low'}},
{'_index': 'index2',
'_type': 'indexresult',
'_id': '4456256',
'_score': 1.0,
'_source': {'UserId': 7893231, 'Name': 'Sebastian', 'Status': 'Low'}]}}
响应包含两个重复的 UserId 值(4264151 和 4327653)。
问题:需要在 Elasticsearch 查询中编写什么内容才能仅获取唯一的 UserId 值(例如返回随机或首次出现的 UserId)?
也就是说,我希望响应看起来像这样:
{'took': 16,
'timed_out': False,
'_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
'hits': {'total': 2069099,
'max_score': 1.0,
'hits': [{'_index': 'index2',
'_type': 'indexresult',
'_id': '8768768',
'_score': 1.0,
'_source': {'UserId': 4264151, 'Name': 'Victor', 'Status': 'High'}},
{'_index': 'index2',
'_type': 'indexresult',
'_id': '2323564',
'_score': 1.0,
'_source': {'UserId': 4327653, 'Name': 'John', 'Status': 'Medium'}}
{'_index': 'index2',
'_type': 'indexresult',
'_id': '4456256',
'_score': 1.0,
'_source': {'UserId': 7893231, 'Name': 'Sebastian', 'Status': 'Low'}]}}
【问题讨论】:
标签: python elasticsearch