NEST API UpdateByQuery

Could someone KINDLY let us know what the syntax is using NEST when we want to do a partial update using the UpdateByQuery.

We know you can only use scripts, but we have not a clue how to do this.

Your assistance will be greatly appreciated.

Update By Query API is supported in NEST. Here's an example adapted from the integration tests

private static void Main()
{
	var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
		.DefaultMappingFor<Test>(m => m
			.IndexName("tests")
			.TypeName("test")
		);

    var client = new ElasticClient(settings);
	var index = IndexName.From<Test>();
	
	if (client.IndexExists(index).Exists)
		client.DeleteIndex(index);

	client.CreateIndex(index, c => c
		.Mappings(m => m
			.Map<Test>(map => map
				.Properties(props => props
					.Text(s => s.Name(p => p.Text))
					.Keyword(s => s.Name(p => p.Flag))
				)
			)
		)
	);

	client.Bulk(b => b
		.IndexMany(new[] {
			new Test { Text = "words words", Flag = "bar" },
			new Test { Text = "words words", Flag = "foo" }
		})
		.Refresh(Refresh.WaitFor)
	);
	
	client.Count<Test>(s => s
		.Query(q => q
			.Match(m => m
				.Field(p => p.Flag)
				.Query("foo")
			)
		)
	);
	
	client.UpdateByQuery<Test>(u => u
		.Query(q => q
			.Term(f => f.Flag, "bar")
		)
		.Script("ctx._source.flag = 'foo'")
		.Conflicts(Conflicts.Proceed)
		.Refresh(true)
	);

	client.Count<Test>(s => s
		.Query(q => q
			.Match(m => m
				.Field(p => p.Flag)
				.Query("foo")
			)
		)
	);
}

public class Test
{
	public string Text { get; set; }
	public string Flag { get; set; }
}

Observe that the count from the first Count API call is 1, and on the second Count API call after the Update By Query API call, it's 2.

Many Thanks Russ.

As usual you seem to be the one who comes to the rescue.

Cheers.

Sorry to bother you again. But as I have no experience with scripts, could you kindly provide the syntax if there are multiple properties to update.

Many thanks

The example is using the Painless scripting language, which is a sandboxed language similar in syntax to Groovy/Java. The best place to start is probably the Painless scripting examples.

Many thanks Russ.

You're a star.

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