Elasticsearch contains document like
{
"array":["1","2"],
"str": "123"
}
With mapping
"array" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"all"
],
"norms" : false,
"analyzer" : "logspeak"
}
and
"str" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
"copy_to" : [
"all"
],
"norms" : false,
"analyzer" : "logspeak"
}
If i do
Debug.explain(doc['array.keyword']);
and
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:
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();
}
}