4

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()
  }
swaheed
  • 3,671
  • 10
  • 42
  • 103

2 Answers2

4

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.

groo
  • 4,213
  • 6
  • 45
  • 69
  • I can not upgrade to ES newer versions right now so is there any work around for the ES version 1.6.0 which currently i am using – swaheed Aug 23 '17 at 13:02
  • yes, but the test class had a different name, please check my updated answer above with a link to their official repo with an example. – groo Aug 23 '17 at 13:25
2

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

Nitish Goyal
  • 97
  • 10