英文:
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 :
{
"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..."
}
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
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."
}
}
]
}
Response:
{
"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."
}
}
}
]
}
As an alternative instead of taking a fixed amount of characters, you could also take a fixed amount of words. Easy to change.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论