my elasticsearch version is 6.3.0,and here is my query:
{
"query":{
"bool" : {
"must" : [
{
"script" : {
"script" : {
"source" : "double minPrice=-1;int userLevel=params.defaultLevel;long userId=params.userId;if(params._source.containsKey('userLevels')){ for(int i = 0; i < params['_source']['userLevels'].length; ++i) { def userLevelObj = params['_source']['userLevels'][i]; if(userLevelObj.userId === userId) { userLevel = userLevelObj.level; } }}for(int ii = 0; ii < params['_source']['productSkus'].length; ++ii){ def productSku = params['_source']['productSkus'][ii]; for(int iii = 0; iii < productSku['levelPrice'].length; ++iii) { def levelPrice = productSku['levelPrice'][iii]; if(userLevel === levelPrice.level && (levelPrice.price < minPrice || minPrice === -1)) { minPrice=levelPrice.price; } }}return minPrice<=params.maxPrice;",
"lang" : "painless",
"params" : {
"defaultLevel" : 1,
"maxPrice" : 10000,
"userId" : 123
}
},
"boost" : 1.0
}
},
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
}
and i am getting following error:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 4,
"skipped": 0,
"failed": 1,
"failures": [
{
"shard": 3,
"index": "product_1",
"node": "wrIbN9oaTXGgRjDJecx8jA",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"if(params._source.containsKey('userLevels')){ ",
" ^---- HERE"
],
"script": "double minPrice=-1;int userLevel=params.defaultLevel;long userId=params.userId;if(params._source.containsKey('userLevels')){ for(int i = 0; i < params['_source']['userLevels'].length; ++i) { def userLevelObj = params['_source']['userLevels'][i]; if(userLevelObj.userId === userId) { userLevel = userLevelObj.level; } }}for(int ii = 0; ii < params['_source']['productSkus'].length; ++ii){ def productSku = params['_source']['productSkus'][ii]; for(int iii = 0; iii < productSku['levelPrice'].length; ++iii) { def levelPrice = productSku['levelPrice'][iii]; if(userLevel === levelPrice.level && (levelPrice.price < minPrice || minPrice === -1)) { minPrice=levelPrice.price; } }}return minPrice<=params.maxPrice;",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
}
}
]
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
and according to the document, I know the _source
is not accessible in script queries,and doc[...]
notation only allows for simple valued fields. but how to get a json object in script queries. or anyone can give me any suggestion to optimize my query scripy. thinks