首页 > 解决方案 > ElasticSearch 连接上的术语聚合

问题描述

我想使用 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" }

为了提供更多上下文,我添加了一些有关映射的信息:

我正在使用弹性 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
  }
} 

标签: elasticsearchsearchparent-child

解决方案


推荐阅读