如何为Elasticsearch自定义插件编写测试? [英] How to write a test for Elasticsearch custom plugin?

查看:127
本文介绍了如何为Elasticsearch自定义插件编写测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自定义的弹性搜索插件。现在我想为这个插件写一个测试。
我的期望是 - 我可以运行嵌入式 Elasticsearch 实例,正确设置,然后进行一些测试(索引一些文档,然后查询)



问题是我无法正确设置我的插件



自定义插件代码解析JSON查询并设置一些对象供以后使用:

  public class CustomQueryParserPlugin extends AbstractPlugin {
public static final String PLUGIN_NAME =custom_query;
私人终端设置设置;

@Inject
public CustomQueryParserPlugin(设置设置){
this.settings = settings;
}

@Override
public String name(){
return PLUGIN_NAME;
}

@Override
public String description(){
returncustom plugin;
}

public void onModule(IndicesQueriesModule module){
module.addQuery(new CustomQueryParser(settings));
}
}

测试代码:

  public class CustomParserPluginTest extends ElasticsearchSingleNodeTest {

private static Node newNode(){
final设置设置= ImmutableSettings.builder()
.put(ClusterName.SETTING,nodeName())
.put(node.name,nodeName())
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS,1)
。 put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS,0)
.put(EsExecutors.PROCESSORS,1)//限制创建的线程数
.put(http.enabled,false)
.put (plugin.types,CustomParserPlugin.class.getName())
.put(path.plugins,pathToPlugin)
.put(index.store.type,ram)
.put(config.ignore_system_properties,true)//确保我们得到我们设置的:)
.put(gateway.type, 无)建立();
Node build = NodeBuilder.nodeBuilder()。local(true).data(true).settings(
settings).build();
build.start();
assertThat(DiscoveryNode.localNode(build.settings()),is(true));
return build;
}


@Test
public void jsonParsing()throws URISyntaxException {
final Client client = newNode()。
final SearchResponse test = client.prepareSearch(test-index)。setSource(addQuery())。execute()。actionGet();
}

private String addQuery(){
return{match_all:{boost:1.2}}
}

我尝试使用 pathToPlugin 的多个值 - 但似乎没有什么效果好,因为JSON查询总是给我一个例外:

  QueryParsingException [[test-index]没有为[custom_query]注册查询]; 

我可以找到的所有文档都是关于在一些本地的Elasticsearch安装中安装插件并进行测试。 >

我在这里做错什么?有没有任何文档或类似的测试示例?



UPD 。这是一个提取的CustomQueryParserPlugin代码的回购 - https://github.com/MysterionRise/es-custom-parser



可能在初始化部分测试我需要创建内存索引?

解决方案

要为您编写测试插件,您可以使用弹性搜索群集运行器
有关参考,请查看 MinHash插件如何编写测试。



更新:



我已经更改了 CustomParserPluginTest 类使用Elasticsearch Cluster Runner:

  import static org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner。 newConfigs; 
import java.util.Map;
import junit.framework.TestCase;
import org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.get.GetField;
import org.junit.Assert;
import org.elasticsearch.action.search.SearchResponse;
import static org.hamcrest.core.Is.is;

public class CustomParserPluginTest扩展TestCase {

private ElasticsearchClusterRunner runner;

@Override
protected void setUp()throws异常{
//创建转轮实例
runner = new ElasticsearchClusterRunner();
//创建ES节点
runner.onBuild(new ElasticsearchClusterRunner.Builder(){
@Override
public void build(final int number,final Builder settingsBuilder){

})。build(newConfigs()。ramIndexStore()。numOfNode(1));

//等待黄色状态
runner.ensureYellow();
}

@Override
protected void tearDown()throws Exception {
// close runner
runner.close();
//删除所有文件
runner.clean();
}

public void test_jsonParsing()throws异常{
final String index =test_index;

runner.createIndex(index,ImmutableSettings.builder()。build());
runner.ensureYellow(index);

final SearchResponse test = runner.client()。prepareSearch(index).setSource(addQuery())。execute()。actionGet();
}

private String addQuery(){
return{\match_all\:{\boost\:1.2}}
}
}

我创建了 es -plugin.properties (pluginrootdirectory\src\main\resources)文件,其中包含以下内容,将强制弹性搜索实例加载插件:

  plugin = CustomQueryParserPlugin 

当您运行此测试时在输出中会看到新创建的弹性搜索的保护装载插件。


[2015-04-29 19:22:10,783] [INFO] [org.elasticsearch.node] [Node 1]
version [1.5 .0],pid [34360],build [5448160 / 2015-03-23T14:30:58Z]
[2015 -04-29 19:22:10,784] [INFO] [org.elasticsearch.node] [Node 1]
initializin g ... [2015-04-29 19:22:10,795] [INFO
] [org.elasticsearch.plugins] [节点1]加载[ custom_query ],网站[]
[2015-04-29 19:22:13,342] [INFO] [org。弹性搜索.node] [节点1]
已初始化



[2015-04-29 19:22:13 ,342] [INFO] [org.elasticsearch.node] [Node 1]
starting ..。


希望这个帮助。


I create custom Elasticsearch plugin. Now I want to write a test for this plugin. My expectations were - that I could run embedded Elasticsearch instance, set up it properly and then do some testing (index some documents, then query for it)

Problem is that I couldn't set up my plugin properly

Custom plugin code is parsing JSON query and set up some objects for later usage:

public class CustomQueryParserPlugin extends AbstractPlugin {
    public static final String PLUGIN_NAME = "custom_query";
    private final Settings settings;

