# Scripted field using painless - creating new field based on multiple fields

**URL:** <https://discuss.elastic.co/t/scripted-field-using-painless-creating-new-field-based-on-multiple-fields/296402>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [February 6, 2022, 8:38pm UTC](https://discuss.elastic.co/t/scripted-field-using-painless-creating-new-field-based-on-multiple-fields/296402 "2022-02-06T20:38:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iraidalavrovaa](https://avatars.discourse-cdn.com/v4/letter/i/f475e1/32.png) [@iraidalavrovaa](https://discuss.elastic.co/u/iraidalavrovaa)\
**Post date:** [February 6, 2022, 8:38pm UTC](https://discuss.elastic.co/t/scripted-field-using-painless-creating-new-field-based-on-multiple-fields/296402/1 "2022-02-06T20:38:30Z")

</div>

Hi all,

I have multiple fields containing the values of the same type (strings). The underlying structure of the division into these fields has changed. So I want to reorganize these fields for better filtering and visualization. I want to create a new scripted field based on one of the old fields. But I also want to exclude some values and add values from the other old field.

Can you please help me with the syntax? I found something like ' doc['field1'].value + '\_' + doc['field2'].value '. This action combines my first text value from one field with the first text value from the other field into one but I want to create a new extended field based on old separate values.

I also want to ask how to select exactly those values that I need from the fields. I mean how 'NewField \<- field1=="NeededValue1" & field2=="NeededValue2" & field2== "NeededValue3" ' would be in painless?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 7, 2022, 1:31am UTC](https://discuss.elastic.co/t/scripted-field-using-painless-creating-new-field-based-on-multiple-fields/296402/2 "2022-02-07T01:31:05Z")

</div>

If the values are stored in an array, `doc['field1']` are List.

> **[Accessing document fields and special variables | Elasticsearch Guide \[7.17\]...](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html)**

Note that you can't use doc accessor for text fields unless you enabled `fielddata` to `true`. If you are using keyword type, there is no problem.

For the latter question, of course you can use if clause as Java to condition the script.

---

<div class="post-metadata">

**Author:** ![iraidalavrovaa](https://avatars.discourse-cdn.com/v4/letter/i/f475e1/32.png) [@iraidalavrovaa](https://discuss.elastic.co/u/iraidalavrovaa)\
**Post date:** [February 7, 2022, 5:00am UTC](https://discuss.elastic.co/t/scripted-field-using-painless-creating-new-field-based-on-multiple-fields/296402/3 "2022-02-07T05:00:59Z")

</div>

@Tomo_M yes, I am using keyword type. Could you please provide an example script of how to create a new scripted field containing the values of the two other fields?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 7, 2022, 5:19am UTC](https://discuss.elastic.co/t/scripted-field-using-painless-creating-new-field-based-on-multiple-fields/296402/4 "2022-02-07T05:19:30Z")

</div>

Hi, this place is not support but discussion forum. Please don't throw everything at me.

There is a very [kind document about Painless scripting](https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html), and this is about [conditional statement](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-statements.html#_conditional_statements). First, try yourself and you may ask here when part of it doens't go well.

And also I don't fully understand what you need. mappings of old fields, whether they are a single value or an array, what will happen if you drop some value (eg. keep ' \_ ' or not?), sample input and the desired output for some cases... should be needed. Your script help understanding what you want to do, even if it doesn't work.

---

<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 7, 2022, 5:19am UTC](https://discuss.elastic.co/t/scripted-field-using-painless-creating-new-field-based-on-multiple-fields/296402/5 "2022-03-07T05:19:45Z")

</div>

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