使用带有弹性搜索和喷涂布线的弹性材料时的例外情况

时间:2014-03-26 11:12:01

标签: scala elasticsearch spray elastic4s

我正在尝试使用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封装了对elastic4s的调用

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声明做错了?

1 个答案:

答案 0 :(得分:1)

我认为在这种情况下你应该使用ElasticClient.local,如elastic4s docs中所述:

https://github.com/sksamuel/elastic4s

To specify settings for the local node you can pass in a settings object like this:

val settings = ImmutableSettings.settingsBuilder()
      .put("http.enabled", false)
      .put("path.home", "/var/elastic/") 
val client = ElasticClient.local(settings.build)