# Nest Bulk update in elasticsearch

**URL:** https://discuss.elastic.co/t/nest-bulk-update-in-elasticsearch/202217
**Category:** Elasticsearch
**Created:** [October 3, 2019, 5:30pm UTC](https://discuss.elastic.co/t/nest-bulk-update-in-elasticsearch/202217 "2019-10-03T17:30:07Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![piyush\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_kumar/32/51705_2.png) [@piyush\_kumar](https://discuss.elastic.co/u/piyush_kumar)
#### Post date: [October 3, 2019, 5:30pm UTC](https://discuss.elastic.co/t/nest-bulk-update-in-elasticsearch/202217/1 "2019-10-03T17:30:07Z")

</div>

Elasticsearch - 6.5.1  
Nest - 6.5.1

Hi All,

I am able to write code to update the single document 1 at a time.

```
var scriptParams = new Dictionary<string, object>
{
    {"id", BaseMapping.id}
};
return Client.UpdateByQuery<BaseMapping>(s => s
    .Query(q => q
        .Match(m => m
            .Field(f => f.question_id.Suffix("raw"))
            .Query(BaseMapping.id.ToString())
        )
    )
    .Script(script =>
        script.Source((
                $"ctx._source.id=params.id;" +
                $"ctx._source.answer='" + BaseMapping.answer + "';" +
                $"ctx._source.question='" + BaseMapping.question + "';" 
            )
            )
        .Params(scriptParams)
        )
    .Conflicts(Elasticsearch.Net.Conflicts.Proceed)
 );

```

**I wanted to write code which can do a bulk update with below logic**  
**1-check if the document exists based on \_id then update the existing document and if it doesn't exist then insert the document.**

I have referred one code from the elasticsearch documentation.

```
POST twitter/_update_by_query
{
  "script": {
    "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'update' } else { ctx.op = 'insert' }",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}

```

**can anybody help me with how to write it in Nest bulk update.?**

---

<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: [October 31, 2019, 5:30pm UTC](https://discuss.elastic.co/t/nest-bulk-update-in-elasticsearch/202217/2 "2019-10-31T17:30:10Z")

</div>

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