In my e-commerce project, I was trying to sort the products based on the sale start field and also by the orderingData from the PIM. eg: I have salestart key in the ES document and also we have orderingDatalike(1,2,3,4)
In FED(react) we need to order the products by orderingData and need to push the not available for sale data to last
from: fromIndex,
index: 'products',
body: {
size: 30,
query: {
bool: {
must:
term: ['category.categoryId'] = 1
}
},
sort: [{_script: {
type: 'number',
script: {
"lang": "painless",
"source" : "(doc['saleStart'].value.toInstant().toEpochMilli() > params.current_time.toInstant().toEpochMilli()) ? 1 : 0",
"params": {
"current_time" : moment(new Date())
}
},
order: 'asc',
}},
'orderingData': 'asc']
}
but i got compilation erro like this
"error": "[script_exception] runtime error, with { script_stack={ 0="(doc['saleStart'].value.getMillis() > params.current_time.getMillis()) ? 1 : 0" & 1=" ^---- HERE" } & script="(doc['saleStart'].value.getMillis() > params.current_time.getMillis()) ? 1 : 0" & lang="painless" & position={ offset=61 & start=0 & end=82 } }"
}
Tried multiple ways and still got the same kind of error. am new to elasticsearch please give me with some solutions to make it work
Thanks in advance
2
Answers
push the test data
test to see the current timestamp, please note that it can show the UTC timestamp.
the query will sort by saleStart date if the date bigger than current timestamp. If not it will sort by orderingData. Also, If the timestamps are the same it will sort by orderingData after sort by saleStart.
The error you are encountering is due to a syntax issue in your Elasticsearch script. The error message indicates that the problem is with the expression around the position where the caret (
^
) is pointing. To fix this, you can use the following script:In this updated script, we added a check to see if the ‘saleStart’ field exists in the document using
doc['saleStart'].size() > 0
. If it does, the script will compare the sale start time with the current time; otherwise, it will set the value to 0.Additionally, we changed the way ‘current_time’ is defined in the script. Instead of using
moment(new Date())
, we directly set it to"now"
as Elasticsearch will handle the current time internally.Please make sure to use the correct variable for ‘fromIndex’ in your code, as it seems to be a placeholder in your provided snippet. Also, ensure that you have the necessary access and permissions to execute scripts in Elasticsearch.