# Elasticsearch-hadoop and updating records

**URL:** https://discuss.elastic.co/t/elasticsearch-hadoop-and-updating-records/54492
**Category:** Elasticsearch
**Tags:** es-hadoop
**Created:** [July 1, 2016, 3:41am UTC](https://discuss.elastic.co/t/elasticsearch-hadoop-and-updating-records/54492 "2016-07-01T03:41:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jspooner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jspooner/32/12984_2.png) [@jspooner](https://discuss.elastic.co/u/jspooner)
#### Post date: [July 1, 2016, 3:41am UTC](https://discuss.elastic.co/t/elasticsearch-hadoop-and-updating-records/54492/1 "2016-07-01T03:41:43Z")

</div>

Let's create 3 records and save them to ES

```
val df = sc.makeRDD(
  Seq(
    (Map(ID -> 1), Device("a", "Apple")), 
    (Map(ID -> 2), Device("b", "Banana")), 
    (Map(ID -> 3), Device("c", "Carrot"))
  ))

EsSpark.saveToEsWithMeta(df, "jstest/device", esConfig)

```

Now we can verify the records

```
val df2 = EsSpark.esRDD(sc, "jstest/device", esConfig)
df2.take(3).foreach(println)
(1,Map(id -> a, name -> Apple))
(2,Map(id -> b, name -> Banana))
(3,Map(id -> c, name -> Carrot))

```

Now let's say we have some new information for each of these docs so we do this

```
val df = sc.makeRDD(
  Seq(
    (Map(ID -> 1), Map("Skate" -> "board")), 
    (Map(ID -> 2), Map("Golf" -> "club"))
  ))

EsSpark.saveToEsWithMeta(df, "jstest/device", esConfig)

```

However we end up overwriting these documents vs appending the attributes to them.

```
val df2 = EsSpark.esRDD(sc, "jstest/device", esConfig)
df2.take(3).foreach(println)
(1,Map(Skate -> board))
(2,Map(Golf -> club))
(3,Map(id -> c, name -> Carrot))

```

The ES \_bulk upload API has an update feature. For example we can do an initial bulk upload

```
POST devices-v1/device/_bulk
{"index":{"_id":"3FB5CE7C0B7A"}}
{"worstgolfer":"Dave"}

```

And the document looks like

```
GET /devices-v1/device/3FB5CE7C0B7A/_source
{
   "worstgolfer": "Dave"
}

```

Now we can append that document with a new key/value.

```
POST devices-v1/device/_bulk
{ "update" : {"_id" : "3FB5CE7C0B7A"} }
{ "doc" : {"best-golfer":"Spooner"} }
{"update":{"_id":"A5584682386F"}}
{ "doc" : {"best-golfer":"Spooner"}, "doc_as_upsert" : true }

```

And you can see we didn't overwrite the original document

```
GET /devices-v1/device/3FB5CE7C0B7A/_source
{
   "worstgolfer": "Dave",
   "best-golfer": "Spooner"
}

```

**So what can I do to get the es-hadoop library to use "update" vs "index" in the \_bulk api?**

My other ideas was to use \_mget to fetch all the documents then map the new values to each of the results but I'm not sure if [\_mget is supported](https://discuss.elastic.co/t/how-to-use--mget-in-elasticserach-hadoop/54481).

I'd also like to hear how people are handling updates in their applications.

---

<div class="post-metadata">

### Author: ![james.baiera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/james.baiera/32/10209_2.png) [@james.baiera](https://discuss.elastic.co/u/james.baiera)
#### Post date: [July 1, 2016, 2:26pm UTC](https://discuss.elastic.co/t/elasticsearch-hadoop-and-updating-records/54492/2 "2016-07-01T14:26:40Z")

</div>

Hello!

Elasticsearch for Apache Hadoop supports the following write operations: `index` (default), `create`, `update`, and `upsert` (which is just a modified `update`).

Please take a look at [this documentation page](https://www.elastic.co/guide/en/elasticsearch/hadoop/current/configuration.html#_operation) for information about the different configurations that you can employ to change how the connector creates bulk requests to Elasticsearch.

---

<div class="post-metadata">

### Author: ![jspooner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jspooner/32/12984_2.png) [@jspooner](https://discuss.elastic.co/u/jspooner)
#### Post date: [July 1, 2016, 2:37pm UTC](https://discuss.elastic.co/t/elasticsearch-hadoop-and-updating-records/54492/3 "2016-07-01T14:37:20Z")

</div>

ah, I see you set 'es.write.operation' on the config. This works as expected.

```
var esConfig:Map[String,String] = Map(
  "es.write.operation" -> "upsert"
)
```

---

<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 6, 2017, 1:24pm UTC](https://discuss.elastic.co/t/elasticsearch-hadoop-and-updating-records/54492/4 "2017-07-06T13:24:06Z")

</div>


