设置处理器

编辑

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

表 41. 设置选项

名称 必填 默认值 描述

field

-

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

value

是*

-

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

copy_from

-

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

override

true

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

ignore_empty_value

false

如果true并且与value结合使用(value是计算结果为null或空字符串的模板片段),则处理器将静默退出而不修改文档。类似地,如果与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"
        }
      }
    }
  ]
}