Home

Awesome

solr-scala-client Scala CI solr-scala-client Scala version support License

The simple Apache Solr client for Scala. This is based on the SolrJ and provides optimal interface for Scala.

Add the following dependency into your build.sbt to use solr-scala-client.

libraryDependencies += "com.github.takezoe" %% "solr-scala-client" % "0.0.27"

If you want to test SNAPSHOT version, add the following dependency instead of above:

resolvers += "sonatype-oss-snapshot" at "https://oss.sonatype.org/content/repositories/snapshots"

libraryDependencies += "com.github.takezoe" %% "solr-scala-client" % "x.x.x-SNAPSHOT"

This is a simplest example to show usage of solr-scala-client.

import com.github.takezoe.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)
}

Query Syntax

Following notations are available to embed variables to the query:

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")

Highlight

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()

Asynchronous API

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.

Release Notes

0.0.27 - 5 Oct 2021

0.0.26 - 14 Sep 2021

0.0.25 - 6 Sep 2021

0.0.24 - 10 Mar 2020

0.0.23 - 24 Feb 2020

0.0.22 - 11 Dec 2019

0.0.21 - 22 Jun 2019

0.0.20 - 13 Jan 2019

0.0.19 - 4 Jun 2018

0.0.18 - 15 Feb 2018

0.0.17 - 5 Dec 2017

0.0.16 - 18 Oct 2017

0.0.15 - 22 Nov 2016

0.0.14 - 14 Aug 2016

0.0.13 - 13 Aug 2016

0.0.12 - 7 Feb 2015

0.0.11 - 29 Mar 2014

0.0.10 - 08 Feb 2014

0.0.9 - 18 Dec 2013

0.0.8 - 2 Aug 2013

0.0.7 - 4 Apr 2013

0.0.6 - 22 Jan 2013

0.0.5 - 20 Nov 2012

0.0.4 - 16 Sep 2012

0.0.3 - 16 Aug 2012

0.0.2 - 27 May 2012

0.0.1 - 4 May 2012