错误无法隐含值ToResponseMarshaller [SearchResponse]

时间:2016-09-27 04:03:45

标签: json scala elasticsearch akka

我想要实现的目标是:创建一个寻找ElasticSearch的API。我的编程语言是Scala。

// myRoute.scala

val pencarianES =
  {
    post
    {
      path("cariES")
      {
        parameters("xQuery", "xNilai")
        {
          (yQuery, yNilai) =>
            val PR = new ProsesRekomendasi
            respondWithMediaType(MediaTypes.`application/json`)
            {
              complete
              {
                PR.ambilDariES(yQuery, yNilai)
              }
            }
        }
      }
    }
  }

// prosesRekomendasi.scala

class ProsesRekomendasi
{
  val ESM = new ESManager
  val CLT = ESM.client

  def ambilDariES(pQuery:String, pNilai:String) =
  {
    CLT.prepareSearch("app_lr_portal_01")
      .setTypes("lr01")
      .setQuery(QueryBuilders.termQuery(s"$pQuery",s"$pNilai"))
      .execute()
      .actionGet()
  }
}

错误是:

could not find implicit value for parameter marshaller:
spray.httpx.marshalling.ToResponseMarshaller[org.eleasticsearch.action.search.SearchResponse]
PR.ambilDariES(yQuery, yNilai)

我正在寻找谷歌,并成立 DefaultMarshallers missing with scala and spray-routing

然后,我按照说明操作:

def ambilDariES(pQuery:String, pNilai:String)(implicit ec:ExecutionContext) =
  {
    CLT.prepareSearch("app_lr_portal_01")
      .setTypes("lr01")
      .setQuery(QueryBuilders.termQuery(s"$pQuery",s"$pNilai"))
      .execute()
      .actionGet()
  }

最后,我得到另一个错误:

Cannot find an implicit ExecutionContext, either import scala.concurrent.ExecutionContext.Implicits.global or use a custom one
                PR.ambilDariES(yQuery, yNilai)

任何想法,如何处理?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

虽然为java类构建RootJsonFormat非常繁琐,但这里只有一个例子。只需导入范围:

object SearchResultProtocol {

   implicit object SearchResulJsonFormatObject extends  RootJsonFormat[SearchResponse] {
     def read(e: JsValue) = null


/* {
 "_shards":{
    "total" : 5,
    "successful" : 5,
    "failed" : 0
 },
"hits":{
    "total" : 1,
    "hits" : [
        {
            "_index" : "twitter",
            "_type" : "tweet",
            "_id" : "1",
            "_source" : {
                "user" : "kimchy",
                "postDate" : "2009-11-15T14:12:12",
                "message" : "trying out Elasticsearch"
            }
        }
    ]
}
}*/

 private def getHits(arr: Array[SearchHit]) : JsArray = { 
   JsArray(arr.map { x => marshallHit(x) }.toVector)
 }  


 private def marshallHit(hit: SearchHit) : JsValue = {
   JsObject(Map("_index" -> JsString(hit.index()),
                "_type" -> JsString(hit.getType),
                "_id" -> JsString(hit.getId),
                "source" -> JsString(hit.getSourceAsString)))
 }

 def write(sr: SearchResponse) = {
   JsObject(Map("_shards" -> 
               JsObject(Map("total" -> JsNumber(sr.totalShards()),
                            "successful" -> JsNumber(sr.getSuccessfulShards()),
                            "failed" -> JsNumber(sr.getFailedShards()))),
                 "hits" -> JsObject(Map("total" -> JsNumber(sr.getHits.totalHits()),
                                         "" -> getHits(sr.getHits.getHits)
                                           ))))

 }
}

}