如何为 Elasticsearch 自定义插件编写测试?
How to write a test for Elasticsearch custom plugin?
我创建自定义 Elasticsearch 插件。现在我想为这个插件写一个测试。
我的期望是 - 我可以 运行 嵌入 Elasticsearch 实例,正确设置它然后做一些测试(索引一些文档,然后查询它)
问题是我无法正确设置我的插件
自定义插件代码正在解析 JSON 查询并设置一些对象供以后使用:
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));
}
}
测试代码:
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}}"
}
我已经为 pathToPlugin 尝试了多个值 - 但似乎没有任何效果,因为 JSON 查询总是给我一个例外:
QueryParsingException[[test-index] No query registered for [custom_query]];
我能找到的所有文档都是关于安装插件并在某些本地 Elasticsearch 安装上测试它们的。
我这里做错了什么?有这样的文档或测试示例吗?
UPD。这是一个带有 CustomQueryParserPlugin 提取代码的回购 - https://github.com/MysterionRise/es-custom-parser
可能在测试的初始化部分我需要在内存中创建索引?
要为您的插件编写测试,您可以使用 Elasticsearch Cluster Runner。
作为参考检查 MinHash Plugin 是如何编写测试的。
更新:
我已将 CustomParserPluginTest
class 更改为使用 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}}";
}
}
我创建了 es-plugin.properties
(pluginrootdirectory\src\main\resources) 文件,其中包含以下内容,这将强制 elasticsearch 实例加载插件:
plugin=CustomQueryParserPlugin
当您运行这个测试时,您将在输出中看到新创建的 elasticsearch 实例加载了插件。
[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 .. .
希望对您有所帮助。
我创建自定义 Elasticsearch 插件。现在我想为这个插件写一个测试。 我的期望是 - 我可以 运行 嵌入 Elasticsearch 实例,正确设置它然后做一些测试(索引一些文档,然后查询它)
问题是我无法正确设置我的插件
自定义插件代码正在解析 JSON 查询并设置一些对象供以后使用:
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));
}
}
测试代码:
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}}"
}
我已经为 pathToPlugin 尝试了多个值 - 但似乎没有任何效果,因为 JSON 查询总是给我一个例外:
QueryParsingException[[test-index] No query registered for [custom_query]];
我能找到的所有文档都是关于安装插件并在某些本地 Elasticsearch 安装上测试它们的。
我这里做错了什么?有这样的文档或测试示例吗?
UPD。这是一个带有 CustomQueryParserPlugin 提取代码的回购 - https://github.com/MysterionRise/es-custom-parser
可能在测试的初始化部分我需要在内存中创建索引?
要为您的插件编写测试,您可以使用 Elasticsearch Cluster Runner。 作为参考检查 MinHash Plugin 是如何编写测试的。
更新:
我已将 CustomParserPluginTest
class 更改为使用 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}}";
}
}
我创建了 es-plugin.properties
(pluginrootdirectory\src\main\resources) 文件,其中包含以下内容,这将强制 elasticsearch 实例加载插件:
plugin=CustomQueryParserPlugin
当您运行这个测试时,您将在输出中看到新创建的 elasticsearch 实例加载了插件。
[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 .. .
希望对您有所帮助。