本文介绍了如何启动弹性搜索5.1嵌入我的java应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With elasticsearch 2.x I used the following code to launch an embedded Node for testing:

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

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

This does not compile any more. How can I start an embedded node in 5.x ?

推荐答案

嵌入弹性搜索已不再正式支持,而且还有一点复杂的比2.x,但它的工作。

Embedding elasticsearch is no longer officially supported, and it's a bit more complicated than in 2.x, but it works.

您需要添加一些依赖项:

You need to add some dependencies:

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency><!-- required by elasticsearch -->
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>transport-netty4-client</artifactId>
        <version>5.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency><!-- required by elasticsearch -->
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>

然后启动一个这样的节点:

And then launch a node like this:

@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<Class<? extends Plugin>> classpathPlugins) {
        super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null), classpathPlugins);
    }
}

这篇关于如何启动弹性搜索5.1嵌入我的java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:39