Mapping:
mappings": {
"alert": {
"properties": {
"history": {
"properties": {
"category": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"timestamp": {
"type": "date"
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
Example _source
:
"_source": {
"history": [
{
"timestamp": "2017-06-23T10:44:31.974Z",
"user": "dave",
"type": "read",
"category": "true"
},
{
"timestamp": "2017-06-23T10:44:33.951Z",
"user": "dave",
"type": "status",
"category": "closed",
"description": "Closure reason: weather"
}
],
}
My query:
{
"query": {
"bool": {
"must": {
"script": {
"script": {
"inline": "int isClosed = 0; long latestTimestamp = 0; for(int i=0; i<doc['history.type.keyword'].values.length; i++) { if(doc['history.type.keyword'][i] == 'status'){ if(doc['history.timestamp'][i] > latestTimestamp){latestTimestamp = doc['history.timestamp'][i]; if(doc['history.category.keyword'][i] == 'closed') { isClosed = 1} else { isClosed = 0} }}} return isClosed;",
"lang": "painless"
}
}
}
}
}
}
In short, my query is trying to filter in (or out) the documents who's history array shows that the most recent entry of type 'status' was of category type 'closed' (the status can open and close many times over the entity's lifetime).
Nothing I do seems to make this script operate as expected. I've stripped it down section by section. Is there anything I'm missing?
My gut says this might be related to this thread on date parsing: 5.0 - Comparing dates in painless however in my stripped down tests, ES seems perfectly capable of correctly comparing a long
against doc['history.timestamp'][i]
.
Any help appreciated.