[英]How to update nested objects using Nest Elasticsearch?
I have product index which for simplicity has two fields Id and ProductAttributes as nested object defined as following: 我有产品索引,为简单起见,它具有两个字段Id和ProductAttributes作为嵌套对象,定义如下:
public class ProductType
{
public Guid Id { get; set; }
public List<ProductAttribute> ProductAttributes { get; set; }
}
public class ProductAttribute
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
And the following mapping: 以及以下映射:
elasticClient.CreateIndex("product", i => i
.Settings(s => s
.NumberOfShards(2)
.NumberOfReplicas(0)
)
.Mappings(m => m
.Map<ProductType>(map => map
.AutoMap()
.Properties(p => p
.Nested<ProductAttribute>(n => n
.Name(c => c.ProductAttributes)
.AutoMap()
.Properties(nc => nc
.Keyword(t => t
.Name(nn => nn.Name)
)
.Keyword(t => t
.Name(nn => nn.Value)
)
)
)
Now I am trying to update name field inside nested object and I have tried implementing that using scripted update as following: 现在,我尝试更新嵌套对象中的名称字段,并尝试使用脚本更新实现该操作,如下所示:
var scriptParams = new Dictionary<string, object>
{
{"name", "new name"}
};
var result = elasticClient.UpdateByQuery<ProductType>(u => u
.Script(sn => sn
.Inline(
$"ctx._source.productAttributes= params.name;"
)
.Params(scriptParams)
)
.Conflicts(Conflicts.Proceed)
.Refresh(true)
);
But using the above query I couldn't update the nested object, could you please tell how can I update nested object using _update_by_query api using nest ES? 但是使用上面的查询,我无法更新嵌套对象,请问如何使用嵌套ES使用_update_by_query api更新嵌套对象?
Look likes problem lies here $"ctx._source.productAttributes= params.name;" 好像问题出在这里$“ ctx._source.productAttributes = params.name;”
productAttributes is an object (nest object) and params.name is a value (string) and that what ES complaints in query response. productAttributes是一个对象(嵌套对象),params.name是一个值(字符串),是ES在查询响应中抱怨的内容。
Not sure what exactly you wants to do, if your requirement is to update the name for all productAttributes elements you can try this: 不确定要做什么,如果您需要更新所有productAttributes元素的名称,则可以尝试以下操作:
var result = elasticClient.UpdateByQuery<ProductType>(u => u
.Index("product")
.Script(ss => ss.Source("for(int i=0; i<ctx._source.productAttributes.size(); i++){HashMap myKV = ctx._source.productAttributes.get(i);myKV.put(params.item.fieldName, params.item.fieldValue);}")
.Params(d => d.Add("item", new Dictionary<string, object>()
{
{"fieldName", "name" },
{"fieldValue", "new name" }
})).Lang("painless")));
Finally I have found how to update name property for only specific nested objects depending on their id as following: 最后,我发现了如何仅根据特定的嵌套对象的ID更新其name属性,如下所示:
var result = elasticClient.UpdateByQuery<ProductType>(u => u
.Query(q => q
.Nested(n => n
.Path(Infer.Field<ProductType>(ff => ff.ProductAttributes))
.Query(nq => nq
.Term(Infer.Field<ProductType>(ff => ff.ProductAttributes.First().Id), productAttributeId)
)
)
)
.Script(ss => ss.Inline("if (ctx._source.productAttributes != null){for (item in ctx._source.productAttributes){if (item.id == params.id) {item.name = params.name;}}}")
.Params(new Dictionary<string, object>()
{
{"id", productAttributeId},
{"name", productAttributeName}
}).Lang("painless")
)
.Conflicts(Conflicts.Proceed)
.Refresh(true)
);
And here the generated query : 这是生成的查询:
POST product/producttype/_update_by_query?conflicts=proceed&refresh=true
{
"query": {
"bool": {
"must": [
{
"nested": {
"query": {
"term": {
"productAttributes.id": {
"value": "563243f0-8fbb-4adf-a78d-1339e5971a43"
}
}
},
"path": "productAttributes"
}
}
]
}
},
"script": {
"params": {
"id":"563243f0-8fbb-4adf-a78d-1339e5971a43",
"name": "CPU"
},
"lang": "painless",
"inline": "if (ctx._source.productAttributes != null){for (item in ctx._source.productAttributes){if (item.id == params.id) {item.name = params.name;}}}"
}
}
So what does the above query do: 那么上面的查询是做什么的:
It first searches for products which have productAttribute with 563243f0-8fbb-4adf-a78d-1339e5971a43 id and then it iterates over productAttributes nested objects to update only attributes with that id and then re-indexes the document again. 它首先搜索具有productAttribute带有563243f0-8fbb-4adf-a78d-1339e5971a43 id的产品,然后遍历productAttributes嵌套对象以仅更新具有该id的属性,然后再次为文档重新索引。
I hope my answer help others facing problems updating nested objects in Elasticsearch. 我希望我的回答能帮助其他人在更新Elasticsearch中的嵌套对象时遇到问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.