# Is allowed to do a upsert with 'script\_upsert' from Cascading

**URL:** <https://discuss.elastic.co/t/is-allowed-to-do-a-upsert-with-script-upsert-from-cascading/44052>\
**Category:** Elasticsearch\
**Tags:** es-hadoop\
**Created:** [March 10, 2016, 4:21pm UTC](https://discuss.elastic.co/t/is-allowed-to-do-a-upsert-with-script-upsert-from-cascading/44052 "2016-03-10T16:21:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebaas](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@sebaas](https://discuss.elastic.co/u/sebaas)\
**Post date:** [March 10, 2016, 4:21pm UTC](https://discuss.elastic.co/t/is-allowed-to-do-a-upsert-with-script-upsert-from-cascading/44052/1 "2016-03-10T16:21:05Z")

</div>

I'm trying to upsert a document from Cascading, and then I expect that the script will executed, but It doesn't happens at initial time, but when I try again (the document already exist) the script is executed.

//Setup

```
	Properties props = new Properties();
	props.setProperty(ConfigurationOptions.ES_NODES, properties.getProperty("es.nodes"));
	props.setProperty(ConfigurationOptions.ES_WRITE_OPERATION, ConfigurationOptions.ES_OPERATION_UPSERT);
	props.setProperty(ConfigurationOptions.ES_UPDATE_SCRIPT, "upsert-test");
	props.setProperty(ConfigurationOptions.ES_UPDATE_SCRIPT_LANG, "groovy");
	props.setProperty(ConfigurationOptions.ES_UPDATE_SCRIPT_PARAMS_JSON, "{ param1 : 1.2 }");

	Hfs inTap = new Hfs(new TextDelimited(false, "\n"), inputPath);
	EsTap outTap = new EsTap("/test/test", Fields.ALL);
	ScrubFunction scrubFunction = new ScrubFunction("id","test1","test2");
	Pipe processPipe = new Each("processPipe", scrubFunction, Fields.RESULTS);

	new Hadoop2MR1FlowConnector(props).connect(inTap, outTap, pagesPipe).complete();

```

upsert-test.groovy

```
	import org.elasticsearch.common.logging.*; 
	ESLogger logger = ESLoggerFactory.getLogger('update-weights');
	logger.info('Entering'); 
	def test1 = ctx._source.'test1'
	def test2 = ctx._source.'test2'
	ctx._source.'test3' = test1 + test2 

```

The logs when It runs a first time:  
[2016-03-10 12:42:56,407][INFO][cluster.metadata] [Synch] [test] update\_mapping [test] (dynamic)  
Result:

```
		{
		_index: "test",
		_type: "test",
		_id: "1",
		_score: 1,
		_source: {
				test1: 1,
				test2: 2
			}
	}

```

At second time:  
[2016-03-10 12:52:53,833][INFO][upsert-test] Entering  
Result:

```
	{
		_index: "test",
		_type: "test",
		_id: "1",
		_score: 1,
		_source: {
				test1: 1,
				test2: 2,
				test3: 3
			}
	}
```

---

<div class="post-metadata">

**Author:** ![costin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/costin/32/44950_2.png) [@costin](https://discuss.elastic.co/u/costin)\
**Post date:** [March 12, 2016, 2:34pm UTC](https://discuss.elastic.co/t/is-allowed-to-do-a-upsert-with-script-upsert-from-cascading/44052/2 "2016-03-12T14:34:52Z")

</div>

Which matches the behaviour in ES (which is the actual executor of the script). To quote the ES [docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#upserts):

> If the document does not already exist, the contents of the upsert element  
> will be inserted as a new document. If the document does exist, then the  
> script will be executed instead:

If the behaviour in ES-Hadoop is curious, try and use the rest client against vanilla ES to see whether it differs.

---

<div class="post-metadata">

**Author:** ![andresg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andresg/32/8514_2.png) [@andresg](https://discuss.elastic.co/u/andresg)\
**Post date:** [March 16, 2016, 9:11pm UTC](https://discuss.elastic.co/t/is-allowed-to-do-a-upsert-with-script-upsert-from-cascading/44052/3 "2016-03-16T21:11:40Z")

</div>

Hi @costin, it seems neither of the two upsert options are working with ES-Hadoop connector (2.2.0).  
There is no way to set "upsert" element and 'scripted\_upsert': true doesn't have any effect.

Do you know if these features will be available in the near future?

---

<div class="post-metadata">

**Author:** ![costin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/costin/32/44950_2.png) [@costin](https://discuss.elastic.co/u/costin)\
**Post date:** [March 21, 2016, 10:22am UTC](https://discuss.elastic.co/t/is-allowed-to-do-a-upsert-with-script-upsert-from-cascading/44052/4 "2016-03-21T10:22:51Z")

</div>

Sorry , I don't follow. What two upsert options? As I quoted from the ES doc, if the doc doesn't exist, it will be inserted.  
The script is executed _only_ if the document is updated.

---

<div class="post-metadata">

**Author:** ![andresg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andresg/32/8514_2.png) [@andresg](https://discuss.elastic.co/u/andresg)\
**Post date:** [March 21, 2016, 12:33pm UTC](https://discuss.elastic.co/t/is-allowed-to-do-a-upsert-with-script-upsert-from-cascading/44052/5 "2016-03-21T12:33:09Z")

</div>

@costin you're right, if the doc doesn't exist, it will be inserted, and **'scripted\_upsert': true** feature will be included soon, as you mention in other post. Thanks.

---

<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:25pm UTC](https://discuss.elastic.co/t/is-allowed-to-do-a-upsert-with-script-upsert-from-cascading/44052/6 "2017-07-06T13:25:38Z")

</div>


