我想使用ElasticSearch 7.7对联接关系执行聚合。
我需要知道每个父母有多少个孩子。 我发现解决问题的唯一方法是在术语聚合中使用脚本,但我担心的是性能。
/my_index/_search
{
"size": 0,
"aggs": {
"total": {
"terms": {
"script": {
"lang": "painless",
"source": "params['_source']['my_join']['parent']"
}
}
},
"max_total": {
"max_bucket": {
"buckets_path": "total>_count"
}
}
}
}
有人知道避免脚本执行此聚合的更快捷方法吗?
如果连接字段不是父母/孩子,我可以将术语“聚合”替换为:
"terms": { "field": "my_field" }
为提供更多背景信息,我添加了一些有关映射的信息:
我正在使用Elastic 7.7。
我还附加了一些示例文档的映射:
{
"mappings": {
"properties": {
"my_join": {
"relations": {
"other": "doc"
},
"type": "join"
},
"reader": {
"type": "keyword"
},
"name": {
"type": "text"
},
"content": {
"type": "text"
}
}
}
}
PUT example/_doc/1
{
"reader": [
"A",
"B"
],
"my_join": {
"name": "other"
}
}
PUT example/_doc/2
{
"reader": [
"A",
"B"
],
"my_join": {
"name": "other"
}
}
PUT example/_doc/3
{
"content": "abc",
"my_join": {
"name": "doc",
"parent": 1
}
}
PUT example/_doc/4
{
"content": "def",
"my_join": {
"name": "doc"
"parent": 2
}
}
PUT example/_doc/5
{
"content": "def",
"acl_join": {
"name": "doc"
"parent": 1
}
}