elasticsearch - 在弹性搜索中更新嵌套对象
问题描述
我有以下索引映射
{
"mappings": {
"xxxxx": {
"properties": {
"ID": {
"type": "text"
},
"pairs": {
"type": "nested"
},
"xxxxx": {
"type": "text"
}
}
}
}
}
该pairs
字段是具有以下结构的对象数组
{
"id": "",
"Answer": "",
"Question": []
}
我想要做的是检索一个特定的嵌套对象并更新它。我已经尝试updateByQuery
使用部分文档/脚本的方法,但我无法更新
脚本
var theScript = {
"inline": "ctx._source.Answer = 'Elastic search update Test'"
}
client.updateByQuery({
index: 'sample',
_source: false,
body: {
query: {
bool: {
must: [
{
"match": {
"ID": '2rXdCf5OM9g1ebPNFdZNqW'
}
},
{
"nested": {
"path": "pairs",
"query": {
"match": {
"pairs.id": "c1vNGnnQLuk"
}
},
"inner_hits": {}
}
}
]
}
},
"script": theScript
}
},function(err, body){
if(err){
throw err;
}
console.log('body: ', body)
console.log('data: ', body.hits.hits)
})
部分文档
client.updateByQuery({
index: 'sample',
_source: false,
body: {
query: {
bool: {
must: [
{
"match": {
"ID": '2rXdCf5OM9g1ebPNFdZNqW'
}
},
{
"nested": {
"path": "pairs",
"query": {
"match": {
"pairs.id": "c1vNGnnQLuk"
}
},
"inner_hits": {}
}
}
]
}
},
"doc": {
"Answer": 'Elastic search update Test'
}
}
},function(err, body){
if(err){
throw err;
}
console.log('body: ', body)
console.log('data: ', body.hits.hits)
})
我收到以下错误:
部分更新
Error: [parsing_exception] Unknown key for a START_OBJECT in [doc]., with { line=1 & col=191 }
脚本
Error: [illegal_argument_exception] [sample][conversation][AWa-p9zBTJHq-_gvo-Af] didn't store _source
注意理想情况下,我希望为此使用部分文档更新,因为嵌套对象有点复杂,并且不可能编写内联脚本
弹性搜索版本 - 5.6
更新
做这样的事情
{
"script" : {
"inline": "ctx._source.pairs= params.pairs",
"lang": "painless",
"params" : {
"pairs" : [{...}, {...}, ...]
}
}
}
但这本质上意味着每次我更新时,我都在重写整个pairs
字段(即使我只更新数组中的一个对象)——这对我来说看起来并不理想还是很好?
解决方案
有一个页面包含有关嵌套类型管理的快速教程:如何管理 Elasticsearch 文档中的嵌套对象。它解释了如何添加、删除和编辑嵌套对象。
推荐阅读
- ios - 斯威夫特:UISlider轨道的一侧不圆
- sql - 查询之间的区别
- python - Python 升级图像(没有外部库帮助)
- c# - 通过菜单c#打开和关闭表单
- python - TkInter 错误:没有显示名称和 $DISPLAY 环境变量
- android - 如何从在线下载视频并将其存储在本地设备上,然后使用视频播放器在 Flutter 应用程序上播放视频?
- c# - 是否使用 System.Security.Cryptography.RSACng 来签名和验证数据线程安全?
- python - 将表列和值转换为嵌套 JSON
- angular - Angular - 可安装性“未检测到匹配的服务人员”
- angular - 动态地图点数据,doc.getElementById('id1') 以角度返回 null