Compare field values but not in the same document

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!!!

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