Hi,
I am trying to find a way to use two different queries (on different indices) in the same watcher.
My case:
I have two indices:
-
index1 - contains the documents I need to count
-
index2 - here I will store the countings
The watcher should:
- get the number of documents from index1 within a date range (would be nice if I could use the date from the last entry of index2)
- sum the counted documents from index 1 and the value of last entry in index2
- save the sum to index 2
Here's my code so far:
PUT _watcher/watch/postsurveyrequest_count/
{
"trigger": {
"schedule": {
"interval": "1m"
}
},
"input": {
"search": {
"request": {
"indices": [
"index1"
],
"body": {
"query": { //how to add second query?
"bool": {
"must": {
"match": {
"fields.RequestType": "PostSurveyRequest"
}
},
"filter": {
"range": {
"@timestamp": {
"lt": "now-3m/m" //here would be nice to return the date from last entry in index2
}
}
}
}
}
}
},
"extract": [
"hits.total"
]//extract hits.total from first query and latest entry count value from the to-be second query
}
},
"actions": {
"index": {
"transform": {
"script": {
"inline": "return [ 'sum': ctx.payload.hits.total , 'time' :ctx.trigger.scheduled_time ]", //the sum should be something like: ctx.payload.hits.total + secondquery_result
}
},
"index": {
"index": "index2",
"doc_type": "PostSurveyRequestSum"
}
}
}
}
I've added comments in the code with the functionality I would like. Can something like this be achieved?
Other questions:
- Could I insert a specific date instead of ctx.trigger.scheduled_time? If yes, how?
- Would it be possible to have some "global variables"? To store query results and use them in other parts of the code.
Any tips or links to documentation would be appreciated.