我的写的类分别是:
public class ScriptFactory implements NativeScriptFactory {
public ExecutableScript newScript(@Nullable Map<String, Object> params) {
return new Script(params);
}
public boolean needsScores() {
return true;
}
public String getName() {
// TODO Auto-generated method stub
return null;
}
}
public class Script extends AbstractDoubleSearchScript {
private String[] paramArray;
public Script(@Nullable Map<String, Object> params) {
if (params == null || params.size() == 0) {
return;
}
Set<String> keys = params.keySet();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String val = params.get(key).toString();
System.out.println("key:" + key + ", val:" + val + "\r\n");
}
if (params.get("fields") == null) {
return;
}
paramArray = params.get("fields").toString().split(",");
System.out.println("fields:" + params.get("fields").toString());
}
public double runAsDouble() {
double defaultReturnVal = Double.parseDouble(String
.valueOf(((ScriptDocValues.Longs) doc().get("id")).getValue()));
if (paramArray == null || paramArray.length == 0) {
return defaultReturnVal;
}
return defaultReturnVal * (new Random().nextDouble());
}
}
然后打包后放到了elasticsearch的lib目录下,修改了elasticsearch的配置文件
添加了一行这个代码: