The simple Apache Solr client for Scala. This is based on the SolrJ and provides optimal interface for Scala.
This project has been built continuously by BuildHive.
Add the following dependency into your build.sbt to use solr-scala-client.
resolvers += "amateras-repo" at "http://amateras.sourceforge.jp/mvn/"
libraryDependencies += "jp.sf.amateras.solr.scala" %% "solr-scala-client" % "0.0.12"
This is a simplest example to show usage of solr-scala-client.
import jp.sf.amateras.solr.scala._
val client = new SolrClient("http://localhost:8983/solr")
// register
client
.add(Map("id"->"001", "manu" -> "Lenovo", "name" -> "ThinkPad X201s"))
.add(Map("id"->"002", "manu" -> "Lenovo", "name" -> "ThinkPad X220"))
.add(Map("id"->"003", "manu" -> "Lenovo", "name" -> "ThinkPad X121e"))
.commit
// query
val result = client.query("name: %name%")
.fields("id", "manu", "name")
.sortBy("id", Order.asc)
.getResultAsMap(Map("name" -> "ThinkPad"))
result.documents.foreach { doc: Map[String, Any] =>
println("id: " + doc("id"))
println(" manu: " + doc("manu"))
println(" name: " + doc("name"))
}
It's also possible to use the case class as the search result and parameters instead of Map.
// query
val result = client.query("name: %name%")
.fields("id", "manu", "name")
.sortBy("id", Order.asc)
.getResultAs[Product](Param(name = "ThinkPad"))
result.documents.foreach { product: Product =>
println("id: " + product.id)
println(" manu: " + product.manu)
println(" name: " + product.name)
}
Following notations are available to embed variables to the query:
- %VAR_NAME% : place holder to set a single word (parameter would be escaped)
- ?VAR_NAME? : place holder to set an expression (&, | and ! are available in an expression)
-
$VAR_NAME$ : string replacement (parameter would be not escaped)
See examples of parameterized queries and assembled Solr queries.
// %VAR_NAME% (Single keyword)
client.query("name: %name%").getResultAsMap(Map("name" -> "ThinkPad X201s"))
// => name:"ThinkPad X201s"
// $VAR_NAME$ (String replacement)
client.query("name: $name$").getResultAsMap(Map("name" -> "ThinkPad AND X201s"))
// => name:ThinkPad AND X201s
// ?VAR_NAME? (Expression)
client.query("name: ?name?").getResultAsMap(Map("name" -> "ThinkPad & X201s"))
// => name:("ThinkPad" AND "X201s")
Configure the query to return the highlighted content by QueryBuilder#highlight()
.
The highlighted content is set as the "highlight" property to the Map or the case class.
val result = client.query("content: Scala")
// NOTE: unique key field is required.
.fields("id")
// Specify the highlighted field, prefix and postfix (prefix and postfix are optional).
.highlight("content", "<strong>", "</strong>")
.getResultAsMap()
result.documents.foreach { doc: Map[String, Any] =>
println("id: " + doc("id"))
println(doc("highlight")) // highlighted content is set as the "highlight" property
}
solr-scala-client expects that the unique key is "id". If your schema has the different field as the unique key, you can specify the unique key name as following:
val result = client.query("content: Scala")
.id("documentId") // Specify the unique key name
.fields("documentId")
.highlight("content", "<strong>", "</strong>")
.getResultAsMap()
solr-scala-client has also asynchronous API based on AsyncHttpCleint.
val client = new AsyncSolrClient("http://localhost:8983/solr")
// Register
client
.register(Map("id" -> "005", "name" -> "ThinkPad X1 Carbon", "manu" -> "Lenovo"))
.onComplete{
case Success(x) => println("registered!")
case Failure(t) => t.printStackTrace()
}
// Query
client.query("name:%name%")
.fields("id", "manu", "name")
.facetFields("manu")
.sortBy("id", Order.asc)
.getResultAsMap(Map("name" -> "ThinkPad X201s"))
.onComplete {
case Success(x) => println(x)
case Failure(t) => t.printStackTrace()
}
See more example at AsyncSolrClientSample.scala.
- Add
QueryBuilderBase#fq()
- Add
QueryBuilderBase#setRequestHandler()
- Add date facet
- Support for streaming results
- Add
SolrClient#shutdown()
QueryBuilder
became immutable- Upgrade solrj version to 4.5.1
- Fix escaping in string literal.
- Bug fix
- Added recommendation search support.
- Added
rollback
andwithTransaction
toSolrScalaClient
. - Added Asynchronous API.
- Add build for Scala 2.10
- Upgrade to SolrJ 4.2.0
- Support highlighting
- Fixed some
ExpressionParser
bugs.
ExpressionParser
became pluggable and addedGoogleExpressionParser
as an optional implementation ofExpressionParser
.- Converts the full-width space to the half-width space in the expression before calling ExpressionParser.
- Introduced the SolrServer factory.
Auth.basic
moved toSolrServerFactory.basicAuth
andSolrServerFactory.dummy
for unit testing.
- Expanding expression to the Solr query by
?VAR_NAME?
inSolrClient#query()
. - Bug fix
- Added case class support in update operations.
- Added
commit()
method toSolrClient
.
- Added initializer which configures
SolrClient
. - Added basic authentication support as initializer.
- Added facet search support.
- Added case class support as query results and query parameters.
- Initial public release.