在聚合中添加脚本
{"aggs": {
"gwAppName": {
"terms": {
"field": "field",
"script": {
"source": "doc['field1'].empty?doc['field'].value:doc['field1'].value",
"lang": "painless"
}
}
}
}
字段结构
field 与 field1 均为 keyword
field 保证绝对有值,field1 不一定有值
报错
在前 10 条数据时,聚合正常
在超过 10 条数据时异常报错
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"java.nio.Buffer.checkIndex(Buffer.java:540)",
"java.nio.DirectByteBuffer.get(DirectByteBuffer.java:253)",
"org.apache.lucene.store.ByteBufferGuard.getByte(ByteBufferGuard.java:118)",
"org.apache.lucene.store.ByteBufferIndexInput$SingleBufferImpl.readByte(ByteBufferIndexInput.java:385)",
"org.apache.lucene.util.packed.DirectReader$DirectPackedReader2.get(DirectReader.java:106)",
"org.apache.lucene.codecs.lucene70.Lucene70DocValuesProducer$19.ordValue(Lucene70DocValuesProducer.java:865)",
"org.apache.lucene.index.SingletonSortedSetDocValues.advanceExact(SingletonSortedSetDocValues.java:83)",
"org.elasticsearch.index.fielddata.FieldData$10.advanceExact(FieldData.java:345)",
"org.elasticsearch.index.fielddata.ScriptDocValues$BinaryScriptDocValues.setNextDocId(ScriptDocValues.java:685)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Strings.setNextDocId(ScriptDocValues.java:720)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
"doc['field1'].empty?doc['field'].value:doc['field1'].value",
" ^---- HERE"
],
"script": "doc['field1'].empty?doc['field'].value:doc['field1'].value",
"lang": "painless"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "log-lemon-agw-access-hexi-20220924",
"node": "m6KEOrUgTMqRKvNpf1eEWw",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"java.nio.Buffer.checkIndex(Buffer.java:540)",
"java.nio.DirectByteBuffer.get(DirectByteBuffer.java:253)",
"org.apache.lucene.store.ByteBufferGuard.getByte(ByteBufferGuard.java:118)",
"org.apache.lucene.store.ByteBufferIndexInput$SingleBufferImpl.readByte(ByteBufferIndexInput.java:385)",
"org.apache.lucene.util.packed.DirectReader$DirectPackedReader2.get(DirectReader.java:106)",
"org.apache.lucene.codecs.lucene70.Lucene70DocValuesProducer$19.ordValue(Lucene70DocValuesProducer.java:865)",
"org.apache.lucene.index.SingletonSortedSetDocValues.advanceExact(SingletonSortedSetDocValues.java:83)",
"org.elasticsearch.index.fielddata.FieldData$10.advanceExact(FieldData.java:345)",
"org.elasticsearch.index.fielddata.ScriptDocValues$BinaryScriptDocValues.setNextDocId(ScriptDocValues.java:685)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Strings.setNextDocId(ScriptDocValues.java:720)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
"doc['field1'].empty?doc['field'].value:doc['field1'].value",
" ^---- HERE"
],
"script": "doc['field1'].empty?doc['field'].value:doc['field1'].value",
"lang": "painless",
"caused_by": {
"type": "index_out_of_bounds_exception",
"reason": null
}
}
}
]
},
"status": 500
}