# NEST and Bulk operations of parital Updates (specifically UpdateMany)

**URL:** https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947
**Category:** Elasticsearch
**Created:** [July 20, 2018, 5:00pm UTC](https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947 "2018-07-20T17:00:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![cscorley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cscorley/32/33592_2.png) [@cscorley](https://discuss.elastic.co/u/cscorley)
#### Post date: [July 20, 2018, 5:00pm UTC](https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947/1 "2018-07-20T17:00:46Z")

</div>

The current implementation of [UpdateMany\<T, TPartialDocument\>](https://github.com/elastic/elasticsearch-net/blob/3e58310d6c29a35d5568923480f86856bfb9675b/src/Nest/Document/Multiple/Bulk/BulkRequest.cs#L93) operates on objects of type T instead of type TPartialDocument. This seems wrong to me, following the implementation of [Update\<T, TPartialDocument\>](https://github.com/elastic/elasticsearch-net/blob/3e58310d6c29a35d5568923480f86856bfb9675b/src/Nest/Document/Single/Update/UpdateRequest.cs#L68), which operates on objects of TPartialDocument.

Am I misunderstanding how to use the UpdateMany to do a bulk partial update or is this just an API bug? I see it is possible to do bulk partial updates by creating [my own BulkRequests](https://discuss.elastic.co/t/bulk-partial-update-using-nest/29124), but I'd like to use the Fluent interface where possible.

Thanks!

---

<div class="post-metadata">

### Author: ![Martijn\_Laarman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_laarman/32/4410_2.png) [@Martijn\_Laarman](https://discuss.elastic.co/u/Martijn_Laarman)
#### Post date: [July 24, 2018, 8:42am UTC](https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947/2 "2018-07-24T08:42:19Z")

</div>

Hey @cscorley I do not think this is an API bug.

The update happens on documents of type `T` so `UpdateMany` iterates over a collection of `T` and uses the provided map selector to create instances of `TPartialDocument` which are than fed to the update descriptor as `Doc(TPartialDocument doc)` which sets the partial update to perform.

---

<div class="post-metadata">

### Author: ![Martijn\_Laarman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_laarman/32/4410_2.png) [@Martijn\_Laarman](https://discuss.elastic.co/u/Martijn_Laarman)
#### Post date: [July 24, 2018, 8:44am UTC](https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947/3 "2018-07-24T08:44:34Z")

</div>

I spoke to soon that is how its suppose to operate but it doesn't, will create a PR for this.

Thank you for raising this!

---

<div class="post-metadata">

### Author: ![cscorley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cscorley/32/33592_2.png) [@cscorley](https://discuss.elastic.co/u/cscorley)
#### Post date: [July 24, 2018, 3:01pm UTC](https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947/4 "2018-07-24T15:01:38Z")

</div>

Excellent, thank you!

---

<div class="post-metadata">

### Author: ![Martijn\_Laarman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_laarman/32/4410_2.png) [@Martijn\_Laarman](https://discuss.elastic.co/u/Martijn_Laarman)
#### Post date: [July 25, 2018, 8:00am UTC](https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947/5 "2018-07-25T08:00:58Z")

</div>

I shall pivot once more, while looking at fixing this today I am convinced the current helper methods are correct.

```auto
/// <summary>
/// Updatemany, convenience method to pass many objects at once to do multiple updates.
/// </summary>
/// <param name="objects">the objects to update</param>
/// <param name="bulkUpdateSelector">An func called on each object to describe the individual update operation</param>
public BulkDescriptor UpdateMany<T, TPartialDocument>(IEnumerable<T> @objects, Func<BulkUpdateDescriptor<T, TPartialDocument>, T, IBulkUpdateOperation<T, TPartialDocument>> bulkUpdateSelector)
	where T : class
	where TPartialDocument : class =>
		Assign(a => @objects.ForEach(o => AddOperation(
			bulkUpdateSelector.InvokeOrDefault(
				new BulkUpdateDescriptor<T, TPartialDocument>().IdFrom(o)
				, o
			)
		)
	)
);

```

My initial response while looking at this was that `o` of type `T` as an argument should be of type `TPartialDocument` however it should not.

The helper makes no assumption on what kind of bulk update operation you want to compose inside `bulkUpdateSelector` this includes mapping from `T` to `TPartialDocument` if you want to specify the partial update via `.Doc()`.

I hope this makes sense!

---

<div class="post-metadata">

### Author: ![cscorley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cscorley/32/33592_2.png) [@cscorley](https://discuss.elastic.co/u/cscorley)
#### Post date: [July 25, 2018, 5:41pm UTC](https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947/6 "2018-07-25T17:41:28Z")

</div>

It still does not make sense. Why would I need to provide `IEnumerable<T>` (@objects) to accomplish a partial update with TPartialDocument in the bulkUpdateSelector? Building a TPartialDocument isn't an issue here, it's getting all those T just to satisfy the parameter requirements.

Does this mean I need to retrieve the original source documents of T myself? That seems to defeat the purpose of a quick partial update, and isn't something that's required in the non-Bulk operation so long as I know what document I'm updating:

```
var documentPath = new DocumentPath<Model>(Id.From(partialDocument));
var updateResponse = await Client.UpdateAsync<Model, PartialUpdate>(documentPath.Index(IndexName), x => x.Doc(partialDocument)));

```

Is this just to have a default operation for getting ids? I don't think it makes sense to create a bunch of T in order to extract an Id from -- I'd expect that Id to already be part of the TPartialDocument.

I think this method is just fine for doing T-\>TPartialDocument conversions, I'd just rather have a different method that only operated on TPartialDocument.

---

<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: [August 22, 2018, 5:41pm UTC](https://discuss.elastic.co/t/nest-and-bulk-operations-of-parital-updates-specifically-updatemany/140947/7 "2018-08-22T17:41:28Z")

</div>

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