【ElasticSearch】脚本条件
REST API
{
"query": {
"bool": {
"must": [
{
"term": {
"sqStatus": {
"value": 3,
"boost": 1
}
}
},
{
"script": {
"script": {
"source": "if (doc['finishTime'] == null) { false } else { doc['finishTime'].value.getMillis() > doc['timeLimit'].value.getMillis() }",
"lang": "painless"
},
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
Java Client
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.scriptQuery(new Script("if (doc['finishTime'] == null) { false } else { doc['finishTime'].value.getMillis() > doc['timeLimit'].value.getMillis() }")));
{
"script": {
"script": {
"lang": "painless",
"source":
"""
Instant instant = doc['resourcePublicationDate'].value.toInstant();
ZoneId zoneId = doc['resourcePublicationDate'].value.getZone();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
long rr = Long.parseLong(DateTimeFormatter.ofPattern('yyyyMMddHHmmss').format(localDateTime));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long fv = 0L;
long sv = 0L;
long rv = format.parse("2024-01-20 14:22:10").getTime();
long f = (long) doc['flagTop'].value;
long s = (long) doc['sortNo'].value;
long r = doc['resourcePublicationDate'].value.toInstant().toEpochMilli();
if (f > fv) {
return true;
} else if (f == fv) {
if (s > sv) {
return true;
} else if (s == sv) {
if (r >= rv) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
"""
},
"boost": 1
}
}
异常:
Elasticsearch exception [type=circuit_breaking_exception, reason=[script] Too many dynamic script compilations within, max: [75/5m]; please use indexed, or scripts with parameters instead; this limit can be changed by the [script.context.filter.max_compilations_rate] setting]
1、调高限制
PUT _cluster/settings
{
"transient" : {
"script.max_compilations_rate" : "200/1m"
}
}
2、使用参数书是脚本内容不变