gpt4 book ai didi

python - Elasticsearch请求

转载 作者:行者123 更新时间:2023-12-03 01:14:23 26 4
gpt4 key购买 nike

我想对Elasticsearch-dsl或Elasticsearch做以下请求。

Select all users with the same name but different ages
示例:
索引数据:
 {  "name": "name1","age": 20  }
{ "name": "name2","age": 23 }
{ "name": "name3","age": 20 }
{ "name": "name1","age": 22 }
{ "name": "name4","age": 18 }
{ "name": "name2","age": 23 }
{ "name": "name4","age": 18 }
{ "name": "name4","age": 14 }
我想要这样的 结果
结果:
 {  "name": "name4","age": 18 ,"age": 14  }
{ "name": "name1","age": 22 ,"age": 20 }

最佳答案

您需要应用nested aggregations。由于您来自python,因此请遵循python脚本:

from elasticsearch import Elasticsearch

# Connect to the elastic cluster
es=Elasticsearch([{'host':'localhost','port':9200}])

your_data = [
{ "name": "name1","age": 20 },
{ "name": "name2","age": 23 },
{ "name": "name3","age": 20 },
{ "name": "name1","age": 22 },
{ "name": "name4","age": 18 },
{ "name": "name2","age": 23 },
{ "name": "name4","age": 18 },
{ "name": "name4","age": 14 }
]

your_index_name = "test_index"

# indexing your exemple data
for doc in your_data:
es.index(index=your_index_name, body=doc)

首先,您需要为每个名称创建文档存储桶,我将其称为“buckets_for_name”,然后在buckets_for_name内部将嵌套的术语聚合应用于年龄:
# the nested aggregation query 
query = {
"aggs": {
"buckets_for_name": {
"terms": { "field": "name.keyword" },
"aggs": {
"age_terms": {
"terms": {
"field": "age"
}
}
}
}
}
}

res = es.search(index=your_index_name, body=query)

# the results are here
res["aggregations"]["buckets_for_name"]["buckets"]
结果并不理想:
[{'key': 'name4',
'doc_count': 3,
'age_terms': {'doc_count_error_upper_bound': 0,
'sum_other_doc_count': 0,
'buckets': [{'key': 18, 'doc_count': 2}, {'key': 14, 'doc_count': 1}]}},
{'key': 'name1',
'doc_count': 2,
'age_terms': {'doc_count_error_upper_bound': 0,
'sum_other_doc_count': 0,
'buckets': [{'key': 20, 'doc_count': 1}, {'key': 22, 'doc_count': 1}]}},
{'key': 'name2',
'doc_count': 2,
'age_terms': {'doc_count_error_upper_bound': 0,
'sum_other_doc_count': 0,
'buckets': [{'key': 23, 'doc_count': 2}]}},
{'key': 'name3',
'doc_count': 1,
'age_terms': {'doc_count_error_upper_bound': 0,
'sum_other_doc_count': 0,
'buckets': [{'key': 20, 'doc_count': 1}]}}]
好干净这里有个建议:
pretty_results = []
for result in res["aggregations"]["buckets_for_name"]["buckets"]:
d = dict()
d["name"] = result["key"]
d["ages"] = []
for age in result["age_terms"]["buckets"]:
d["ages"].append(age["key"])
pretty_results.append(d)

漂亮的输出:
[{'name': 'name4', 'ages': [18, 14]},
{'name': 'name1', 'ages': [20, 22]},
{'name': 'name2', 'ages': [23]},
{'name': 'name3', 'ages': [20]}]

关于python - Elasticsearch请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63171226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com