点扩展器
dot_expander
处理器是一种帮助您处理分层数据的工具。它将包含点的字段转换为对象字段,使其可供管道中的其他处理器访问。如果没有此转换,带有点的字段将无法被处理。
以下是 dot_expander
处理器的语法:
{
"dot_expander": {
"field": "field.to.expand"
}
}
配置参数
下表列出了 dot_expander
处理器所需和可选的参数。
参数 | 必需/可选 | 描述 |
---|---|---|
field | 必需 | 要扩展为对象字段的字段。 |
路径 | 可选 | 仅当要展开的字段嵌套在另一个对象字段中时,才需要此字段。这是因为 field 参数只识别叶字段。 |
description | 可选 | 处理器的简要描述。 |
if | 可选 | 运行处理器的条件。 |
ignore_failure | 可选 | 如果设置为 true ,则忽略失败。默认为 false 。 |
on_failure | 可选 | 处理器失败时要运行的处理器列表。 |
tag | 可选 | 处理器的标识符标签。有助于调试以区分相同类型的处理器。 |
使用处理器
按照以下步骤在管道中使用处理器。
步骤 1:创建管道
以下查询创建一个 dot_expander
处理器,它将把名为 user.address.city
和 user.address.state
的两个字段扩展为嵌套对象
PUT /_ingest/pipeline/dot-expander-pipeline
{
"description": "Dot expander processor",
"processors": [
{
"dot_expander": {
"field": "user.address.city"
}
},
{
"dot_expander":{
"field": "user.address.state"
}
}
]
}
步骤 2(可选):测试管道
建议在摄取文档之前测试您的管道。
要测试管道,请运行以下查询
POST _ingest/pipeline/dot-expander-pipeline/_simulate
{
"docs": [
{
"_index": "testindex1",
"_id": "1",
"_source": {
"user.address.city": "New York",
"user.address.state": "NY"
}
}
]
}
响应
以下示例响应确认管道按预期工作
{
"docs": [
{
"doc": {
"_index": "testindex1",
"_id": "1",
"_source": {
"user": {
"address": {
"city": "New York",
"state": "NY"
}
}
},
"_ingest": {
"timestamp": "2024-01-17T01:32:56.501346717Z"
}
}
}
]
}
步骤 3:摄取文档
以下查询将文档摄取到名为 testindex1
的索引中
PUT testindex1/_doc/1?pipeline=dot-expander-pipeline
{
"user.address.city": "Denver",
"user.address.state": "CO"
}
步骤 4(可选):检索文档
要检索文档,请运行以下查询
GET testindex1/_doc/1
响应
以下响应确认了指定的字段已扩展为嵌套字段
{
"_index": "testindex1",
"_id": "1",
"_version": 1,
"_seq_no": 3,
"_primary_term": 1,
"found": true,
"_source": {
"user": {
"address": {
"city": "Denver",
"state": "CO"
}
}
}
}
path
参数
您可以使用 path
参数来指定对象内带点字段的路径。例如,以下管道指定了位于 user
对象内的 address.city
字段
PUT /_ingest/pipeline/dot-expander-pipeline
{
"description": "Dot expander processor",
"processors": [
{
"dot_expander": {
"field": "address.city",
"path": "user"
}
},
{
"dot_expander":{
"field": "address.state",
"path": "user"
}
}
]
}
您可以按如下方式模拟管道:
POST _ingest/pipeline/dot-expander-pipeline/_simulate
{
"docs": [
{
"_index": "testindex1",
"_id": "1",
"_source": {
"user": {
"address.city": "New York",
"address.state": "NY"
}
}
}
]
}
dot_expander
处理器将文档转换为以下结构
{
"user": {
"address": {
"city": "New York",
"state": "NY"
}
}
}
字段名冲突
如果已存在一个字段,其路径与 dot_expander
处理器应扩展值的路径相同,则处理器会将这两个值合并到一个数组中。
考虑以下扩展字段 user.name
的管道:
PUT /_ingest/pipeline/dot-expander-pipeline
{
"description": "Dot expander processor",
"processors": [
{
"dot_expander": {
"field": "user.name"
}
}
]
}
您可以使用包含两个值且路径完全相同的文档 user.name
来模拟该管道
POST _ingest/pipeline/dot-expander-pipeline/_simulate
{
"docs": [
{
"_index": "testindex1",
"_id": "1",
"_source": {
"user.name": "John",
"user": {
"name": "Steve"
}
}
}
]
}
响应确认这些值已合并到一个数组中
{
"docs": [
{
"doc": {
"_index": "testindex1",
"_id": "1",
"_source": {
"user": {
"name": [
"Steve",
"John"
]
}
},
"_ingest": {
"timestamp": "2024-01-17T01:44:57.420220551Z"
}
}
}
]
}
如果字段包含相同的名称但路径不同,则需要重命名该字段。例如,以下 _simulate
调用会返回一个解析异常
POST _ingest/pipeline/dot-expander-pipeline/_simulate
{
"docs": [
{
"_index": "testindex1",
"_id": "1",
"_source": {
"user": "John",
"user.name": "Steve"
}
}
]
}
为避免解析异常,请首先使用 rename
处理器重命名字段
PUT /_ingest/pipeline/dot-expander-pipeline
{
"processors" : [
{
"rename" : {
"field" : "user",
"target_field" : "user.name"
}
},
{
"dot_expander": {
"field": "user.name"
}
}
]
}
现在您可以模拟该管道了
POST _ingest/pipeline/dot-expander-pipeline/_simulate
{
"docs": [
{
"_index": "testindex1",
"_id": "1",
"_source": {
"user": "John",
"user.name": "Steve"
}
}
]
}
响应确认这些字段已合并
{
"docs": [
{
"doc": {
"_index": "testindex1",
"_id": "1",
"_source": {
"user": {
"name": [
"John",
"Steve"
]
}
},
"_ingest": {
"timestamp": "2024-01-17T01:52:12.864432419Z"
}
}
}
]
}