How to start elasticsearch 5.1 embedded in my java application?

后端 未结 4 1219
轮回少年
轮回少年 2020-12-03 21:41

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

@Bean
public Node elasticSearchTestNode() {
    return NodeBuilder.n         


        
4条回答
  •  没有蜡笔的小新
    2020-12-03 22:19

    The simplest way to make ES5 working is to use Allegro embedded-elasticsearch library (more info in article here). After day of struggling with embedding ES5 on jar level I found it pretty easy. Include into your pom:

    
        pl.allegro.tech
        embedded-elasticsearch
        2.5.0
        test
    
    

    and use following code in your unit test

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

    Test will automatically download Elastic and run it in isolated environment driven from your test at OS process level. http://localhost:21121 proves it works.

    Our application has to interact with different versions of ESs. So this was another reason why this solution was also useful for our case, since we wouldn't otherwise be able to test multiple versions of ESs by adding multiple elasticsearch.jars into classpath.

    Note: if this approach resembles "poor-man's Docker" to you, you can also have a look at TestContainers project. I didn't try it myself though I suppose it would be possible, assuming your testing infrastructure have the use of Docker.

提交回复
热议问题