ElasticsearchNet 如何更新指定字段?
您可以使用 Elasticsearch.Net 的 Update API 来更新指定字段。
以下是一个示例代码:
var client = new ElasticClient(settings);
var updateResponse = client.Update<MyDocument, object>(document.Id, u => u
.Index(indexName)
.Type(typeName)
.Doc(new { MyField = "new value" })
.DocAsUpsert()
);
这将更新具有指定 ID 的文档的 MyField 字段。.DocAsUpsert() 方法将确保如果文档不存在,则会创建一个新文档。
您也可以使用 Partial Update API 更新文档的一部分,如下所示:
var client = new ElasticClient(settings);
var updateResponse = client.Update<MyDocument, object>(document.Id, u => u
.Index(indexName)
.Type(typeName)
.Doc(new { MyField = "new value" })
.DocAsUpsert()
);
这将更新具有指定 ID 的文档的 MyField 字段,而不会覆盖其他字段。
请注意,您需要定义 MyDocument 类型,以便它与您的 Elasticsearch 映射相匹配
原文地址: https://www.cveoy.top/t/topic/cyQW 著作权归作者所有。请勿转载和采集!