在 Elasticsearch 中使用query的時候, 如果指定的欄位不存在的話,會發生 error illegal_state_exception 。
可以按照response內的建議,使用 doc[<field>].size()==0 或是 doc.containsKey('myfield') 去做檢查。
"caused_by" : {
"type" : "illegal_state_exception",
"reason" : "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
}
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.get(ScriptDocValues.java:246)",
"org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.getValue(ScriptDocValues.java:240)",
"return 0.3 * doc['test_field'].value;",
" ^---- HERE"
],
Using Datetime in Painless | Painless Scripting Language [8.3] | Elastic https://www.elastic.co/guide/en/elasticsearch/painless/8.3/painless-datetime.html
"When a query’s results span multiple indexes, some indexes may not contain a specific field. Use the containsKey method call on the doc input to ensure a field exists as part of the index for the current document."