# Copy some fields from one index to another

**URL:** https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421
**Category:** Elasticsearch
**Created:** [June 16, 2022, 2:04pm UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421 "2022-06-16T14:04:55Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![lubosvr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lubosvr/32/104400_2.png) [@lubosvr](https://discuss.elastic.co/u/lubosvr)
#### Post date: [June 16, 2022, 2:04pm UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/1 "2022-06-16T14:04:55Z")

</div>

I am thinking if it's possible to merge two indices to holt my data

1. first\_index:

```auto
PUT first_index
{
  "mappings": {
    "dynamic": "false",
    "_source": {
      "excludes": ["content"]
    },
    "properties": {
      "content": {
        "type": "text"
      },
      "title": {
        "type": "text"
      }
    }
  }
}

```

1. second\_index:

```auto
PUT second_index
{
  "mappings": {
    "dynamic": "false",
    "properties": {
      "content": {
        "type": "text",
        "index": false
      }
    }
  }
}

```

As you can see, in the first\_index the field `content` is marked to not be stored in `_source` and the same field is not indexed in `second_index`. I want to use it as a backup for future reindexing.  
When I do reindexing of the `first_index` I'm about to lose the `content` field. Is there some way how to fill the `content` field from the `second_index`?

```auto
POST _reindex
{
  "source": {
    "index": "first_index"
  },
  "dest": {
    "index": "third_index"
  }
}

```

I mean I will not be able to find in `third_index`'s `content` field

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [June 17, 2022, 6:50am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/2 "2022-06-17T06:50:31Z")

</div>

[Reindex API | Elasticsearch Guide [8.2] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.2/docs-reindex.html#reindex-with-an-ingest-pipeline) might work, where the pipeline pulls the second index in.

---

<div class="post-metadata">

### Author: ![lubosvr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lubosvr/32/104400_2.png) [@lubosvr](https://discuss.elastic.co/u/lubosvr)
#### Post date: [June 17, 2022, 7:26am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/3 "2022-06-17T07:26:56Z")

</div>

Well, but is Reindex API able to merge documents or do I have to store all documents twice?  
I mean in `first_index` I have document with all searcheable fields `content` and `title` in my example, but to save the space I was thinking to have only field `content`.  
Documents are not merged, but rewritten (they miss `title` field) when I try to use this

```auto
POST _reindex
{
  "source": {
    "index": ["first_index", "second_index"]
  },
  "dest": {
    "index": "third_index"
  }
}

```

---

<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: [June 20, 2022, 6:33am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/4 "2022-06-20T06:33:11Z")

</div>

I suppose there is no such merging built-in function for re-index.

I'm not sure it works, but one possibility is to set [enrich processor](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest-enriching-data.html) on the destination third\_index for the reindex from first\_index. You can add some fields from the matched document in the second index.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [June 20, 2022, 6:45am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/5 "2022-06-20T06:45:17Z")

</div>

> [@lubosvr](#):
>
> Well, but is Reindex API able to merge documents or do I have to store all documents twice?

If you look at the link, it shows you can use an ingest pipeline. That approach should be able to do the merge for you.

---

<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: [June 20, 2022, 6:54am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/6 "2022-06-20T06:54:04Z")

</div>

Thank you @warkolm , I missed you said about the ingest pipelines. I was just repeating the same thing you said.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [June 20, 2022, 7:03am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/7 "2022-06-20T07:03:39Z")

</div>

Yep, no worries 🙂

---

<div class="post-metadata">

### Author: ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)
#### Post date: [June 20, 2022, 7:07am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/8 "2022-06-20T07:07:39Z")

</div>

Yes, you can achieve it using a simple script, you don't have to use any processor for it, step by step thing to achieve it.

1. Create a new index with a single field only, for example

```auto
PUT /second_index
{
   "mappings" : {
       "properties" : {
           "content" : {
               "type" : "text"
           }
       }
   }
}

```

Now, use the reindex API as shown below

```auto
PUT /_reindex
{
    "source": {
        "index": "first_index"
    },
    "dest": {
        "index": "second_index"
    },
    "script": {
         "source": "ctx._source.content=ctx._source.content + ' ' + ctx._source.title"
    }
}

```

Note: I am assuming your first index had `content` and `title` field and this simple script will just merge the values of these two fields and store it in `content` field of `second_index`. Hope this helps.

Update: You can test it, by simply indexing 2 documents(I already did) and search on second index as

POST second\_index/\_search

Response

```auto
"hits": [
            {
                "_index": "second_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "title",
                    "content": "content title"
                }
            },
            {
                "_index": "second_index",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "latest title",
                    "content": "latest content latest title"
                }
            }
        ]

```

---

<div class="post-metadata">

### Author: ![lubosvr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lubosvr/32/104400_2.png) [@lubosvr](https://discuss.elastic.co/u/lubosvr)
#### Post date: [June 20, 2022, 11:00am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/9 "2022-06-20T11:00:20Z")

</div>

Hi Amit,  
thanks for your response, but this is not what I wand. I don't want to merge two fields from the same index to the new one. This is easy.  
I want to merge field field `title` from `first_index` and field `content` from `second_index` into the new `third_index`.

---

<div class="post-metadata">

### Author: ![amitmbm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amitmbm/32/102111_2.png) [@amitmbm](https://discuss.elastic.co/u/amitmbm)
#### Post date: [June 20, 2022, 11:24am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/10 "2022-06-20T11:24:48Z")

</div>

Got it, let me try and get back to you.

---

<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 18, 2022, 11:24am UTC](https://discuss.elastic.co/t/copy-some-fields-from-one-index-to-another/307421/11 "2022-07-18T11:24:51Z")

</div>

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