Elasticsearch update by query using NEST

Can anyone help how to pass paramter dynamically in update by query using nest c# .

var test= highLevelClient.UpdateByQuery(u => u
.Index("test")
.Type("doc")
.Query(q => q
.Term("_id", id)
)
.Script(("ctx._source.name=" + name.ToString() ))
.Conflicts(Conflicts.Proceed)
.Refresh(true)
);

Can anyone help me it is throwing me error

What error is it throwing? Can you include the test.DebugInformation?

It looks like you're want to update a single document, based on the term query on _id. For this, you can use a scripted update with the Update API (looks like you're using NEST 6.x?)

var id = 1;
var name = "updated_name";

var test = client.Update<object>(id, u => u
	.Index("test")
	.Type("doc")
	.Script(s => s
		.Source("ctx._source.name = params.name")
		.Params(p => p
			.Add("name", name)
		)
	)
	.Refresh(Refresh.True)
);

which is the following request

POST /test/doc/1/_update
{
  "script": {
    "params": {
      "name": "updated_name"
    },
    "source": "ctx._source.name = params.name"
  }
}

for an Update where you are simply overwriting a field value though, you can supply a partial document to do this. A partial document can be modelled with an anonymous type

var id = 1;
var name = "updated_name";

var test = client.Update<object>(id, u => u
	.Index("test")
	.Type("doc")
	.Doc(new 
	{
		name = name	
	})
	.Refresh(Refresh.True)
);

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.