# Compare field values but not in the same document

**URL:** <https://discuss.elastic.co/t/compare-field-values-but-not-in-the-same-document/184309>\
**Category:** Elasticsearch\
**Created:** [June 5, 2019, 8:47am UTC](https://discuss.elastic.co/t/compare-field-values-but-not-in-the-same-document/184309 "2019-06-05T08:47:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasony](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jasony](https://discuss.elastic.co/u/jasony)\
**Post date:** [June 5, 2019, 8:47am UTC](https://discuss.elastic.co/t/compare-field-values-but-not-in-the-same-document/184309/1 "2019-06-05T08:47:18Z")

</div>

Hello,

I am trying to compare 2 field values (not in the same documents but in the same index) then perform MINUS operation with other field values.

My data looks below. As you see, "UUID" and "tx\_id" have the same value but not in the same document.

First I turned on fielddata for the target fields "UUID" and "tx\_id".

```
PUT test01/_mapping/_doc
{
  "properties": {
    "UUID": { 
      "type": "text",
      "fielddata": true
    },
    "tx_id": { 
      "type": "text",
      "fielddata": true
    }
  }
}

```

Then, I put the data as below.

```
PUT test01/doc/1
{
  "UUID": "016667ba-d7db-4367-a0c8-fbeffb7c9dfb",
  "logdate_gw": "2019-06-05T01:51:24.498Z"
}
PUT test01/doc/2
{
  "UUID": "12345ba-d7db-4367-a0c8-fbeffb7c9dfb",
  "logdate_gw": "2019-06-05T01:51:34.498Z"
}
PUT test01/doc/3
{
  "UUID": "67890ba-d7db-4367-a0c8-fbeffb7c9dfb",
  "logdate_gw": "2019-06-05T01:51:44.498Z"
}
PUT test01/doc/4
{
  "tx_id": "016667ba-d7db-4367-a0c8-fbeffb7c9dfb",
  "logdate_app": "2019-06-05T01:51:26.498Z"
}
PUT test01/doc/5
{
  "tx_id": "12345ba-d7db-4367-a0c8-fbeffb7c9dfb",
  "logdate_app": "2019-06-05T01:51:37.498Z"
}
PUT test01/doc/6
{
  "tx_id": "67890ba-d7db-4367-a0c8-fbeffb7c9dfb",
  "logdate_app": "2019-06-05T01:51:48.498Z"
}

```

Below was what I tried for searching and minus operation, but I got failed (result was shown as "null").

```
GET test01/_search
{
    "query": {
      "match_all": {}
    },
    "script_fields": {
      "latency_logdate": {
        "script": {
          "inline": "if (doc['UUID'].value == doc['tx_id'].value) { doc['logdate_gw'].date.millisOfDay - doc['logdate_app'].date.millisOfDay}",
          "lang": "painless"
        }
      }
    }
}

```

Please advise if i can do this with script\_fields or any other ways.

Thank 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 3, 2019, 8:47am UTC](https://discuss.elastic.co/t/compare-field-values-but-not-in-the-same-document/184309/2 "2019-07-03T08:47:19Z")

</div>

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