My elastic index has 3 attributes: - accountNumber, timestamp and score. I want to calculate the difference in score from today to the previous day (or the last value) for each account and build visualizations for the difference. How can I calculate the difference in score for each account and save in a derived variable?
I need help with scripted / runtime variables. New to elasticsearch
tried this - but getting error with new BoolQueryBuilder()
// Retrieve today's score
def todayScore = doc['score'].value;
// Search for the last score for the current accountNumber
def lastScore = 0; // Default value if no score found
def lastQuery = new BoolQueryBuilder().must(
QueryBuilders.termQuery('accountNumber', doc['accountNumber'].value)
).mustNot(
QueryBuilders.termQuery('_id', doc['_id'].value) // Exclude the current document
);
def lastResult = client().prepareSearch('your_index_name')
.setQuery(lastQuery).addSort(SortBuilders.fieldSort('timestamp').order(SortOrder.DESC)).setSize(1).get();
if (lastResult.getHits().getTotalHits() > 0) {
def lastDoc = lastResult.getHits().getAt(0);
lastScore = lastDoc['score'].value;
}
// Calculate the score difference
def scoreDifference = todayScore - lastScore;
return scoreDifference;