How can use Solr from within Scala/ Play? Specifically how do I add/ update documents?
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
}
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
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With