简体   繁体   English

如何使用NEST从Elasticsearch文档中删除字段值?

[英]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.

相关问题 ElasticSearch NEST删除所有文件 - ElasticSearch NEST Delete all document 我如何使用Nest API 5在Elasticsearch中进行重新索引以从Elastic Search 1.4迁移到ElasticSearch 5 - How can i do reindexing in elasticsearch to migrate from elastic search 1.4 to elasticsearch 5 using Nest api 5 如何为ElasticSearch 5.x使用NEST流畅的映射指定索引/字段分析器? - How can I specify index / field analyzers using NEST fluent mapping for ElasticSearch 5.x? 如何使用C#使用Nest Elasticsearch编写查询? - How can i write a query by using Nest Elasticsearch by C# ? 如何通过使用Nest从Json ElasticSearch获取logType和级别? - How can I get logType and level from Json ElasticSearch by using Nest? 使用NEST的elasticsearch:文档版本控制 - elasticsearch using NEST: document versioning 如何使用 NEST 更新 ElasticSearch 索引中的现有文档? - How do I update an existing document inside ElasticSearch index using NEST? 如何使用NEST删除特定的河流? - How can I delete a specific river using NEST? C#为Elasticsearch嵌套:如何将更新弹性搜索文档特定字段的查询转换为Nest? - c# nest for elasticsearch: how to convert query that updates particular field of elastic search document to nest? 如何使用 elasticsearch Nest 客户端通过 _id 查询特定文档 - How to query for a specific document by _id using the elasticsearch Nest client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM