Jonas Olesen
Jonas Olesen

Reputation: 568

Nest updatebyquery how can i use a poco instead of writing the script

We have an elastic index with about 2 million documents, i need a way to update a list of them based on a single unique field. What i've tried is using the build in updatebyquery function in Nest, but what documentation i've found, requires me to write the update script by hand in my code like this:

foreach (var document in batch)
{
    var script = "ctx._source.brand_no = params.brandNo;" +
                 "ctx._source.order_no = params.orderNo";

    var paramDict = new Dictionary<string, object>(){
        {"brandNo",document.BrandNo},
        {"orderNo",document.OrderNo}
    };

    await _clientProvider.ElasticClient
                         .UpdateByQueryAsync<Orderline>(x =>
                             x.Index(indexName).Query(q =>
                                 q.Term(t =>
                                     t.Field(f =>
                                         f.OrderLineID).Value(document.OrderLineID))).Script(s =>
                                 s.Source(script).Params(paramDict))));
}

My problem is that my Orderline class is pretty big, and this hardcoded script is very error prone and hard to maintain.

Another issue is that this update takes a long time for 2 million rows.

Upvotes: 1

Views: 1024

Answers (1)

Russ Cam
Russ Cam

Reputation: 125528

My problem is that my Orderline class is pretty big, and this hardcoded script is very error prone and hard to maintain.

Update by query API only supports updating with scripts so you could implement a small component that generates the script from a given POCO instance, to reduce the likelihood of errors within the script.

Another issue is that this update takes a long time for 2 million rows.

Would indexing documents into a new index, and using aliases to point at a newer version index work in your case? Updating/Deleting millions of documents in an existing index is a relatively expensive operation.

Upvotes: 2

Related Questions