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

我正在关注本教程


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中有关如何使用它的完整示例。

本文翻译自 https://stackoverflow.com/questions/45839904/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

标签 java scala elasticsearch integration-testing elasticsearch-1.6.0


相关文章:

performance - Elasticsearch索引速度慢

mysql - 当我不使用依赖项“ mysql”%“ mysql-connector-java”%“ 5.1.21”时,我得到“无法连接到数据库[默认]”

ruby - 将弹性搜索限制设置为“无限”

java - 如何获取LibGDX中按住的所有鼠标按钮?

java - java-获取输入星期几的最后日期

scala - EMR Spark无法将Dataframe保存到S3

scala - 如何使用循环在Spark-Scala中迭代HDFS中的多个文本文件?

elasticsearch - Elasticsearch中_source和_all有什么区别

java - 如何在Intellij 13.1.4中的maven多模块项目中重建/生成.iml文件

java - Android中匿名类的InstantiationException问题