    @Inject
    public CustomQueryParserPlugin (Settings settings) {
        this.settings = settings;
    }

    @Override
    public String name() {
        return PLUGIN_NAME;
    }

    @Override
    public String description() {
        return "custom plugin";
    }

    public void onModule(IndicesQueriesModule module) {
        module.addQuery(new CustomQueryParser(settings));
    }
}

Test code:

public class CustomParserPluginTest extends ElasticsearchSingleNodeTest {

    private static Node newNode() {
        final Settings settings = ImmutableSettings.builder()
                .put(ClusterName.SETTING, nodeName())
                .put("node.name", nodeName())
                .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
                .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
                .put(EsExecutors.PROCESSORS, 1) // limit the number of threads created
                .put("http.enabled", false)
                .put("plugin.types", CustomParserPlugin.class.getName())
                .put("path.plugins", pathToPlugin)
                .put("index.store.type", "ram")
                .put("config.ignore_system_properties", true) // make sure we get what we set :)
                .put("gateway.type", "none").build();
        Node build = NodeBuilder.nodeBuilder().local(true).data(true).settings(
                settings).build();
        build.start();
        assertThat(DiscoveryNode.localNode(build.settings()), is(true));
        return build;
    }


    @Test
    public void jsonParsing() throws URISyntaxException {
        final Client client = newNode().client();
        final SearchResponse test = client.prepareSearch("test-index").setSource(addQuery()).execute().actionGet();
    }

    private String addQuery() {
         return "{"match_all":{"boost":1.2}}"
    }

I've try multiple values for pathToPlugin - but nothing seems to works well, because JSON query always give me an exception:

QueryParsingException[[test-index] No query registered for [custom_query]];

All documentation I could find was about installing plugins and testing them on some local Elasticsearch installation.

What I am doing wrong here? Is there any documentation or examples of tests like that?

UPD. Here is a repo with extracted code of CustomQueryParserPlugin - https://github.com/MysterionRise/es-custom-parser

May be in initialize section in test I need to create in memory index?

解决方案

To write tests for you plugin you can use Elasticsearch Cluster Runner. For reference check how MinHash Plugin wrote test.

UPDATE:

I've changed CustomParserPluginTest class to use Elasticsearch Cluster Runner:

import static org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner.newConfigs;
import java.util.Map;
import junit.framework.TestCase;
import org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.get.GetField;
import org.junit.Assert;
import org.elasticsearch.action.search.SearchResponse;
import static org.hamcrest.core.Is.is;

public class CustomParserPluginTest extends TestCase {

    private ElasticsearchClusterRunner runner;

    @Override
    protected void setUp() throws Exception {
        // create runner instance
        runner = new ElasticsearchClusterRunner();
        // create ES nodes
        runner.onBuild(new ElasticsearchClusterRunner.Builder() {
            @Override
            public void build(final int number, final Builder settingsBuilder) {
            }
        }).build(newConfigs().ramIndexStore().numOfNode(1));

        // wait for yellow status
        runner.ensureYellow();
    }

    @Override
    protected void tearDown() throws Exception {
        // close runner
        runner.close();
        // delete all files
        runner.clean();
    }

    public void test_jsonParsing() throws Exception {
        final String index = "test_index";

        runner.createIndex(index, ImmutableSettings.builder().build());
        runner.ensureYellow(index);

        final SearchResponse test = runner.client().prepareSearch(index).setSource(addQuery()).execute().actionGet();
    }

    private String addQuery() {
        return "{\"match_all\":{\"boost\":1.2}}";
    }
}

I've created es-plugin.properties(pluginrootdirectory\src\main\resources) file with following content which will force elasticsearch instance to load plugin:

plugin=CustomQueryParserPlugin

When you will run the this test you will see in the output that the newly created insance of elasticsearch loaded the plugin.

[2015-04-29 19:22:10,783][INFO ][org.elasticsearch.node ] [Node 1] version[1.5 .0], pid[34360], build[5448160/2015-03-23T14:30:58Z] [2015-04-29 19:22:10,784][INFO ][org.elasticsearch.node ] [Node 1] initializin g ... [2015-04-29 19:22:10,795][INFO ][org.elasticsearch.plugins] [Node 1] loaded [custom_query], sites [] [2015-04-29 19:22:13,342][INFO ][org.elasticsearch.node ] [Node 1] initialized

[2015-04-29 19:22:13,342][INFO ][org.elasticsearch.node ] [Node 1] starting .. .

Hope this helps.

这篇关于如何为Elasticsearch自定义插件编写测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