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

huangapple go评论92阅读模式
英文:

Copy part of Elasticsearch field to new one

问题

有没有办法在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..."
}

基本上,“preview”是“description”的一部分复制,但只包括前几个字符。
我查看了Elasticsearch中的copy_to方法,但没有选项可以实现这一点。

英文:

Is there a way to copy part of a field into a new one in ES when indexing :

I'd like to have something like this :

  1. {
  2. "key": "123",
  3. "type": "novel",
  4. "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.",
  5. "preview": "Lorem Ipsum is simply dummy..."
  6. }

Basically "preview" is a copy of "description" but only a few first characters.
I checked the copy_to method in Elasticsearch but there are no option to do this.

答案1

得分: 1

这可以通过使用摄取管道和脚本处理器来实现。

POST _ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {
"description": "对描述字段进行摘要",
"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."
}
}
}
]
}

作为替代,您还可以选择摘取固定数量的词汇。易于更改。

英文:

This can be achieved with an ingest pipeline and a script processor

  1. POST _ingest/pipeline/_simulate
  2. {
  3. "pipeline": {
  4. "processors": [
  5. {
  6. "script": {
  7. "description": "Summarize the description field",
  8. "lang": "painless",
  9. "source": "ctx.preview = ctx.description.substring(0, 20) + '...'"
  10. }
  11. }
  12. ]
  13. },
  14. "docs": [
  15. {
  16. "_source": {
  17. "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."
  18. }
  19. }
  20. ]
  21. }

Response:

  1. {
  2. "docs" : [
  3. {
  4. "doc" : {
  5. "_source" : {
  6. "preview" : "Lorem Ipsum is simpl...",
  7. "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."
  8. }
  9. }
  10. }
  11. ]
  12. }

As an alternative instead of taking a fixed amount of characters, you could also take a fixed amount of words. Easy to change.

huangapple
  • 本文由 发表于 2023年6月19日 20:18:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定