设置处理器

编辑

设置一个字段并将其与指定的值关联。如果该字段已存在,则其值将被提供的值替换。

表 41. 设置选项

名称 必需 默认值 描述

field

-

要插入、更新或更新的字段。支持 模板片段

value

是*

-

要为字段设置的值。支持 模板片段。只能指定 valuecopy_from 中的一个。

copy_from

-

将复制到 field 的源字段,不能同时设置 value。支持的数据类型为 booleannumberarrayobjectstringdate 等。

override

true

如果 true,处理器将更新具有预先存在的非空值的字段。当设置为 false 时,将不会触及此类字段。

ignore_empty_value

false

如果 true 并且与评估为 null 或空字符串的 模板片段value 组合使用,则处理器会静默退出而不修改文档。类似地,如果与 copy_from 组合使用,如果该字段不存在或其值评估为 null 或空字符串,则它将静默退出。

media_type

application/json

编码 value 的媒体类型。仅当 value模板片段时才适用。必须是 application/jsontext/plainapplication/x-www-form-urlencoded 之一。

description

-

处理器的描述。用于描述处理器的目的或其配置。

if

-

有条件地执行处理器。请参阅 有条件地运行处理器

ignore_failure

false

忽略处理器的失败。请参阅 处理管道失败

on_failure

-

处理处理器的失败。请参阅 处理管道失败

tag

-

处理器的标识符。对于调试和度量很有用。

{
  "description" : "sets the value of count to 1",
  "set": {
    "field": "count",
    "value": 1
  }
}

此处理器也可用于将数据从一个字段复制到另一个字段。例如

resp = client.ingest.put_pipeline(
    id="set_os",
    description="sets the value of host.os.name from the field os",
    processors=[
        {
            "set": {
                "field": "host.os.name",
                "value": "{{{os}}}"
            }
        }
    ],
)
print(resp)

resp1 = client.ingest.simulate(
    id="set_os",
    docs=[
        {
            "_source": {
                "os": "Ubuntu"
            }
        }
    ],
)
print(resp1)
response = client.ingest.put_pipeline(
  id: 'set_os',
  body: {
    description: 'sets the value of host.os.name from the field os',
    processors: [
      {
        set: {
          field: 'host.os.name',
          value: '{{{os}}}'
        }
      }
    ]
  }
)
puts response

response = client.ingest.simulate(
  id: 'set_os',
  body: {
    docs: [
      {
        _source: {
          os: 'Ubuntu'
        }
      }
    ]
  }
)
puts response
const response = await client.ingest.putPipeline({
  id: "set_os",
  description: "sets the value of host.os.name from the field os",
  processors: [
    {
      set: {
        field: "host.os.name",
        value: "{{{os}}}",
      },
    },
  ],
});
console.log(response);

const response1 = await client.ingest.simulate({
  id: "set_os",
  docs: [
    {
      _source: {
        os: "Ubuntu",
      },
    },
  ],
});
console.log(response1);
PUT _ingest/pipeline/set_os
{
  "description": "sets the value of host.os.name from the field os",
  "processors": [
    {
      "set": {
        "field": "host.os.name",
        "value": "{{{os}}}"
      }
    }
  ]
}

POST _ingest/pipeline/set_os/_simulate
{
  "docs": [
    {
      "_source": {
        "os": "Ubuntu"
      }
    }
  ]
}

结果

{
  "docs" : [
    {
      "doc" : {
        "_index" : "_index",
        "_id" : "_id",
        "_version" : "-3",
        "_source" : {
          "host" : {
            "os" : {
              "name" : "Ubuntu"
            }
          },
          "os" : "Ubuntu"
        },
        "_ingest" : {
          "timestamp" : "2019-03-11T21:54:37.909224Z"
        }
      }
    }
  ]
}

此处理器还可以使用点表示法访问数组字段

