Combine three scripted fields into one

(Kevin Maguire) #1

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?


(Ryan Ernst) #2

Try reindexing with a script. There are several examples in the reindex docs of manipulating the document source while reindexing.

(Kevin Maguire) #3


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)
delete city1
delete city2
delete city3

(Ryan Ernst) #4

Painless is mostly a subset of java, with a few syntactic sugars from groovy. It is documented, in particular with a full specification of the syntax.

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

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);

(system) closed #5

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