# NEST API UpdateByQuery

**URL:** <https://discuss.elastic.co/t/nest-api-updatebyquery/128759>\
**Category:** Elasticsearch\
**Created:** [April 19, 2018, 5:53pm UTC](https://discuss.elastic.co/t/nest-api-updatebyquery/128759 "2018-04-19T17:53:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [April 19, 2018, 5:53pm UTC](https://discuss.elastic.co/t/nest-api-updatebyquery/128759/1 "2018-04-19T17:53:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [May 1, 2018, 5:39am UTC](https://discuss.elastic.co/t/nest-api-updatebyquery/128759/2 "2018-05-01T05:39:16Z")

</div>

[Update By Query API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html) is supported in NEST. Here's an example [adapted from the integration tests](https://github.com/elastic/elasticsearch-net/blob/master/src/Tests/Document/Multiple/UpdateByQuery/UpdateByQueryApiTests.cs)

```
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.

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [May 1, 2018, 12:29pm UTC](https://discuss.elastic.co/t/nest-api-updatebyquery/128759/3 "2018-05-01T12:29:46Z")

</div>

Many Thanks Russ.

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

Cheers.

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [May 1, 2018, 1:05pm UTC](https://discuss.elastic.co/t/nest-api-updatebyquery/128759/5 "2018-05-01T13:05:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [May 1, 2018, 1:08pm UTC](https://discuss.elastic.co/t/nest-api-updatebyquery/128759/6 "2018-05-01T13:08:43Z")

</div>

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.](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-examples.html)

---

<div class="post-metadata">

**Author:** ![han1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/han1/32/20141_2.png) [@han1](https://discuss.elastic.co/u/han1)\
**Post date:** [May 1, 2018, 6:27pm UTC](https://discuss.elastic.co/t/nest-api-updatebyquery/128759/7 "2018-05-01T18:27:01Z")

</div>

Many thanks Russ.

You're a star.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 29, 2018, 6:27pm UTC](https://discuss.elastic.co/t/nest-api-updatebyquery/128759/8 "2018-05-29T18:27:01Z")

</div>

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