gpt4 book ai didi

scala - 将Elastic4s与 Elasticsearch 和喷雾路由一起使用时异常(exception)

转载 作者:行者123 更新时间:2023-12-02 22:33:37 26 4
gpt4 key购买 nike

我正在尝试使用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声明做错了?

最佳答案

我想您应该在这种情况下使用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)

关于scala - 将Elastic4s与 Elasticsearch 和喷雾路由一起使用时异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22659181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com