将Elasticsearch字段的一部分复制到新字段

pinkon5k  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(153)

索引时,是否有方法将字段的一部分复制到ES中的新字段中:
我想要这样的东西:

{
    "key": "123",
    "type": "novel",
    "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
    "preview": "Lorem Ipsum is simply dummy..."
}

基本上“预览”是“描述”的副本,但只有几个第一个字符。我检查了Elasticsearch中的copy_to方法,但没有这样做的选项。

9jyewag0

9jyewag01#

这可以通过摄取管道和script processor

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "description": "Summarize the description field",
          "lang": "painless",
          "source": "ctx.preview = ctx.description.substring(0, 20) + '...'"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
      }
    }
  ]
}

回复:

{
  "docs" : [
    {
      "doc" : {
        "_source" : {
          "preview" : "Lorem Ipsum is simpl...",
          "description" : "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
        }
      }
    }
  ]
}

作为替代方案,您也可以采用固定数量的单词,而不是采用固定数量的字符。很容易改变。

相关问题