聚合(一个字段中有多个值)elasticsearch
我在一个字段中有许多值,当我进行聚合时,我将这些值作为单独的值接收 示例:聚合(一个字段中有多个值)elasticsearch,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我在一个字段中有许多值,当我进行聚合时,我将这些值作为单独的值接收 示例: name : jess , Region : new york name : jess , Region : poland query = { "size": total, "aggs": { "buckets_for_name": { "terms": {
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
}
}
}
}
}
}
{'name': 'jess ', 'region ': ['new york', 'poland']}
请求:
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
}
}
}
}
}
}
{'name': 'jess ', 'region ': ['new york', 'poland']}
使用响应[“聚合”][“buckets\u 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}]}}
{'name': 'jess ', 'region ': ['new' , 'york', 'poland']}
与
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)
我得到了:
{'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}]}}
{'name': 'jess ', 'region ': ['new' , 'york', 'poland']}
我想得到这个结果:
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
}
}
}
}
}
}
{'name': 'jess ', 'region ': ['new york', 'poland']}
区域
(我认为名称
)字段是使用标准分析器进行分析的,标准分析器将纽约
划分为标记[纽约
,纽约
]
您可能需要设置关键字
映射,将字符串视为独立的标记:
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
}
}
}
}
}
}
对此
{
"query": {
// possibly leave the whole query attribute out
},
"aggs": {
"buckets_for_name": {
...
结果我得到了一个
[]
!我使用PUT mytable/_映射{“properties”:{“name”:{“type”:“text”,“fielddata”:true,“fields”:{“keyword”:{“type”:“keyword”}}},“region”:{“type”:“text”,“fielddata”:true,“fields”:{“keyword”:{“type”:“keyword”}}}}}
什么是获取mytable/\u search
返回的?你能把它粘贴到这里吗?{“take”:53,“timed\u out”:false,“\u shards”:{“total”:1,“successful”:1,“skiped”:0,“failed”:0},“hits:{“total”:{“value”:2395,“relation”:“eq”},“最高分数”:1,“点击次数”:[{“\U索引”:“mytable”、“\U类型”:“mytable”、“\U id”:“7769”、“\U分数”:1、“\U来源”:{“id\U mytable”:7769,“日期”:“2020-07-21”“地区”:“波兰”、“名称”:“AdminDB”“,},
如果仔细观察,区域
字段名有一个前导空格:“region”。解决这个问题,它就会工作!