elasticsearch - 如何使用 Elasticsearch 插件定义的过滤器

标签 elasticsearch elasticsearch-plugin

我为 Elasticsearch 创建了一个插件并成功安装了它(http://localhost:9200/_nodes/plugins/ 显示它已安装。)但我似乎无法在我的查询中使用它 - 我只得到错误。 “ScriptException [禁用 [groovy] 的动态脚本]”。看来我需要不同的语言设置。但我试过'lang':'java'。没有喜悦。我试过 lang: 表达式。然后我得到“ExpressionScriptCompilationException [表达式中的未知变量 [maxmind]”。如何访问我创建的插件?还是我需要做更多的事情来注册它?

我一直在关注这个优秀的指南:
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 - 如何使用 Elasticsearch 插件定义的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784135/

相关文章:

elasticsearch - 有没有办法像其他编程语言一样用脚本迭代弹性数组文档

python - 如何使用 python 脚本增加 elasticsearch 中的 max_result_window?

java - 无法启动 ElasticSearch - 找不到插件类

java - 将 Elasticsearch 的 DeleteByQueryPlugin 从 2.4 迁移到 5.2.2

elasticsearch - 更改映射字段类型elasticsearch 1.7

elasticsearch - 如何使用elasticsearch正确处理多词同义词扩展?

elasticsearch - Timelion多次 split

search - 通过术语弹性​​搜索限制过滤器

ruby - 如何使用 ruby​​ 遍历这个 json 文档?

php - Elasticsearch 2.3通过使用PHP库进行查询来删除类型中的所有文档