elasticsearch - 如何使用摘要管道插入脚本字段

标签 elasticsearch

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

{
    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请求主体应该是什么样子,如果我想用我的文档索引相同的脚本字段?

最佳答案

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

样本来源索引

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

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:套用Reindex 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"
              ]
            }
          }
        ]
      }
    }
    

    希望这可以帮助!

    关于elasticsearch - 如何使用摘要管道插入脚本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60629839/

    相关文章:

    Action 部分的elasticsearch批量索引和冗余数据

    python - 带有 Elasticsearch 后端的 Django Haystack

    java - 使用 Elastic Search 的高级 REST JAVA 客户端异步放置映射 - 已弃用的错误

    elasticsearch - 加载自定义模块时 ElasticSearch Controller 出现 "No such index"错误

    search - ElasticSearch——根据字段值提升相关性

    pdf - Elasticsearch-提取PDF内容并使用base64进行编码

    elasticsearch - Postman AWS S3 快照请求 Elasticsearch

    elasticsearch - 如何忽略 Elasticsearch 中未映射的字段?

    elasticsearch - 将 String 转换为 JSON,以便可以在 Kibana/Elasticsearch 中搜索

    elasticsearch - 将 Spark Dataframe 保存到 Elasticsearch - 无法处理类型异常