# Combining two fields into one during Reindex

**URL:** <https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700>\
**Category:** Elasticsearch\
**Created:** [August 10, 2019, 9:47am UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700 "2019-08-10T09:47:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [August 10, 2019, 9:47am UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/1 "2019-08-10T09:47:33Z")

</div>

Hi,

We have a requirement wherein we want to combine two fields into one field during reindex.  
For Example:  
Original Index:  
field1: firstname  
field2: lastname

After reindex:  
Name: firstname lastname

I have checked some documents on reindex and \_update\_by\_query but I am not able to find my requirement.Can this be done using reindex? or Is there any other way?

Thanks,  
Nikhil

---

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [August 11, 2019, 1:19pm UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/2 "2019-08-11T13:19:50Z")

</div>

Hello,

Any suggestions?

Thanks,  
Nikhil

---

<div class="post-metadata">

**Author:** ![newbie1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@newbie1](https://discuss.elastic.co/u/newbie1)\
**Post date:** [August 11, 2019, 1:31pm UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/3 "2019-08-11T13:31:55Z")

</div>

Did you try copy\_to ?

Defined in mappings :  
"mappings": {  
"\_doc": {  
"properties": {  
"region\_name": {  
"type": "keyword",  
"copy\_to": "locations\_combined"  
},  
"country\_name": {  
"type": "keyword",  
"copy\_to": "locations\_combined"  
},  
"city\_name": {  
"type": "keyword",  
"copy\_to": "locations\_combined"  
},  
"locations\_combined": {  
"type": "text"  
}  
...

---

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [August 12, 2019, 4:30pm UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/4 "2019-08-12T16:30:24Z")

</div>

Hi @newbie1,

I tried using copy\_to. I created mappings for the new index and then reindexed the data into the new index but the field is not visible. Since it doesn't create any field, I cannot use it in visualizations.

Can it be done using update\_by\_query to add a new field which is a concatenated value of two fields?

Thanks,  
nikhil

---

<div class="post-metadata">

**Author:** ![newbie1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@newbie1](https://discuss.elastic.co/u/newbie1)\
**Post date:** [August 15, 2019, 1:09pm UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/5 "2019-08-15T13:09:14Z")

</div>

That's strange, did you check the mapping of the new index to be sure to have defined the copy\_to field ? Did you try with a sample ?

---

<div class="post-metadata">

**Author:** ![radoslav.sholev](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@radoslav.sholev](https://discuss.elastic.co/u/radoslav.sholev)\
**Post date:** [August 15, 2019, 2:08pm UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/6 "2019-08-15T14:08:29Z")

</div>

Have you tried using a script inside the reindex request? I have personally done this many times. You could combine the two fields into one and then use ctx.\_source.remove("[property name]") to remove the extra field.

---

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [August 15, 2019, 5:02pm UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/7 "2019-08-15T17:02:26Z")

</div>

Hi @newbie1,

Yes, I have tried with sample data.After using reindex and copy\_to to create a new field(containing combined value from other two fields ), it will create the new field and you can also use search based on the new field but the new field will not visible in "Discover".

---

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [August 15, 2019, 5:06pm UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/8 "2019-08-15T17:06:17Z")

</div>

Hi @radoslav.sholev,

Yes, I have tried various combination of using scripts during reindex but I did't get any expected results.

My expectation is to create to a new field(containing values from two other fields) during reindex.

Example:  
For Example:

Original Index: Containing two fields  
firstname: Ryan  
lastname: Gosling

After reindex: Contains only one field  
name: Ryan Gosling

Expectation is to combine two values into one during reindex

---

<div class="post-metadata">

**Author:** ![newbie1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@newbie1](https://discuss.elastic.co/u/newbie1)\
**Post date:** [August 16, 2019, 6:54am UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/9 "2019-08-16T06:54:23Z")

</div>

On this example could you provide the mapping of original index and teh mapping of destination index. A good test woul be to set dynamic to strict like that you will see if there is an issue during reindexation.

---

<div class="post-metadata">

**Author:** ![radoslav.sholev](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@radoslav.sholev](https://discuss.elastic.co/u/radoslav.sholev)\
**Post date:** [August 16, 2019, 7:53am UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/10 "2019-08-16T07:53:01Z")

</div>

@Nikhil04 here is a simple example of what I am trying to say. Downside is that the target index will have to create a mapping for the two fields, but you don't have to store them in the document. As I wrote before, the fields can be removed in the reindex script. Here is the example:

```
PUT index_4
{
  "mappings": {
    "properties": {
      "firstName":{
        "type":"text"
      },
      "lastName":{
        "type":"text"
      }
    }
  }
}

PUT index_4/_doc/1
{
  "firstName": "A",
  "secondName": "B"
}

PUT index_5
{
  "mappings": {
    "properties": {
      "name":{
        "type":"text"
      }
    }
  }
}

POST _reindex
{
  "source": {
    "index": "index_4"
  },
  "dest": {
    "index": "index_5"
  },
  "script": {
    "source": "ctx._source.name=ctx._source.firstName + ' ' + ctx._source.secondName"
  }
}

GET index_5/_search
```

---

<div class="post-metadata">

**Author:** ![Nikhil04](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Nikhil04](https://discuss.elastic.co/u/Nikhil04)\
**Post date:** [August 20, 2019, 10:06am UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/11 "2019-08-20T10:06:42Z")

</div>

Thank you @radoslav.sholev for your help!

---

<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:** [September 17, 2019, 10:06am UTC](https://discuss.elastic.co/t/combining-two-fields-into-one-during-reindex/194700/12 "2019-09-17T10:06:53Z")

</div>

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