Elasticsearch contains document like
JavaScript
{
"array":["1","2"],
"str": "123"
}
With mapping
JavaScript
"array" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"all"
],
"norms" : false,
"analyzer" : "logspeak"
}
and
JavaScript
"str" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"all"
],
"norms" : false,
"analyzer" : "logspeak"
}
If i do
JavaScript
Debug.explain(doc['array.keyword']);
and
JavaScript
Debug.explain(doc['str.keyword']);
I get org.elasticsearch.index.fielddata.ScriptDocValues$Strings
type for both fields.
How can i determine source field type? (I need get string length if field is simple string or size of array if field is array)
Advertisement
Answer
The correct painless expression to use is:
JavaScript
def size = -1;
if (doc['array.keyword'].size() > 0) {
// string case
if (doc['array.keyword'].size() == 1) {
size = doc['array.keyword'].value.length();
}
// array case
else {
size = doc['array.keyword'].values.size();
}
}