[英]How can I delete a field value from an Elasticsearch document using NEST?
I am using Nest to insert/update Documents in Elasticsearch. 我正在使用Nest在Elasticsearch中插入/更新文档。 Here's a sample POCO class that I use to map the Document...
这是我用来映射文档的示例POCO类。
public class MyClass
{
public string Id { get; set; }
public decimal? MyField { get; set; }
}
This works as expected...When I add the Document, if the nullable field MyField
has a value, it is returned in the JSON. 这按预期工作...当我添加文档时,如果可为空的字段
MyField
具有值,则会在JSON中返回它。 If the nullable field doesn't have a value, it's not returned in the _source
JSON because null values aren't stored. 如果可为空的字段没有值,则不会在
_source
JSON中返回它,因为不会存储空值。
However, there might be times where I need to update a single document and remove the value from a single field. 但是,有时我需要更新单个文档并从单个字段中删除值。 That is, when I first insert the document,
MyField
has a value and is returned in the Elasticsearch JSON result. 也就是说,当我第一次插入文档时,
MyField
有一个值,并在Elasticsearch JSON结果中返回。 Then later, for whatever reason, I need to remove that value. 然后,无论出于何种原因,我都需要删除该值。
I am using partial document updates, and if possible, would prefer to keep it like that. 我正在使用部分文档更新,并且如果可能的话,希望保持这种状态。 (The full Document model I'm using will have 100+ fields, and my index will eventually have 100M+ records.) So, I'm looking for the most efficient way possible to partially update the documents.
(我正在使用的完整文档模型将具有100多个字段,而我的索引最终将具有100M +记录。)因此,我正在寻找最有效的方式来部分更新文档。
Thanks in advance! 提前致谢!
Think how would you do it just using api? 想想您将如何使用api来做到这一点? there are several ways.
有几种方法。 for example like this:
例如这样的:
// kibana version
POST my_index/_update_by_query
{
"query": {
"ids": {
"values": [
"12345"
]
}
},
"script": {
"source": "ctx._source.myField = null"
}
}
// nest version
var response = client.UpdateByQuery<CustomerDto>(u => u
.Query(q => q
.Ids(i => i.Values(12345))
)
.Script("ctx._source.flag = null")
//.Conflicts(Conflicts.Proceed)
.Refresh(true)
);
or this: 或这个:
// kibana version
POST my_index/my_type/12345/_update
{
"script": {
"source": "ctx._source.remove('myField')"
}
}
// nest version
var response = client.Update<CustomerDto>(
new UpdateDescriptor<CustomerDto, CustomerDto>("index_name", "type", 12345)
.Script(s => s.Source("ctx._source.remove('middleName')")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.