I've got a native script that runs as a transform when adding a new document. I need the document id but its not passed in as part of the script params. Only the _source is passed in as the params, but not the _id value. Is there any way to get ElasticSearch to pass in the _id? Or some way of reading it ... somehow?
Below is a contrived ElasticSearch native script example that demonstrates what I'm talking about. setNextVar() is called by ElasticSearch and the "ctx" object is passed in. The value is a Map that only has one object in it, the _source object.
But the _id key/value pair is not passed in by ElasticSearch by default. I'm hoping there is a why to config the native script in the mapping json that tells it to pass in the document id.
public class ExampleNativeScript extends AbstractExecutableScript {
/**
* Factory class that serves native script to ElasticSearch
*/
public static class Factory extends AbstractComponent implements NativeScriptFactory {
@Inject
public Factory(Settings settings, String prefixSettings) {
super(settings, prefixSettings);
}
@Override
public ExecutableScript newScript(@Nullable Map<String, Object> params) {
return new ExampleNativeScript(params);
}
}
private final Map<String, Object> variables;
private Map ctx = null;
private Map source = null;
public ExampleNativeScript(Map<String, Object> params) {
this.variables = params;
}
@Override
public void setNextVar(String name, Object value) {
variables.put(name, value);
if (name.equals("ctx")) {
ctx = (Map<String, LinkedHashMap>) value;
source = (Map<String, LinkedHashMap>) ctx.get("_source");
} else {
//never gets here
System.out.println("Unexpected variable");
}
}
@Override
public Object run() {
//PROBLEM: ctx only has _source. _id does not get passed in so this blows chunks
String documentId = ctx.get("_id").toString();
System.out.println(documentId);
return ctx;
}
}
AbstractExecutableScript
the_id
field might still not be there if memory serves me right (for instance if using that script in a, now deprecated, transform). Maybe that's the reason they don't pass it there? – Mateusz Dymczyk