Scala、Spray.io、Elastic4s、および ElasticSearch を使用して、小さな REST API を作成しようとしています。私の ES インスタンスはデフォルト パラメータで実行されています。パラメータ network.host を 127.0.0.1 に変更しました。
これが私のスプレールーティングの定義です
package com.example
import akka.actor.Actor
import spray.routing._
import com.example.core.control.CrudController
class ServiceActor extends Actor with Service {
def actorRefFactory = context
def receive = runRoute(routes)
}
trait Service extends HttpService {
val crudController = new CrudController()
val routes = {
path("ads" / IntNumber) {
id =>
get {
ctx =>
ctx.complete(
crudController.getFromElasticSearch
)
}
}
}
}
私のcrudController:
package com.example.core.control
import com.example._
import org.elasticsearch.action.search.SearchResponse
import scala.concurrent._
import scala.util.{Success, Failure}
import ExecutionContext.Implicits.global
class CrudController extends elastic4s
{
def getFromElasticSearch : String = {
val something: Future[SearchResponse] = get
something onComplete {
case Success(p) => println(p)
case Failure(t) => println("An error has occured: " + t)
}
"GET received \n"
}
}
そして、elastic4s への呼び出しをカプセル化している traitelastic4s
package com.example
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
import scala.concurrent._
import org.elasticsearch.action.search.SearchResponse
trait elastic4s {
def get: Future[SearchResponse] = {
val client = ElasticClient.remote("127.0.0.1", 9300)
client execute { search in "ads"->"categories" }
}
}
このコードはうまく動作し、次の出力が得られます。
[INFO] [03/26/2014 11:41:50.957] [on-spray-can-akka.actor.default-dispatcher-4] [akka://on-spray-can/user/IO-HTTP/listener-0] Bound to localhost/127.0.0.1:8080
しかし、ブラウザでルート「localhost/ads/8」にアクセスしようとすると、常にエラーが発生し、intellij コンソールに次のエラー出力が表示されます。
An error has occured: org.elasticsearch.transport.RemoteTransportException: [Skinhead][inet[/127.0.0.1:9300]][search]
(私の端末でelasticSearchを実行してもコンソール出力はありません)
この例外は ElasticSearch に関連していますか、それとも Future 宣言に誤りがありますか?