如何在我的java应用程序中嵌入elasticsearch 5.1?

使用elasticsearch 2.x,我使用以下代码启动嵌入式节点进行测试:

@Bean public Node elasticSearchTestNode() { return NodeBuilder.nodeBuilder() .settings(Settings.settingsBuilder() .put("http.enabled", "true") .put("path.home", "elasticsearch-data") .build()) .node(); } 

这不再编译。 如何在5.x中启动嵌入式节点?

嵌入弹性搜索不再是官方支持的,它比2.x更复杂,但它有效。

您需要添加一些依赖项:

   org.elasticsearch elasticsearch 5.1.1 test   org.elasticsearch.plugin transport-netty4-client 5.1.1 test   org.apache.logging.log4j log4j-api 2.7  

然后启动这样的节点:

 @Bean public Node elasticSearchTestNode() throws NodeValidationException { Node node = new MyNode( Settings.builder() .put("transport.type", "netty4") .put("http.type", "netty4") .put("http.enabled", "true") .put("path.home", "elasticsearch-data") .build(), asList(Netty4Plugin.class)); node.start(); return node; } private static class MyNode extends Node { public MyNode(Settings preparedSettings, Collection> classpathPlugins) { super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null), classpathPlugins); } } 

使ES5工作的最简单方法是使用Allegro embedded-elasticsearch库 ( 此处有更多信息)。 经过一天努力将ES5嵌入jar级别后,我发现它非常简单。 包括在你的pom中:

  pl.allegro.tech embedded-elasticsearch 2.5.0 test  

并在unit testing中使用以下代码

 EmbeddedElastic embeddedElastic = EmbeddedElastic.builder() .withElasticVersion("5.5.2") .withSetting(PopularProperties.HTTP_PORT, 21121) .build(); embeddedElastic.start(); 

测试将自动下载Elastic并在操作系统进程级别的测试驱动的隔离环境中运行。 http://localhost:21121certificate它有效。

我们的应用程序必须与不同版本的ES交互。 所以这也是为什么这个解决方案对我们的情况也很有用的另一个原因,因为我们不能通过在classpath中添加多个elasticsearch.jars来测试多个版本的ES。

注意:如果这种方法类似于“穷人的Docker”,您还可以查看TestContainers项目。 虽然我认为你的测试基础设施可以使用Docker,但我认为它是可能的,我没有尝试过。

它不受支持。

你应该阅读这篇博文 。

编辑:

这就是我用maven问题解决集成测试的方法。

不再支持嵌入式弹性搜索

您可以使用此maven依赖项,它将为您启动elasticsearch 6集群

  org.elasticsearch-6 elasticsearch-embedded-cluster 1.0-SNAPSHOT  

您可以在https://github.com/nitishgoyal13/elasticsearch-6-embedded-cluster上阅读更多详细信息。