【问题标题】:How to insert a scripted field using igestion pipeline如何使用 gestion 管道插入脚本字段
【发布时间】:2020-03-11 05:38:31
【问题描述】:

所以我的文档中有两个字段

{
    emails: ["", "", ""]
    name: "",
 }

一旦文档被索引,我希望有一个名为 uid 的新字段,它将只包含所有电子邮件的串联字符串和每个文档的名称。

我可以在我的索引 _search 端点上使用这个 GET 请求来获取类似的脚本字段

{
 "script_fields": {
"combined": {
    "script": {
      "lang": "painless",
      "source": "def result=''; for (String email: doc['emails.keyword']) { result = result + email;} return doc['name'].value + result;"
    }
}
  }
 }

如果我想用我的文档索引相同的脚本字段,我想知道我的摄取管道 PUT 请求正文应该是什么样子?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    假设我有以下示例索引和示例文档。

    样本来源索引

    为了便于理解,我创建了以下映射。

    PUT my_source_index
    {
      "mappings": {
        "properties": {
          "email":{
            "type":"text"
          },
          "name":{
            "type": "text"
          }
        }
      }
    }
    

    示例文档:

    POST my_source_index/_doc/1
    {
      "email": ["john@gmail.com","doe@outlook.com"],
      "name": "johndoe"
    }
    

    只需按照以下步骤操作

    第 1 步:创建摄取管道

    PUT _ingest/pipeline/my-pipeline-concat
    {
      "description" : "describe pipeline",
      "processors" : [
        {
          "join": {
            "field": "email",
            "target_field": "temp_uuid",
            "separator": "-"
          }
        },
        {
          "set": {
            "field": "uuid",
            "value": "{{name}}-{{temp_uuid}}"
          }
        },
        {
          "remove":{
            "field": "temp_uuid"
          }
        }
      ]
    }
    

    请注意,我使用了Ingest API,其中我使用了三个processors,而creating the above pipeline 将按顺序执行:

    • 第一个处理器是Join Processor,它连接所有电子邮件ID并创建temp_uuid

    • 第二个处理器是Set Processor,我将nametemp_uuid 结合使用。

    • 在第三步中,我将使用 Remove Processor 删除 temp_uuid

    请注意,我使用- 作为所有值之间的分隔符。你可以随意使用任何你想要的东西。

    第 2 步:创建目标索引:

    PUT my_dest_index
    {
      "mappings": {
        "properties": {
          "email":{
            "type":"text"
          },
          "name":{
            "type": "text"
          },
          "uuid":{                  <--- Do not forget to add this
            "type": "text"
          }
        }
      }
    }
    

    第 3 步:应用重新索引 API:

    POST _reindex
    {
      "source": {
        "index": "my_source_index"
      },
      "dest": {
        "index": "my_dest_index",
        "pipeline": "my-pipeline-concat"       <--- Make sure you add pipeline here
      } 
    }
    

    注意我在使用 Reindex API 时是如何提到管道的

    第 4 步:验证目标索引:

    {
      "took" : 0,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "my_dest_index",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "name" : "johndoe",
              "uuid" : "johndoe-john@gmail.com-doe@outlook.com",   <--- Note this
              "email" : [
                "john@gmail.com",
                "doe@outlook.com"
              ]
            }
          }
        ]
      }
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      相关资源
      最近更新 更多