scala - 从 Play Framework 异步调用 Solr

标签 scala solr playframework playframework-2.1 solrj

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

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

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

最佳答案

以下是我在副项目中使用 WS 的方式:

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 特别有用。

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

关于scala - 从 Play Framework 异步调用 Solr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270393/

相关文章:

scala - 是否有 EnumSet/EnumMap 的 Scala 等价物?

php - 通过 PHP cURL 将文档添加到 Apache Solr

scala - Scala 函数中的参数列表。有人可以解释一下代码吗?

PlayFramework 的 Nginx 代理从端口到路径前缀

用于分组的 Scala 集合,同时保持顺序

list - 列表的::: 和++ 之间有什么区别?

scala flatMap 展平嵌套列表

solr - 在 solr dih 中,在一个位置导入两个双倍

java - 如何使用 spring data solr 在 solr 中插入嵌套文档?

java - 使用 Play Framework 的博客 Web 应用程序出现逻辑错误