如何使用 Elasticsearch 插件定义的过滤器
How to use Elasticsearch plugin-defined filter
我已经为 Elasticsearch 创建了一个插件并成功安装了它(http://localhost:9200/_nodes/plugins/ 显示它已安装。)但我似乎无法在我的查询中使用它 - 我只会收到错误消息。 "ScriptException[dynamic scripting for [groovy] disabled]"。看来我需要不同的语言设置。但我试过 'lang':'java'。没有快乐。我试过 lang: 表达式。然后我得到 "ExpressionScriptCompilationException[Unknown variable [maxmind] in expression"。如何访问我创建的插件?或者我需要做更多的事情来注册它吗?
我一直在关注这个优秀的指南:
https://github.com/imotov/elasticsearch-native-script-example
但是它没有说明应该如何编写查询。
我的抽象插件:
package org.elasticsearch.plugin.maxmind;
import java.util.Collection;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.plugin.maxmind.GeoLoc;
public class MaxMind extends AbstractPlugin {
@Override public String name() {
return "maxmind";
}
@Override public String description() {
return "Plugin to annotate ip addresses with maxmind geo data";
}
// Thanks https://github.com/imotov/elasticsearch-native-script-example
public void onModule(ScriptModule module) {
module.registerScript("geoloc", GeoLoc.Factory.class);
}
}
记下姓名 "geoloc"。这是我在查询中使用的名称吗?
我的 GeoLoc 模块:
package org.elasticsearch.plugin.maxmind;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.script.ScriptException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.script.AbstractSearchScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.NativeScriptFactory;
public class GeoLoc extends AbstractSearchScript {
public static class Factory implements NativeScriptFactory {
// called on every search on every shard
@Override
public ExecutableScript newScript
(@Nullable Map<String, Object> params)
{
String fieldName = params == null ? null:
XContentMapValues.nodeStringValue(params.get("field"), null);
if (fieldName == null) {
throw new ScriptException("Missing field parameter");
}
return new GeoLoc(fieldName);
}
}
private final String fieldName;
private GeoLoc(String fieldName) {
this.fieldName = fieldName;
}
@Override
public Object run() {
ScriptDocValues docValue = (ScriptDocValues) doc().get(fieldName);
if (docValue != null && !docValue.isEmpty()) {
// TODO: real geolocation here
HashMap fakeloc = new HashMap<String, String>();
fakeloc.put("lat", "1.123");
fakeloc.put("lon", "44.001");
fakeloc.put("basedon", docValue);
return fakeloc;
}
return false;
}
}
我的查询:
{
"_source": [
"uri",
"user_agent",
"server_ip",
"server_port",
"client_ip",
"client_port"
],
"query": {
"filtered": {
"filter": {}
}
},
"script_fields": {
"test1": {
"params": {
"field": "client_ip"
},
"script": "geoloc" // is this right?
}
},
"size": 1
}
您应该可以在脚本中指定 lang: "native"
,任何用 Java 编写并在 registerScript
注册的脚本都是 "native" 类型。
我已经为 Elasticsearch 创建了一个插件并成功安装了它(http://localhost:9200/_nodes/plugins/ 显示它已安装。)但我似乎无法在我的查询中使用它 - 我只会收到错误消息。 "ScriptException[dynamic scripting for [groovy] disabled]"。看来我需要不同的语言设置。但我试过 'lang':'java'。没有快乐。我试过 lang: 表达式。然后我得到 "ExpressionScriptCompilationException[Unknown variable [maxmind] in expression"。如何访问我创建的插件?或者我需要做更多的事情来注册它吗?
我一直在关注这个优秀的指南: https://github.com/imotov/elasticsearch-native-script-example
但是它没有说明应该如何编写查询。
我的抽象插件:
package org.elasticsearch.plugin.maxmind;
import java.util.Collection;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.plugin.maxmind.GeoLoc;
public class MaxMind extends AbstractPlugin {
@Override public String name() {
return "maxmind";
}
@Override public String description() {
return "Plugin to annotate ip addresses with maxmind geo data";
}
// Thanks https://github.com/imotov/elasticsearch-native-script-example
public void onModule(ScriptModule module) {
module.registerScript("geoloc", GeoLoc.Factory.class);
}
}
记下姓名 "geoloc"。这是我在查询中使用的名称吗?
我的 GeoLoc 模块:
package org.elasticsearch.plugin.maxmind;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.script.ScriptException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.script.AbstractSearchScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.NativeScriptFactory;
public class GeoLoc extends AbstractSearchScript {
public static class Factory implements NativeScriptFactory {
// called on every search on every shard
@Override
public ExecutableScript newScript
(@Nullable Map<String, Object> params)
{
String fieldName = params == null ? null:
XContentMapValues.nodeStringValue(params.get("field"), null);
if (fieldName == null) {
throw new ScriptException("Missing field parameter");
}
return new GeoLoc(fieldName);
}
}
private final String fieldName;
private GeoLoc(String fieldName) {
this.fieldName = fieldName;
}
@Override
public Object run() {
ScriptDocValues docValue = (ScriptDocValues) doc().get(fieldName);
if (docValue != null && !docValue.isEmpty()) {
// TODO: real geolocation here
HashMap fakeloc = new HashMap<String, String>();
fakeloc.put("lat", "1.123");
fakeloc.put("lon", "44.001");
fakeloc.put("basedon", docValue);
return fakeloc;
}
return false;
}
}
我的查询:
{
"_source": [
"uri",
"user_agent",
"server_ip",
"server_port",
"client_ip",
"client_port"
],
"query": {
"filtered": {
"filter": {}
}
},
"script_fields": {
"test1": {
"params": {
"field": "client_ip"
},
"script": "geoloc" // is this right?
}
},
"size": 1
}
您应该可以在脚本中指定 lang: "native"
,任何用 Java 编写并在 registerScript
注册的脚本都是 "native" 类型。