# Best way to do a Bulk update with script

**URL:** https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017
**Category:** Elastic Search
**Created:** [October 17, 2024, 5:04pm UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017 "2024-10-17T17:04:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Matt\_Shallow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_shallow/32/138476_2.png) [@Matt\_Shallow](https://discuss.elastic.co/u/Matt_Shallow)
#### Post date: [October 17, 2024, 5:04pm UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017/1 "2024-10-17T17:04:41Z")

</div>

I have been following a bulk update example to do a bulk update on my index however I get the following error:

> Validation Failed: 1: can't provide both script and doc;

The below code is what is giving the error.

```auto
var bulkResponse = client.Bulk(b => b
    .Index("my_index")
    .UpdateMany(my_dictionary, (descriptor, dict) => descriptor
        .Id(dict.Key)
        .Script(s => s
            .Source("ctx._source.list.add(params.item)")
            .Params(p => p
                .Add("item", dict.Value)
            )
        )
    )
);

```

Would love some suggestions on how I can do a bulk update on multiple documents. The below works fine for single update on a document. I just need to apply it to bulk update.

```auto
var response = await _client.UpdateAsync<MyObject, MyObject>(
    index:"my_index",
    id:"my_id",
    u => u.Script(s => s
        .Source("ctx._source.list.add(params.item)")
        .Params(p => p
            .Add("item", myObject.Value))));

```

---

<div class="post-metadata">

### Author: ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)
#### Post date: [October 18, 2024, 9:29am UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017/2 "2024-10-18T09:29:28Z")

</div>

Hi @Matt_Shallow, Welcome to the Elastic Community.

1. May i know which version of Elastic you're using?
2. Also are you referring this [javascript client](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_bulk) ? It looks `UpdateMany` is not the part of it.

---

<div class="post-metadata">

### Author: ![Matt\_Shallow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_shallow/32/138476_2.png) [@Matt\_Shallow](https://discuss.elastic.co/u/Matt_Shallow)
#### Post date: [October 18, 2024, 9:41am UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017/3 "2024-10-18T09:41:37Z")

</div>

Hi @ashishtiwari1993 . I am using the latest version of Elastic. Doing some exploration work.

I am using the latest available .NET Elastic client library. Documentation seems pretty lax on Bulk Api elastic docs for .NET. Even entries deleted 🧐 See below link.

Essentially, what I am trying to do is bulk update multiple documents adding a new item to a list I have in each of these documents. I am iterating through a dictionary whose Key = DocumentID and Value is the item to be added to the documents list.

Bulk UpdateMany seems exactly what I need but I am getting that error. So I need to somehow restructure the Query which I need help with 🙂

> **[Indexing documents | Elasticsearch .NET Client \[8.15\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/indexing-documents.html)**

---

<div class="post-metadata">

### Author: ![flobernd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flobernd/32/124877_2.png) [@flobernd](https://discuss.elastic.co/u/flobernd)
#### Post date: [October 21, 2024, 7:20am UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017/4 "2024-10-21T07:20:44Z")

</div>

Hi @Matt_Shallow,

you are hitting a very specific limitation of the `UpdateMany` API here.

The `_bulk` endpoint accepts either `script` or `doc`, but not both. In the client, we internally check which variant the user has provided. For this, we compare the value of `script` or `doc` to `null`.

In your specific case, `TDocument` is of type `KeyValuePair<,>` which is a value type. The `null` check yields incorrect results as value types are never `null`.

As a workaround, you could convert your `KeyValuePair<,>` to a reference type. This will cause a few extra allocations, but is currently the only way to make the bulk API work in the way you want:

```csharp
var bulkResponse = await client.BulkAsync(b => b
	.Index("persons")
	.UpdateMany(my_dictionary.Select(x => new { x.Key, x.Value }), (descriptor, dict) => descriptor
		.Id(dict.Key)
		.Script(s => s
			.Source("ctx._source.list.add(params.item)")
			.Params(p => p
				.Add("item", dict.Value)
			)
		)
	)
);

```

---

<div class="post-metadata">

### Author: ![Matt\_Shallow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_shallow/32/138476_2.png) [@Matt\_Shallow](https://discuss.elastic.co/u/Matt_Shallow)
#### Post date: [October 21, 2024, 4:19pm UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017/5 "2024-10-21T16:19:33Z")

</div>

Thanks @flobernd for taking a look and solution. Let me give that a whirl and get back to you.

I will be doing some performance checks on different ways to store and then query the data in elastic so possibly my current approach might not be the way forward.

Thanks.

---

<div class="post-metadata">

### Author: ![Matt\_Shallow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_shallow/32/138476_2.png) [@Matt\_Shallow](https://discuss.elastic.co/u/Matt_Shallow)
#### Post date: [October 22, 2024, 12:14pm UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017/6 "2024-10-22T12:14:21Z")

</div>

@flobernd This worked great and not really any noticeable performance issues. I appreciate you explaining clearly why I was getting that specific error and for providing a solution.

Thanks\<  
Matt

---

<div class="post-metadata">

### Author: ![flobernd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flobernd/32/124877_2.png) [@flobernd](https://discuss.elastic.co/u/flobernd)
#### Post date: [October 22, 2024, 6:06pm UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017/7 "2024-10-22T18:06:49Z")

</div>

Thanks for updating the issue! I’m glad that the solution works for you 🙂

---

<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: [November 19, 2024, 6:07pm UTC](https://discuss.elastic.co/t/best-way-to-do-a-bulk-update-with-script/369017/8 "2024-11-19T18:07:09Z")

</div>

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