Using Solr from Scala / Play

How to use Solr from Scala / Play? In particular, how can I add / update documents?

+5
source share
4 answers

Update: see my new answer fooobar.com/questions/1128143 / ...


Here is the code I wrote that uses the Play JSON library and Dispatch HTTP client. This is not ideal, 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

}
+5
source

You might want to use the SolrJ Java Lib, which uses the binary protocol to communicate with the Solr server, which works better than using the XML method.

Adding a document to the index is as follows:

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

Hope that helps

Floor

+3
source

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

+1
source

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

0
source

All Articles