【发布时间】:2015-07-28 00:25:03
【问题描述】:
我有一个本地脚本,在添加新文档时作为转换运行。我需要文档 ID,但它没有作为脚本参数的一部分传入。只有 _source 作为参数传入,而不是 _id 值。有什么方法可以让 ElasticSearch 传入 _id?或者以某种方式阅读它……以某种方式?
下面是一个人为的 ElasticSearch 原生脚本示例,它演示了我在说什么。 setNextVar() 由 ElasticSearch 调用并传入“ctx”对象。该值是一个 Map,其中只有一个对象,即 _source 对象。
但是ElasticSearch默认不传入_id键值对。我希望有一个为什么要在映射 json 中配置本机脚本,告诉它传递文档 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,如果记忆对我有用,_id字段可能仍然不存在(例如,如果在现已弃用的转换中使用该脚本)。也许这就是他们没有通过它的原因?
标签: plugins elasticsearch transform