使用.NET更新Elasticsearch指定字段 - 只更新Sku.Price
要使用.NET实现Elasticsearch的指定字段更新,您可以使用Elasticsearch.Net和Nest库来执行以下步骤:
- 创建一个Elasticsearch客户端对象:
var settings = new ConnectionSettings(new Uri('http://localhost:9200'));
var client = new ElasticClient(settings);
- 使用Update API指定要更新的文档和字段:
var updateResponse = client.Update<Document, object>(new Document
{
Id = '1',
Sku = new Sku
{
Price = 9.99
}
}, u => u.Index('myindex').Doc(new { Sku = new { Price = 9.99 } }).DocAsUpsert());
在此示例中,我们使用Update API更新名为'myindex'的索引中ID为'1'的文档的Sku.Price字段。我们使用Doc方法指定要更新的字段,并使用DocAsUpsert方法指定如果文档不存在,则将其插入。
完整的示例代码如下:
using Elasticsearch.Net;
using Nest;
using System;
namespace ElasticsearchExample
{
class Program
{
static void Main(string[] args)
{
var settings = new ConnectionSettings(new Uri('http://localhost:9200'));
var client = new ElasticClient(settings);
var updateResponse = client.Update<Document, object>(new Document
{
Id = '1',
Sku = new Sku
{
Price = 9.99
}
}, u => u.Index('myindex').Doc(new { Sku = new { Price = 9.99 } }).DocAsUpsert());
Console.WriteLine(updateResponse.Result);
}
}
public class Document
{
public string Id { get; set; }
public Sku Sku { get; set; }
}
public class Sku
{
public double Price { get; set; }
}
}
原文地址: https://www.cveoy.top/t/topic/jl2M 著作权归作者所有。请勿转载和采集!