当前位置: 首页 > news >正文

【Elasticsearch】parent aggregation

在Elasticsearch中,`Parent Aggregation`是一种特殊的单桶聚合,用于选择具有指定类型的父文档,这些类型是通过一个`join`字段定义的。以下是关于`Parent Aggregation`的详细介绍:

1.基本概念

`Parent Aggregation`是一种聚合操作,主要用于处理父-子关系的文档。通过这种聚合,可以将子文档的聚合结果映射到父文档上,从而实现跨文档类型的聚合。

2.应用场景

假设有一个索引,其中包含问题(`question`)和答案(`answer`)两种类型的文档。`answer`文档通过`join`字段与`question`文档关联。通过`Parent Aggregation`,可以将答案的聚合结果(如答案的数量、答案的作者等)映射到问题文档上。

3.配置方法

在使用`Parent Aggregation`时,需要指定以下内容:

• `type`:指定子文档的类型。例如,在问题和答案的场景中,`type`应设置为`answer`。

• 子聚合:可以在`Parent Aggregation`中嵌套其他聚合操作,例如`terms`、`avg`等。

4.示例

以下是一个具体的例子,展示如何使用`Parent Aggregation`将答案的作者聚合到问题的标签上:

索引映射

```json

PUT parent_example

{

  "mappings": {

    "properties": {

      "join": {

        "type": "join",

        "relations": {

          "question": "answer"

        }

      }

    }

  }

}

```

索引文档

```json

PUT parent_example/_doc/1

{

  "join": {

    "name": "question"

  },

  "body": "I have Windows 2003 server and i bought a new Windows 2008 server...",

  "title": "Whats the best way to file transfer my site from server to a newer one?",

  "tags": [

    "windows-server-2003",

    "windows-server-2008",

    "file-transfer"

  ]

}

 

PUT parent_example/_doc/2?routing=1

{

  "join": {

    "name": "answer",

    "parent": "1"

  },

  "owner": {

    "location": "Norfolk, United Kingdom",

    "display_name": "Sam",

    "id": 48

  },

  "body": "Unfortunately you're pretty much limited to FTP...",

  "creation_date": "2009-05-04T13:45:37.030"

}

```

查询

```json

POST parent_example/_search?size=0

{

  "aggs": {

    "top-names": {

      "terms": {

        "field": "owner.display_name.keyword",

        "size": 10

      },

      "aggs": {

        "to-questions": {

          "parent": {

            "type": "answer"

          },

          "aggs": {

            "top-tags": {

              "terms": {

                "field": "tags.keyword",

                "size": 10

              }

            }

          }

        }

      }

    }

  }

}

```

响应

```json

{

  "aggregations": {

    "top-names": {

      "buckets": [

        {

          "key": "Sam",

          "doc_count": 1,

          "to-questions": {

            "doc_count": 1,

            "top-tags": {

              "buckets": [

                {

                  "key": "file-transfer",

                  "doc_count": 1

                },

                {

                  "key": "windows-server-2003",

                  "doc_count": 1

                },

                {

                  "key": "windows-server-2008",

                  "doc_count": 1

                }

              ]

            }

          }

        }

      ]

    }

  }

}

```

5.注意事项

• `Parent Aggregation`依赖于`join`字段来定义父-子关系。

• 子聚合可以是任意类型的聚合操作,但必须与父文档的类型兼容。

通过`Parent Aggregation`,可以有效地将子文档的聚合结果映射到父文档上,从而实现复杂的跨文档类型的聚合操作。

http://www.lryc.cn/news/532646.html

相关文章:

  • IDEA使用Auto-dev+DeepSeek 10分钟快速集成,让java开发起飞
  • ASP.NET Core中间件Markdown转换器
  • 使用page assist浏览器插件结合deepseek-r1 7b本地模型
  • 【华为OD-E卷 - 108 最大矩阵和 100分(python、java、c++、js、c)】
  • 【Reading Notes】Favorite Articles from 2025
  • 云计算行业分析
  • 【Linux系统】线程:线程的优点 / 缺点 / 超线程技术 / 异常 / 用途
  • 3.攻防世界 weak_auth
  • 代码随想录算法训练营| 二叉树总结
  • Python OCR工具pytesseract识别数字验证码
  • SpringBoot开发(五)SpringBoot接收请求参数
  • 文件基础IO
  • 05vue3实战-----配置项目代码规范
  • 八大排序算法细讲
  • 网络爬虫学习:借助DeepSeek完善爬虫软件,增加停止任务功能
  • docker安装es及分词器ik
  • 【论文阅读】On the Security of “VOSA“
  • Docker 国内最新可用镜像源20250205
  • (2025|ICLR,音频 LLM,蒸馏/ALLD,跨模态学习,语音质量评估,MOS)音频 LLM 可作为描述性语音质量评估器
  • 使用 CSS 实现透明效果
  • 4G核心网的演变与创新:从传统到虚拟化的跨越
  • 数据库系统概论的第六版与第五版的区别,附pdf
  • uniapp小程序自定义中间凸起样式底部tabbar
  • 自己实现的一个缓存数据库(搞着玩) .net Core/6/8/9
  • 在Qt中,slots 关键字有什么用?
  • 如何查看linux机器有几个cpu
  • Swoole如何处理内存泄漏
  • Llama最新开源大模型Llama3.1
  • Pixflow - CL-DJI Drone LUTs 120个大疆Drone无人机相机航拍电影级镜头LUT调色预设
  • 了解AI绘图,Stable Diffusion的使用