I am looking for a solution to convert an object to string during reindexing. I searched for solutions using painless scripting but was unable to do so as it seems painless doesn't support JSON ( might be wrong here )
There is no class handle Json in painless, as you said. If you have only numerical fields, the easiest way might be to replace '{' with '{"' and so on. If it is not so simple, maybe you have to make your custom stringifier (possibly recursive) function of hash.
Thanks @Tomo_M for the reply. Yes, looks like we need to implement the stringify function in painless script. Our object contains complex data types as well apart from numerical.
Was wondering if there is any other approach ?
We thought of enforcing the mapping on the new index ( index-b in the above example )
But in this case, the field req.body cannot be searched. Is there a way during indexing itself we enforce that the field req.body be indexed as string.
We don't have control over the data coming from source, so we can't stringify the req.body at the source. We need a solution to do it at the Elasticsearch layer
Below is a painless jsonify sample, but I don't recommend to use such script reinventing the wheel. I have no idea about other way to do it purely in Elasticsearch.
The easier way is use some client (eg. python). Retrive documents, jsonify using library and update documents with new jsonified text field.
String jsonify(def object){
if (object instanceof Map) {
String out = "{";
List keyList = new ArrayList(object.keySet());
Collections.sort(keyList);
for (int i=0; i< keyList.length; i++){
String key = keyList[i];
out = out + "\"" + key + "\"";
if (object[key] instanceof String){
out = out + ": \"" + object[key] + "\""
} else {
out = out + ":" + jsonify(object[key])
}
if (i< keyList.length-1){
out = out+","
}
}
out = out + "}";
return out
} else if (object instanceof ArrayList) {
String out = "[";
for (int i = 0; i< object.length; i++){
out = out + jsonify(object[i]);
if (i< object.length-1){
out = out + ","
}
}
out = out + "]";
return out
} else {
if (object instanceof String){
return "\"" + object + "\""
} else {
return object.toString()
}
}
}
jsonify(params._source)
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.