我正在学习这个教程
我正在尝试点击链接,因为我使用的是 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 - 如何使用嵌入式 ElasticSearch 进行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45839904/