當前版本7.2.0
由於有些場景需要根據條記錄的兩個時間來判斷,得出另一個屬性,所以就需要比較大小
場景:需要判斷工單是否超時,已取消的工單,就需要根據取消時間 cancelTime 和超時時間 expireTime 比較,若 cancelTime 小於 expireTime,判定爲正常,否則判定爲超時
由於DSL不支持兩個屬性直接比較,目前只能藉助腳本
問題1:兩個時間的不能直接比較
Cannot apply [>] operation to types [org.elasticsearch.script.JodaCompatibleZonedDateTime] and [org.elasticsearch.script.JodaCompatibleZonedDateTime].
最開始的版本是支持這樣比較的,但是6.1版本就移除了這個功能,所以要改用別的方法
Comparison (<) not supported on datetime fields anymore since upgrade to 6.1
然後只能查找其他方法,我也不記得是哪裏找到的,好像是翻牆吧,現在再找找不到了,官方文檔也沒有翻到
doc['cancelTime'].value.toInstant().toEpochMilli()
問題2:腳本需要判空操作:這個方法是可以用,但是如果碰到有爲空的屬性就會報錯,比如整個系統,肯定會有 cancelTime 不存在的數據,那麼就會報錯
A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!
所以腳本還需要加上判空操作,終於查詢成功
GET order_index_dev/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"status": 5
}
},
{
"script": {
"script": "if (doc['expireTime'].size()==0 || doc['cancelTime'].size()==0) {return false} else {return doc['cancelTime'].value.toInstant().toEpochMilli() > doc['expireTime'].value.toInstant().toEpochMilli()}"
}
}
]
}
}
}