2014-03-26 35 views
2

我正在嘗試使用Scala,Spray.io,Elastic4s和ElasticSearch編寫一個小REST api。 我的ES實例正在使用默認參數運行,我只是將參數network.host更改爲127.0.0.1。使用elastic4s彈性搜索和噴霧路由的例外

這是我噴路由定義

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 

但是,當嘗試進入的路線「本地主機/廣告/ 8」與我的瀏覽器的情況下故障始終觸發,我得到了我的IntelliJ控制檯上的錯誤輸出:

An error has occured: org.elasticsearch.transport.RemoteTransportException: [Skinhead][inet[/127.0.0.1:9300]][search] 

(沒有控制檯輸出與我的終端上運行elasticSearch)

是這個異常與ElasticSearch相關,或者我做錯了我的未來宣言?

+0

是正確使用 '遠程' 方法本地連接? – Ashalynd

回答

1

我想你應該在這種情況下使用ElasticClient.local,如elastic4s文件規定:

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) 
+0

這是問題所在。謝謝:) - – ylos

+0

不客氣:) – Ashalynd