gpt4 book ai didi

elasticsearch - 聚合(一个字段中的许多值)elasticsearch

转载 作者:行者123 更新时间:2023-12-02 22:53:36 24 4
gpt4 key购买 nike

我在一个字段中有很多值,当我进行汇总时,我将这些值作为单独的值接收。
示例:

name : jess , Region : new york 
name : jess , Region : poland
请求:
  query = {
"size": total,
"aggs": {
"buckets_for_name": {
"terms": {
"field": "name",
"size": total
},
"aggs": {
"region_terms": {
"terms": {
"field": "region",
"size": total
}
}
}
}
}
}
response["aggregations"]["buckets_for_name"]["buckets"] 我得到:
 {'key': 'jess ', 'doc_count': 61, 'region_terms': {'doc_count_error_upper_bound': 0, 'sum_other_doc_count': 0, 'buckets': [{'key': 'oran', 'doc_count': 60}, {'key': 'new ', 'doc_count': 1}, {'key': 'york', 'doc_count': 1}]}}, {'key': 'jess ', 'doc_count': 50, 'egion_terms': {'doc_count_error_upper_bound': 0, 'sum_other_doc_count': 0, 'buckets': [{'key': 'poland', 'doc_count': 50}]}}
pretty_results = []
for result in response["aggregations"]["buckets_for_name"]["buckets"]:
d = dict()
d["name"] = result["key"]
d["region"] = []
for region in result["region_terms"]["buckets"]:
d["region "].append(region ["key"])
pretty_results.append(d)
print(d)
我得到:
{'name': 'jess ', 'region ': ['new' , 'york', 'poland']}
我想得到以下结果:
{'name': 'jess ', 'region ': ['new york', 'poland']}

最佳答案

使用标准分析器分析了region(我假设为name)字段,该分析器将new york分解为 token [newyork]。
您可能想做的是设置一个keyword映射,将字符串视为独立 token :

PUT regions
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fielddata": true,
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"region": {
"type": "text",
"fielddata": true,
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
之后,在 .keyword字段上执行aggs:
{
"size": 200,
"aggs": {
"buckets_for_name": {
"terms": {
"field": "name.keyword", <---
"size": 200
},
"aggs": {
"region_terms": {
"terms": {
"field": "region.keyword", <---
"size": 200
}
}
}
}
}
}
如果要保持 newyork的空间较小,请查看分析仪中的 pattern_replace 过滤器。

从评论中编辑
Aggs不是查询的一部分-它们有自己的作用域-因此请更改此设置
{
"query": {
"aggs": {
"buckets_for_name": {
对此
{
"query": {
// possibly leave the whole query attribute out
},
"aggs": {
"buckets_for_name": {
...

关于elasticsearch - 聚合(一个字段中的许多值)elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63264289/

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