Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Solr from Scala/ Play

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

like image 772
Jesvin Jose Avatar asked Sep 22 '12 20:09

Jesvin Jose


3 Answers

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

}
like image 85
Jesvin Jose Avatar answered Nov 04 '22 07:11

Jesvin Jose


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

like image 3
earthling paul Avatar answered Nov 04 '22 08:11

earthling paul


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/

like image 1
ngeek Avatar answered Nov 04 '22 09:11

ngeek