从Play Framework异步调用Solr [英] Call Solr asynchronous from Play Framework

查看:148
本文介绍了从Play Framework异步调用Solr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Play 2.1 Scala应用程序.我不确定从Play应用程序调用Solr的最佳方法是什么

I have created a Play 2.1 Scala application. I am uncertain what's the best way to call Solr from a Play application:

  • Play 2没有Solr模块.
  • AFAIK像SolrJ一样阻止所有Solr-API.
  • 我可以将SolrJ调用包装到Future中,但这也会阻塞线程,对吗?
  • 我应该使用play.api.libs.ws.WS库来调用Solr并使用Plays JSON支持来提取结果(如下面的示例所示),还是有任何更简便/更快的方法?

  • There is no Solr module for Play 2.
  • AFAIK all Solr-APIs like SolrJ are blocking.
  • I could wrap a SolrJ call into a Future, but this will also block a thread, correct?
  • Should I use the play.api.libs.ws.WS library to call Solr and use Plays JSON support to extract the result (like in the example below) or is there any easier/faster way?

val solrQuery: Future[play.api.libs.ws.Response] = WS.url("http://localhost:8983/solr/collection1/select?q=id%3A123&wt=json").get()

推荐答案

这是我在辅助项目中使用WS的方式:

Here's how I use WS in my side project:

val itselfNodeFuture = Statix.doParams( Statix.SolrSelectWSReq, 
    List(
    "wt"     -> "json", 
    "q"      -> "*:*",
    "fq"     -> "node_type:collection",
    "fq"     -> "id:%d".format( nodeId),
    "indent" -> "true",
    "rows"   -> "1",
    "fl"     -> "id,parent_id,title",
    "fl"     -> "date_created,date_about,date_modified")
).get()

//Use the first Await after the last future
val itselfJson = Await.result(
    itselfNodeFuture, Duration("2 sec")).json

val mainRow = (itselfJson \ "response" \ "docs").as[ Seq[JsValue]]
val mainNodeParent = (mainRow(0) \ "parent_id").as[Long]
val mainNodeTitle = (mainRow(0) \ "title").as[String]

这是我使用的实用程序类,doParams特别有用.

And here's the utility class I use, the doParams is especially useful.

object Statix { //Noder must extend this
    def SolrSelectWSReq = WS.url("http://127.0.0.1:8080/solr-store/collection1/select/")
    def SolrUpdateWSReq = WS.url("http://127.0.0.1:8080/solr-store/collection1/update/json/")

    def doParams(request: WS.WSRequestHolder, params: List[(String, String)]) = {
        params.foldLeft( request){
            (wsReq, tuple) => wsReq.withQueryString( tuple)}}
}

这篇关于从Play Framework异步调用Solr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