简体   繁体   English

ElasticSearch NEST 批量插入

[英]ElasticSearch NEST bulk upsert

I conducted the following piece of code which performs partial update to the Content field in existing documents:我执行了以下代码,对现有文档中的Content字段执行部分更新:

var partials = new object[]
            {
                new{
                Id = 1337,
                Content = "test"
            }
            };

            var response = await _elastic.BulkAsync(b => b
                .Index(indexName)
                                         .Index(indexName)
                                         .UpdateMany(partials, (bu, d) => bu.Doc(d))
                                         );

However, I want to upsert this document if it does not exist and I'm not sure how.但是,如果它不存在并且我不确定如何插入此文档,我想更新它。 I tried to change bu.Doc to bu.Upsert however it expects a script.我试图将bu.Doc更改为bu.Upsert但它需要一个脚本。

Doc() and Upsert() work the same as described in the elastic "Update API" documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html Doc()Upsert()的工作方式与弹性“更新 API”文档中的描述相同: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html


With Doc() you specify a partial document to update an existing document.使用Doc()您可以指定部分文档来更新现有文档。 This is not an upsert, only an update这不是更新,只是更新


With Upsert() the documentation states:使用Upsert()文档状态:

If the document does not already exist, the contents of the upsert element are inserted as a new document.如果文档不存在,则将 upsert 元素的内容作为新文档插入。 If the document exists, the script is executed如果文档存在,则执行脚本

So the upsert part specifies what happens if the document does not exist.所以upsert部分指定了如果文档不存在会发生什么。 You need to add a script which is executed against the existing document.您需要添加一个针对现有文档执行的script


There is also the doc_as_upsert field in conjunction with doc .还有与doc一起使用的doc_as_upsert字段。 If set to true, then the document in doc is inserted if it does not exist, else it is updated.如果设置为 true,则doc中的文档如果不存在则插入,否则更新。 You can set it with bu.DocAsUpsert()您可以使用bu.DocAsUpsert()进行设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM