# Python/Elasticsearch Bulk problem

**URL:** https://discuss.elastic.co/t/python-elasticsearch-bulk-problem/220219
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [February 20, 2020, 3:38pm UTC](https://discuss.elastic.co/t/python-elasticsearch-bulk-problem/220219 "2020-02-20T15:38:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ivan\_Devic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ivan_devic/32/46762_2.png) [@Ivan\_Devic](https://discuss.elastic.co/u/Ivan_Devic)
#### Post date: [February 20, 2020, 3:38pm UTC](https://discuss.elastic.co/t/python-elasticsearch-bulk-problem/220219/1 "2020-02-20T15:38:18Z")

</div>

Hi all,

I would like to perform bulk request of next action. I want to create document if it does not exist, but I would like to update it, if it does exist. Within the update, I append new values (new\_a and new\_b) to arrays which are inside a single doc (a and b). For a single document, next part of code in Python seems to work flawlessly, when I invoke update function with following body:

```
{"script":{"inline": "ctx._source.a.addAll(params.new_a); ctx._source.b.addAll(params.new_b)",
           "params": {"a": new_a, "b": new_b}},
 "upsert": {"a": new_a, "b": new_b}}

```

However, when I try to take the present logic and expand it into Bulk request, I kind of hit the wall. Thank you in advance for help

EDIT: Preview purposes only

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 21, 2020, 8:09am UTC](https://discuss.elastic.co/t/python-elasticsearch-bulk-problem/220219/2 "2020-02-21T08:09:48Z")

</div>

Hey,

can you maybe explain in more detail, what exactly fails and how your code looks like? are you calling the bulk endpoint yourself are you using the bulk helper?

Thanks!

--Alex

---

<div class="post-metadata">

### Author: ![Ivan\_Devic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ivan_devic/32/46762_2.png) [@Ivan\_Devic](https://discuss.elastic.co/u/Ivan_Devic)
#### Post date: [February 21, 2020, 9:20am UTC](https://discuss.elastic.co/t/python-elasticsearch-bulk-problem/220219/3 "2020-02-21T09:20:28Z")

</div>

Hi Alex,

so what I do is I loop over my data in a python loop and I yield every map (dictionary) that I need to update. What I send to helpers.bulk() function from a Python Elasticsearch library is next scheme

```
{"_index": index_name,
 "op_type": "update",
 "_type": doc_type
 "_id": doc_id
 "_source": {"source": updateSchema}}

```

in which updateSchema is a map from my original post. The scripts executes, but rather then storing results from scripts, it stores them as literal strings. Additionally, I tried the variant with body explained on next [link](https://stackoverflow.com/questions/53394321/update-index-with-script-in-elasticsearch-bulk-javascript-api), but again, scripts are not being executed but stored as strings.

EDIT:

Figured it out, op\_type has to be sent with a starting "\_" to be interpreted properly

---

<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: [March 20, 2020, 9:20am UTC](https://discuss.elastic.co/t/python-elasticsearch-bulk-problem/220219/4 "2020-03-20T09:20:39Z")

</div>

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