elasticsearch 如何使用脚本比较同一文档中的两个字段

h22fl7wq  于 2023-03-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(215)

我遇到了一个您在讲座中没有讨论的问题。如何通过比较两个字段来获得结果。我在下面提供了一个示例数据。我只需要订单的returnValue不等于其payments.paymentValue的记录。
下面是示例记录结构:

{
        "_index" : "return-202202",
        "_type" : "return",
        "_id" : "return-202202",
        "_source" : {
          "country" : "US",
          "referenceInvoiceNumber" : null,
          "returnValue" : 25.2,
          "payBonus" : true,
          "payments" : [
            {
              "paymentValue" : 25.2,
              "paymentTransactions" : [
                {
                  "transactionType" : "REFUND",
                  "transactionIssuerCode" : null,
                  "transactionCurrency" : "USD",
                  "transactionStatus" : "SUCCESS",
                  "transactionAmount" : 55.2,
                  "transactionDate" : "2022-02-01T00:02:50Z",
                  "transactionId" : "011111XXXXX-REFUND"
                }
              ],
 
              "paymentId" : "011111XXXXXX",
              "paymentCreationDate" : "2022-01-16T02:37:37Z",
              "paymentStatus" : "PAID",
              "paymentType" : "CREDITCARD"
            }
          ],
          "returnCustomerName" : "XXXX EEEEE",
          "invoiceNumber" : "33323615555",
          "returnDateTime" : "2022-01-30T21:46:22Z",
          "eventId" : "dd424508-eeeee-4c61-a366-06beeee5b4a6",
          "eventType" : "RETURN_REFUND_ISSUED"
        }
}

下面给出了我提出但根本不起作用的查询:

{
  "size":1,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "eventType": {
              "query": "RETURN_REFUND_ISSUED"
            }
          }
        },
        {
          "range": {
            "timestamp": {
              "gte": 1673678805986,
              "lte": 1678862805986,
              "format": "epoch_millis"
            }
          }
        }
      ],
      "filter": [
        {
          "script": {
            "script": "doc['returnValue'].value != doc['payments.paymentValue'].value"
          }
        }
      ]
    }
  }
}
qvk1mo1f

qvk1mo1f1#

经过更多的研究和尝试,我找到了一个解决方案,我的查询现在可以工作了。但是如果存在更好的解决方案,人们仍然可以提出建议。

"filter": [
        {
          "script": {
            "script": {
              "lang": "painless",
              "source": "Math.round(doc['returnValue'].value * 100) / 100 != Math.round(doc['payments.paymentValue'].values.sum() * 100) / 100"
            }
          }
        }
      ]

相关问题