# Compare Two Indexes

**URL:** <https://discuss.elastic.co/t/compare-two-indexes/269254>\
**Category:** Kibana\
**Created:** [April 5, 2021, 5:50pm UTC](https://discuss.elastic.co/t/compare-two-indexes/269254 "2021-04-05T17:50:15Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![Dinesh\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dinesh_sharma/32/86511_2.png) [@Dinesh\_Sharma](https://discuss.elastic.co/u/Dinesh_Sharma)\
**Post date:** [April 7, 2021, 4:02pm UTC](https://discuss.elastic.co/t/compare-two-indexes/269254/15 "2021-04-07T16:02:04Z")

</div>

Hi @Hendrik_Muhs ,  
The above suggested code is working like a charm. Thanks for the same.

(1) If I want to apply the transform on single index then will the below code work for me:

```auto
POST /_transform/_preview?pretty
{
  "id": "index_compare",
  "source": {
    "index": [
      **"compare_pim_index"**
    ],
    "query": {
      "match_all": {}
    }
  },
  "dest": {
    "index": "compare"
  },
  "pivot": {
    "group_by": {
      "unique-id": {
        "terms": {
          "field": "IP.keyword"
        }
      }
    },
    "aggregations": {
      "compare": {
        "scripted_metric": {
          "map_script": "state.doc = new HashMap(params['_source'])",
          "combine_script": "return state",
          "reduce_script": """ 
            if (states.size() != 2) {
return "count_mismatch"
            }
return "match"
            """
        }
      }
    }
  }
}

```

(2) And If instead of comparing the same field name document , can I compare two document with the two different field name like some document in a index will have IP1.keyword and some will have IP2.keyword then will the below code work:

```auto
POST /_transform/_preview?pretty
{
  "id": "index_compare",
  "source": {
    "index": [
      "compare_pim_index"
    ],
    "query": {
      "match_all": {}
    }
  },
  "dest": {
    "index": "compare"
  },
  "pivot": {
    "group_by": {
      "unique-id": {
        "terms": {
          **"field": "IP1.keyword",**
 **"field1":"IP2.keyword"**
        }
      }
    },
    "aggregations": {
      "compare": {
        "scripted_metric": {
          "map_script": "state.doc = new HashMap(params['_source'])",
          "combine_script": "return state",
          "reduce_script": """ 
            if (states.size() != 2) {
return "count_mismatch"
            }
return "match"
            """
        }
      }
    }
  }
}

```

---

_[View the full topic](https://discuss.elastic.co/t/compare-two-indexes/269254)._
