3

我尝试运行一个elastic4s的示例代码,如下,

import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._

object hw extends App {
  val client = ElasticClient.local
  client.execute(create index "bands")
  client.execute { index into "bands/artists" fields "name"->"coldplay" }.await
  val resp = client.execute { search in "bands/artists" query "coldplay" }.await
  println(resp)
  client.close
}

程序正确打印结果,但它不会自行退出。我不知道我的代码或环境是否有问题。

4

1 回答 1

2

尝试使用shutdown. shutdown实际上将委托给prepareNodesShutdown,这是ClusterAdminClient的一种方法并关闭节点。shutdown没有任何参数将关闭本地节点。

编辑:添加代码和 javadoc 链接

以下确实对我有用,并且在 elastic4s 1.4.0 上按预期工作(即 main 已终止)

import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._

object Main extends App {
  val client = ElasticClient.local
  client.execute(create index "bands")
  client.execute { index into "bands/artists" fields "name"->"coldplay" }.await
  val resp = client.execute { search in "bands/artists" query "coldplay" }.await
  println(resp)
  client.close()
  client.shutdown
}
于 2014-12-23T14:22:33.583 回答