resp = client.ingest.simulate(
    pipeline={
        "processors": [
            {
                "set": {
                    "field": "my_field",
                    "value": "{{{input_field.1}}}"
                }
            }
        ]
    },
    docs=[
        {
            "_index": "index",
            "_id": "id",
            "_source": {
                "input_field": [
                    "Ubuntu",
                    "Windows",
                    "Ventura"
                ]
            }
        }
    ],
)
print(resp)
response = client.ingest.simulate(
  body: {
    pipeline: {
      processors: [
        {
          set: {
            field: 'my_field',
            value: '{{{input_field.1}}}'
          }
        }
      ]
    },
    docs: [
      {
        _index: 'index',
        _id: 'id',
        _source: {
          input_field: [
            'Ubuntu',
            'Windows',
            'Ventura'
          ]
        }
      }
    ]
  }
)
puts response
const response = await client.ingest.simulate({
  pipeline: {
    processors: [
      {
        set: {
          field: "my_field",
          value: "{{{input_field.1}}}",
        },
      },
    ],
  },
  docs: [
    {
      _index: "index",
      _id: "id",
      _source: {
        input_field: ["Ubuntu", "Windows", "Ventura"],
      },
    },
  ],
});
console.log(response);
POST /_ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "set": {
          "field": "my_field",
          "value": "{{{input_field.1}}}"
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_id": "id",
      "_source": {
        "input_field": [
          "Ubuntu",
          "Windows",
          "Ventura"
        ]
      }
    }
  ]
}

结果

{
  "docs": [
    {
      "doc": {
        "_index": "index",
        "_id": "id",
        "_version": "-3",
        "_source": {
          "input_field": [
            "Ubuntu",
            "Windows",
            "Ventura"
          ],
          "my_field": "Windows"
        },
        "_ingest": {
          "timestamp": "2023-05-05T16:04:16.456475214Z"
        }
      }
    }
  ]
}

可以使用 copy_from 将字段的内容(包括数组和对象等复杂值)复制到另一个字段

resp = client.ingest.put_pipeline(
    id="set_bar",
    description="sets the value of bar from the field foo",
    processors=[
        {
            "set": {
                "field": "bar",
                "copy_from": "foo"
            }
        }
    ],
)
print(resp)

resp1 = client.ingest.simulate(
    id="set_bar",
    docs=[
        {
            "_source": {
                "foo": [
                    "foo1",
                    "foo2"
                ]
            }
        }
    ],
)
print(resp1)
response = client.ingest.put_pipeline(
  id: 'set_bar',
  body: {
    description: 'sets the value of bar from the field foo',
    processors: [
      {
        set: {
          field: 'bar',
          copy_from: 'foo'
        }
      }
    ]
  }
)
puts response

response = client.ingest.simulate(
  id: 'set_bar',
  body: {
    docs: [
      {
        _source: {
          foo: [
            'foo1',
            'foo2'
          ]
        }
      }
    ]
  }
)
puts response
const response = await client.ingest.putPipeline({
  id: "set_bar",
  description: "sets the value of bar from the field foo",
  processors: [
    {
      set: {
        field: "bar",
        copy_from: "foo",
      },
    },
  ],
});
console.log(response);

const response1 = await client.ingest.simulate({
  id: "set_bar",
  docs: [
    {
      _source: {
        foo: ["foo1", "foo2"],
      },
    },
  ],
});
console.log(response1);
PUT _ingest/pipeline/set_bar
{
  "description": "sets the value of bar from the field foo",
  "processors": [
    {
      "set": {
        "field": "bar",
        "copy_from": "foo"
      }
    }
  ]
}

POST _ingest/pipeline/set_bar/_simulate
{
  "docs": [
    {
      "_source": {
        "foo": ["foo1", "foo2"]
      }
    }
  ]
}

结果

{
  "docs" : [
    {
      "doc" : {
        "_index" : "_index",
        "_id" : "_id",
        "_version" : "-3",
        "_source" : {
          "bar": ["foo1", "foo2"],
          "foo": ["foo1", "foo2"]
        },
        "_ingest" : {
          "timestamp" : "2020-09-30T12:55:17.742795Z"
        }
      }
    }
  ]
}