4/29/2012

The simple Apache Solr client for Scala

I pushed solr-scala-client into github. This is a simple Apache Solr client for Scala wrapping SolrJ.

The basic concept of solr-scala-client is providing fluent interface and wrapping SolrJ classes by Scala collection API. This is an example to register documents into the Solr server which is working at localhost:8983.

import jp.sf.amateras.solr.scala._

val client = new SolrClient("http://localhost:8983/solr")

client
  .add(Map("id"->"1", "name" -> "ThinkPad X201s"))
  .add(Map("id"->"2", "name" -> "ThinkPad X220"))
  .add(Map("id"->"3", "name" -> "ThinkPad X121e"))
  .commit

add() takes a variable-length argument. So an example above could be rewritten as follows:

client.add(
  Map("id"->"1", "name" -> "ThinkPad X201s"),
  Map("id"->"2", "name" -> "ThinkPad X220"),
  Map("id"->"3", "name" -> "ThinkPad X121e")
).commit

Next, see the following example to search document using the query.

// query
val result: List[Map[String, Any]] =
  client.query("name:%name%")
    .fields("id", "manu", "name")
    .sortBy("id", Order.asc)
    .getResult(Map("name" -> "ThinkPad"))

result.foreach { doc =>
  println("id: " + doc("id"))
  println("name: " + doc("name"))
  println("--")
}

%VARNAME% in the query is replaced by given parameters as Map and the result is returned as List[Map[String, Any]].

The current version of solr-scala-client does not support facet search yet. I wish to support it in the near future. And I'm also planning about using case class to specify document or parameters instead of Map.

0 件のコメント: