# Combine three scripted fields into one

**URL:** <https://discuss.elastic.co/t/combine-three-scripted-fields-into-one/157238>\
**Category:** Elasticsearch\
**Created:** [November 18, 2018, 6:26pm UTC](https://discuss.elastic.co/t/combine-three-scripted-fields-into-one/157238 "2018-11-18T18:26:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [November 18, 2018, 6:26pm UTC](https://discuss.elastic.co/t/combine-three-scripted-fields-into-one/157238/1 "2018-11-18T18:26:44Z")

</div>

i have index with 3 scripted fields

maximum of one of these 3 fields is non-null. But all 3 might be null.

Let's say city1, city2, city3.

i want new single field, say city, whose value is from the non-null cityX field, or null otherwise

i can re-index into new index if it helps.

how to accomplish this?

RT

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 20, 2018, 12:18am UTC](https://discuss.elastic.co/t/combine-three-scripted-fields-into-one/157238/2 "2018-11-20T00:18:30Z")

</div>

Try reindexing with a script. There are several examples in the [reindex docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html) of manipulating the document source while reindexing.

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [November 20, 2018, 9:56pm UTC](https://discuss.elastic.co/t/combine-three-scripted-fields-into-one/157238/3 "2018-11-20T21:56:38Z")

</div>

Hi

Thanks for suggestion.

I dont see any special support for scripted\_fields in \_reindex.

I did reindex and scrupted fields stayed as scripted fields. I dont see how to override that.

Plus painless seems not the most widely used or documented language, so it not obvious to me how to get logic that will implement

if (city1) { city=city1) else  
if (city2) { city=city2) else  
if (city3) ( city=city3)  
fi  
delete city1  
delete city2  
delete city3

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [November 20, 2018, 10:51pm UTC](https://discuss.elastic.co/t/combine-three-scripted-fields-into-one/157238/4 "2018-11-20T22:51:44Z")

</div>

Painless is mostly a subset of java, with a few syntactic sugars from groovy. It is [documented](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-painless.html), in particular with a [full specification](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-lang-spec.html) of the syntax.

A reindex script like you want would look something like this:

```auto
POST _reindex
{
  "source": {
    "index": "YOUR_SOURCE_INDEX"
  },
  "dest": {
    "index": "YOUR_NEW_INDEX"
  },
  "script": {
    "source": """
String city1 = ctx._source.remove('city1');
String city2 = ctx._source.remove('city2'');
String city3 = ctx._source.remove('city3');
ctx._source['city'] = city1 ?: (city2 ?: city3);
"""
  }
}

```

---

<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:** [December 18, 2018, 10:51pm UTC](https://discuss.elastic.co/t/combine-three-scripted-fields-into-one/157238/5 "2018-12-18T22:51:48Z")

</div>

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