Skip to content

Latest commit

 

History

History
53 lines (44 loc) · 1.32 KB

File metadata and controls

53 lines (44 loc) · 1.32 KB
endpoint update_by_query
lang dotnet
es_version 9.3
client Elastic.Clients.Elasticsearch==9.3.0

Elasticsearch 9.3 update_by_query endpoint (.NET example)

Use client.UpdateByQueryAsync() to update all documents matching a query using a script.

var response = await client.UpdateByQueryAsync("products", u => u
    .Query(q => q
        .Term(t => t.Field("category").Value("electronics"))
    )
    .Script(s => s
        .Source("ctx._source.price *= params.discount")
        .Params(p => p.Add("discount", 0.9))
    )
);
Console.WriteLine($"Updated {response.Updated} documents");

Handling conflicts

By default, version conflicts abort the operation. Set Conflicts to Proceed to skip conflicting documents and continue:

var response = await client.UpdateByQueryAsync("products", u => u
    .Conflicts(Conflicts.Proceed)
    .Query(q => q
        .Term(t => t.Field("in_stock").Value(true))
    )
    .Script(s => s.Source("ctx._source.in_stock = false"))
);

Limiting scope

Use MaxDocs to cap the number of updates, and ScrollSize to control batch size:

var response = await client.UpdateByQueryAsync("products", u => u
    .Query(q => q.MatchAll(new MatchAllQuery()))
    .Script(s => s.Source("ctx._source.rating += 0.1"))
    .MaxDocs(500)
    .ScrollSize(100)
);