# Please explain how to perform a bulk update with partials using UpdateMany

**URL:** https://discuss.elastic.co/t/please-explain-how-to-perform-a-bulk-update-with-partials-using-updatemany/183873
**Category:** Elasticsearch
**Created:** [June 2, 2019, 10:43pm UTC](https://discuss.elastic.co/t/please-explain-how-to-perform-a-bulk-update-with-partials-using-updatemany/183873 "2019-06-02T22:43:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mFriemann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mfriemann/32/47299_2.png) [@mFriemann](https://discuss.elastic.co/u/mFriemann)
#### Post date: [June 2, 2019, 10:43pm UTC](https://discuss.elastic.co/t/please-explain-how-to-perform-a-bulk-update-with-partials-using-updatemany/183873/1 "2019-06-02T22:43:01Z")

</div>

I am really surprised by the lack of documentation for the NEST client.

I am using NEST 6.6.0. I have an array of partial documents (anonymous objects) -- each includes an id field and the fields that I want to update.

Please show me an example of how to do this. I've seen an example where the you have to pass an IEnumerable\< T \> instead of IEnumerable\< TPartial \>, but that doesn't make sense. I have a bunch of partial documents that include the ids already. Is there a way to do this?

---

<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: [June 3, 2019, 12:20am UTC](https://discuss.elastic.co/t/please-explain-how-to-perform-a-bulk-update-with-partials-using-updatemany/183873/2 "2019-06-03T00:20:12Z")

</div>

The `IEnumerable<T>` passed to `UpdateMany<T>` can be a collection of partial objects, you just need to be aware that index name and type name would be inferred from `T` in this case, unless explicitly provided.

I think you want something like the following

```auto
private static void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionSettings(pool);
    var client = new ElasticClient(settings);

	var partials = new object[] 
	{
		new { id = 1, title = "foo" },
		new { id = 2, body = "bar" },
		new { id = 3, title = "baz" },
	};
	
	var bulkResponse = client.Bulk(b => b
		.Index("index_name") // explicitly provide index name
		.Type("doc_type") // explicitly provide doc type
		.UpdateMany(partials, (bu, d) => bu.Doc(d))
	);
}

```

This generates the following request

```json
POST http://localhost:9200/index_name/doc_type/_bulk
{"update":{"_id":"1"}}
{"doc":{"id":1,"title":"foo"}}
{"update":{"_id":"2"}}
{"doc":{"id":2,"body":"bar"}}
{"update":{"_id":"3"}}
{"doc":{"id":3,"title":"baz"}}

```

---

<div class="post-metadata">

### Author: ![mFriemann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mfriemann/32/47299_2.png) [@mFriemann](https://discuss.elastic.co/u/mFriemann)
#### Post date: [June 3, 2019, 3:43pm UTC](https://discuss.elastic.co/t/please-explain-how-to-perform-a-bulk-update-with-partials-using-updatemany/183873/3 "2019-06-03T15:43:19Z")

</div>

> [@forloop](#):
>
> POST [http://localhost:9200/index\_name/doc\_type/\_bulk](http://localhost:9200/index_name/doc_type/_bulk) {"update":{"\_id":"1"}} {"doc":{"id":1,"title":"foo"}} {"update":{"\_id":"2"}} {"doc":{"id":2,"body":"bar"}} {"update":{"\_id":"3"}} {"doc":{"id":3,"title":"baz"}}

That mostly works for me. Is there a way to somehow specify a property in the anonymous object to be an id so that I don't have to use the property name "id"?

---

<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: [June 4, 2019, 1:20am UTC](https://discuss.elastic.co/t/please-explain-how-to-perform-a-bulk-update-with-partials-using-updatemany/183873/4 "2019-06-04T01:20:41Z")

</div>

> [@mFriemann](#):
>
> Is there a way to somehow specify a property in the anonymous object to be an id so that I don't have to use the property name "id"?

Not for an anonymous type. The `id` property is picked up by NEST conventions, but if you wanted to have another property inferred to be the id of the document, you would need to create a POCO for it

```auto
[ElasticsearchType(IdProperty = nameof(MyId))]
public class PartialDoc
{
	public int MyId { get; set; }

	public string Title { get; set; }
	
	public string Body { get; set; }
}

var partials = new PartialDoc[]
{
	new PartialDoc{ MyId = 1, Title = "foo" },
	new PartialDoc{ MyId = 2, Body = "bar" },
	new PartialDoc{ MyId = 3, Title = "baz" },
};

var bulkResponse = client.Bulk(b => b
	.Index("index_name") // explicitly provide index name
	.Type("doc_type") // explicitly provide doc type
	.UpdateMany(partials, (bu, d) => bu.Doc(d))
);

```

which would yield

```json
POST http://localhost:9200/index_name/doc_type/_bulk?pretty=true 
{"update":{"_id":"1"}}
{"doc":{"myId":1,"title":"foo"}}
{"update":{"_id":"2"}}
{"doc":{"myId":2,"body":"bar"}}
{"update":{"_id":"3"}}
{"doc":{"myId":3,"title":"baz"}}

```

You can also [configure the property to use for the document ID on `ConnectionSettings`](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/ids-inference.html#_inferring_id_from_a_type).

---

<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: [July 2, 2019, 1:20am UTC](https://discuss.elastic.co/t/please-explain-how-to-perform-a-bulk-update-with-partials-using-updatemany/183873/5 "2019-07-02T01:20:44Z")

</div>

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