从 Play 框架异步调用 Solr [英] Call Solr asynchronous from Play Framework
本文介绍了从 Play 框架异步调用 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 框架异步调用 Solr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文