sarah w
sarah w

Reputation: 3485

how to use embedded ElasticSearch for integration testing

I am following this tutorials

  1. https://orrsella.com/2014/10/28/embedded-elasticsearch-server-for-scala-integration-tests/

All I am trying to follow the link because I am using ELasticSearch 1.6.0 but it's using NodeClient and I want to use transportClient but I am getting exception

 org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
[info]   at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:305)
[info]   at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:200)
[info]   at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.java:86)
[info]   at org.elasticsearch.client.support.AbstractIndicesAdminClient.create(AbstractIndicesAdminClient.java:367)
[info]   at org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder.doExecute(CreateIndexRequestBuilder.java:250)
[info]   at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
[info]   at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
[info]   at testcontrollers.campaign.ElasticsearchServer.createAndWaitForIndex(ElasticsearchServer.scala:40)
[info]   at testcontrollers.campaign.CampaignTestSearch.<init>(CampaignTestSearch.scala:41)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

here is the code

class ElasticsearchServer {
  val log = LoggerFactory.getLogger(this.getClass)
  private val clusterName = "testcluster"
  private val dataDir = Files.createTempDirectory("elasticsearch_test_data_").toFile
  private val settings = ImmutableSettings.settingsBuilder
    .put("path.data", dataDir.toString)
    .put("cluster.name", clusterName)
    .put("node.local",true)
    .build

  def client: TransportClient ={
    val client=new TransportClient(settings)
    .addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300))
   client
  }
  def stop(): Unit = {
    client.close();

    try {
      FileUtils.forceDelete(dataDir)
    } catch {
      case e: Exception => // dataDir cleanup failed
    }
  }

    def createAndWaitForIndex(index: String): Unit = {
      client.admin.indices.prepareCreate(index).execute.actionGet()
      client.admin.cluster.prepareHealth(index).setWaitForActiveShards(1).execute.actionGet()
    }
}

class CampaignTestSearch extends PlaySpec{
  val server = new ElasticsearchServer
  server.createAndWaitForIndex("arteciatetestdb")
val client=server.client

val response = client.prepareSearch("dbtest")
      .setTypes(CAMPAIGN_COLLECTION_NAME)
      .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
      .addFields("uuid","campaignName","artworkID","activationDate","_source")
      .setQuery(query)
      .execute()
      .actionGet()
  }

Upvotes: 5

Views: 9832

Answers (2)

groo
groo

Reputation: 4448

First you need to keep in mind that newer versions of elasticsearch have dropped support to use it embedded, reasons for that are described in this blog post.

On many versions of Elasticsearch, including elastic 5, you could use ESIntegTestCase support class and make your integration test class extend it in order to have support of embedded Elasticsearch on tests. You can read more about this at their official documentation.

From their official docs:

public class Mytests extends ESIntegTestCase {

  @Override
  protected Settings nodeSettings(int nodeOrdinal) {
      return Settings.builder().put(super.nodeSettings(nodeOrdinal))
             .put("node.mode", "network")
             .build();
  }

}

In version 1.x there was a class named ElasticsearchTestCase that would give support for unit testing with elastic, check this link on their official repository for a full example on how to use it.

Upvotes: 4

Nitish Goyal
Nitish Goyal

Reputation: 107

Embedded elasticsearch is not supported anymore

You can use this maven dependency, it will start elasticsearch 6 cluster for you

<dependency>
    <groupId>org.elasticsearch-6</groupId>
    <artifactId>elasticsearch-embedded-cluster</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

You can read more details on https://github.com/nitishgoyal13/elasticsearch-6-embedded-cluster

Upvotes: 2

Related Questions