For example I have a date field delivery_datetime
in Index, I have to show the user for the current day whether a particular parcel is Due today or Over Due or Not Due
I can't create a separate field and do reindex because it's based on current date and that changes every day, for instance if I have to calculate while indexing I have to reindex every day, and that's not feasible because I have a lot of data.
I may use update by query but my index is frequently updated via a Python script, thought we don't have ACID property here we'll have version conflict.
For my knowledge I think my only option is to use Scripted Field.
If I have to write the logic in pseudocode:
Due - delivery_datetime.dateOnly == now.dateOnly
Over Due - delivery_datetime.dateOnly < now.dateOnly
Not Due - delivery_datetime.dateOnly > now.dateOnly
Thought I have a lot of data if I generate CSV I don't want scripted field to make major impact on cluster performance.
So I need some help to do this efficiently in scripted field, or if there were any completely different solution will also be greatly helpful.
Expecting help by providing painless script if Scripted Field is the only solution.
Answering my own question, here is what worked for me.
Scripted Field Script:
def DiffMillis = 0;
if(!doc['delivery_datetime'].empty) {
// Converting each to days, 1000*60*60*24 = 86400000
DiffMillis = (new Date().getTime() / 86400000) - (doc['delivery_datetime'].value.getMillis() / 86400000);
}
doc['delivery_datetime'].empty ? "No Due Date": (DiffMillis==0?"Due": (DiffMillis>0?"Over Due":"Not Due") )
I specifically used ternary operator, because if I use if else
then I have to use return
, if I use return
I faced search_phase_execution_exception
while adding filters for the scripted field.