java - 如何使用嵌入式ElasticSearch进行集成测试

原文 标签 java scala elasticsearch integration-testing elasticsearch-1.6.0

我正在关注本教程


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


我正在尝试按照该链接进行操作,因为我正在使用ELasticSearch 1.6.0,但是它正在使用NodeClient,并且我想使用transportClient,但是却遇到异常

 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)


这是代码

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()
  }

最佳答案

首先,您需要记住,新版本的Elasticsearch已放弃了对其进行嵌入使用的支持,原因在this blog post中进行了描述。

在许多版本的Elasticsearch上,包括elastic 5,您可以使用ESIntegTestCase支持类,并使您的集成测试类对其进行扩展,以在测试中支持嵌入式Elasticsearch。您可以在their official documentation上阅读有关此内容的更多信息。

从他们的官方文档:

public class Mytests extends ESIntegTestCase {

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

}


在版本1.x中,有一个名为ElasticsearchTestCase的类,该类将支持使用Elastic进行单元测试,请查看this link on their official repository中有关如何使用它的完整示例。

相关文章:

java - 编译pom文件以使用Eclipse下载依赖项

java - 根据索引值排序列表

java - JTree的所有节点中的JCheckBox

scala - 带有akka的蛋糕模式:为多个图层提供隐式actorSystem

scala - IO和Future [Option] monad变压器

elasticsearch - 没有在字段上声明类型[join]的处理程序

elasticsearch - 如何在Timelion中指定索引和日期/时间字段?

java - Java-库路径错误

scala - Spark udf初始化

c# - 跨多种类型的ElasticSearch NEST查询