gpt4 book ai didi

scala - 从 Play Framework 异步调用 Solr

转载 作者:行者123 更新时间:2023-12-04 02:52:52 24 4
gpt4 key购买 nike

我已经创建了一个 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/

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