# Copy document if fields are different

**URL:** <https://discuss.elastic.co/t/copy-document-if-fields-are-different/332269>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 2, 2023, 12:13pm UTC](https://discuss.elastic.co/t/copy-document-if-fields-are-different/332269 "2023-05-02T12:13:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lduvnjak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lduvnjak/32/77724_2.png) [@lduvnjak](https://discuss.elastic.co/u/lduvnjak)\
**Post date:** [May 2, 2023, 12:13pm UTC](https://discuss.elastic.co/t/copy-document-if-fields-are-different/332269/1 "2023-05-02T12:13:58Z")

</div>

Hey Everyone,

I have two indices which overlap with data, but there are some differences.  
What I would like, is to copy documents from `index1` to `index2`, where `index1.someField != index2.someField`. `someField` being a custom unique field which can be used for scripting here.

So for every document in `index1`, it would need to check if a document already exists in `index2` with the same `someField` value, and skip that document if it does exist.  
Basically, copying index1 to index2, but avoiding duplicates.

Could this be done with a combination of painless and a reindex, or is there some other/better way to do this? Any help would be greatly appreciated.

Cheers,  
Luka

---

<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:** [May 30, 2023, 12:14pm UTC](https://discuss.elastic.co/t/copy-document-if-fields-are-different/332269/2 "2023-05-30T12:14:35Z")

</div>

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