2016-09-27 2 views
0

私が達成したいのは、ElasticSearchを探しているAPIを作成することです。私のプログラミング言語はScalaです。エラーは暗黙の値にできませんでしたToResponseMarshaller [SearchResponse]

は//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() 
    } 

は、私は別のエラーを取得しますか?ご協力いただきありがとうございます!

+0

まず、コードを読む人の認知的負担を軽減するために、コード例として英語を使用することをお勧めします。最初のエラーでは、SprayはSearchResponseをマーシャリングする方法を知らないため、ElasticSearch APIが提供しない場合は、明示的にマーシャルを提供する必要があります。このドキュメントでは、次の手順を理解しておく必要があります。http://spray.io/documentation/1.2.2/spray-httpx/marshalling/最後にエラーが発生しました。 – Tim

+0

大丈夫、赦免@Tim。はい、私はしました。 hehe – SunBright

+0

mr @Tim、私はSunBrightのような同様の問題を抱えていました。何か案が? –

答えて

0

JavaクラスのRootJsonFormatを構築するのは非常に面倒ですが、ここでは1つの結果の例を示します。スコープでインポートする:

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

} 
} 

} 
+0

hello @ EmiCareOfCell44、SunBrightのような問題が発生します。あなたの答えのようなコードを置く必要がありますか?ありがとう! –

+0

はい、SearchResultProtocol._をインポートすれば、コンパイラはシリアライザを見つけることができます。 – EmiCareOfCell44

+0

ここで私はSearchResultProtocol._を見つけることができますか? @ EmiCareOfCell44 –

関連する問題