5

如何在 Scala/Play 中使用 Solr?具体如何添加/更新文档?

4

3 回答 3

5

更新:请参阅我的更新答案参考https://stackoverflow.com/a/17315047/604511


这是我使用 Play 的 JSON 库和 Dispatch HTTP 客户端编写的代码。它并不完美,但它应该可以帮助您入门。

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.json.Json
import play.api.libs.json.Json.toJson
import dispatch._

object Application extends Controller {

    def index = Action {
        val addDocument = Json.toJson(
        Map(
            "add" -> 
                Seq(
                //a document
                Map(
                "id"      -> toJson("123"),
                "subject" -> toJson("you have been served")
                )
            )
        ))
        val toSend  = Json.stringify( addDocument)
        val params  = Map( "commit" -> "true", "wt" -> "json")
        val headers = Map( "Content-type" -> "application/json")

        val solr = host( "127.0.0.1", 8983)
        val req  = solr / "solr" / "update" / "json" <<?
            params <:< headers << toSend

        val response = Http(req)()
        Ok( toSend + response.getResponseBody)
    //Redirect(routes.Application.tasks)
    }

    def tasks = TODO
    def newTask = TODO
    def deleteTask(id: Long) = TODO

}
于 2012-09-22T20:46:07.977 回答
3

您可能会考虑使用 SolrJ Java Lib,它使用二进制协议与 Solr 服务器通信,它比使用 XML 方式执行得更好。

将文档添加到索引是这样做的:

http://wiki.apache.org/solr/Solrj#Adding_Data_to_Solr

希望有帮助

保罗

于 2012-09-23T19:06:02.013 回答
1

Not directly related to updating documents, but a nice query DSL for Solr in Scala build by foursquare is described in their engineering blog article: http://engineering.foursquare.com/2011/08/29/slashem-a-rogue-like-type-safe-scala-dsl-for-querying-solr/

于 2012-11-28T22:38:58.120 回答