Using Solr from Scala/ Play

大憨熊 提交于 2019-12-22 06:50:40

问题


How can use Solr from within Scala/ Play? Specifically how do I add/ update documents?


回答1:


Update: see my newer answer refer https://stackoverflow.com/a/17315047/604511


Here is code I wrote which uses Play's JSON library and Dispatch HTTP client. Its not perfect, but it should help you get started.

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

}



回答2:


You might consider using the SolrJ Java Lib, which uses a binary protocol to communicate with the Solr Server which performs better than using the XML way.

Adding a document to the index is done this:

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

Hope that helps

Paul




回答3:


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/




回答4:


You can integrate Apache Solr with Play Framework in Scala by following the steps inside this article, there is an example.



来源:https://stackoverflow.com/questions/12547581/using-solr-from-scala-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!