Install the Scala API Client
With Maven, add the following dependency to your pom.xml
file:
1
2
3
4
5
<dependency>
<groupId>com.algolia</groupId>
<artifactId>algoliasearch-scala_2.11</artifactId>
<version>[1,)</version>
</dependency>
For snapshots, add the sonatype
repository:
1
2
3
4
5
6
7
8
9
10
<repositories>
<repository>
<id>oss-sonatype</id>
<name>oss-sonatype</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
If you’re using sbt
, add the following dependency to your build.sbt
file:
1
libraryDependencies += "com.algolia" %% "algoliasearch-scala" % "[1,)"
For snapshots, add the sonatype
repository:
1
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
Supported platforms
The API client only supports Scala 2.11 & 2.12.
Source on GitHub
All our API clients are open source and available on Github.
Language-specific notes
The JVM has an infinite cache on successful DNS resolution. As our hostnames points to multiple IPs, the load could be not evenly spread among our machines, and you might also target a dead machine.
You should change this TTL by setting the property networkaddress.cache.ttl
. For example to set the cache to 60 seconds:
1
java.security.Security.setProperty("networkaddress.cache.ttl", "60");
For debug purposes you can enable debug logging on the API client. It’s using slf4j so it should be compatible with most java loggers.
The logger is named algoliasearch
.
Philosophy
DSL
The main goal of this client is to provide a human-accessible and readable DSL for using AlgoliaSearch.
The entry point of the DSL is the algolia.AlgoliaDSL
object.
This DSL is used in the execute
method of algolia.AlgoliaClient
.
As we want to provide human-readable DSL, there’s more than one way to use this DSL. For example, to get an object by its objectID
:
1
2
3
4
5
client.execute { from index "index" objectId "myId" }
// or
client.execute { get / "index" / "myId" }
Future
The execute
method always returns a scala.concurrent.Future
.
Depending on the operation, it’s parametrized by a case class
. For example:
1
2
3
4
val future: Future[Search] =
client.execute {
search into "index" query "a"
}
JSON as case class
Putting or getting objects from the API is wrapped into case class
automatically with json4s
.
If you want to get objects, search for them and unwrap the result:
1
2
3
4
5
6
7
8
9
10
11
12
13
case class Contact(firstname: String,
lastname: String,
followers: Int,
company: String)
val future: Future[Seq[Contact]] =
client
.execute {
search into "index" query "a"
}
.map { search =>
search.as[Contact]
}
If you want to get the full results (with _highlightResult
, etc.):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
case class EnhanceContact(firstname: String,
lastname: String,
followers: Int,
company: String,
objectID: String,
_highlightResult: Option[Map[String, HighlightResult]
_snippetResult: Option[Map[String, SnippetResult]],
_rankingInfo: Option[RankingInfo]) extends Hit
val future: Future[Seq[EnhanceContact]] =
client
.execute {
search into "index" query "a"
}
.map { search =>
search.asHit[EnhanceContact]
}
For indexing documents, pass an instance of your case class
to the DSL:
1
2
3
client.execute {
index into "contacts" `object` Contact("Jimmie", "Barninger", 93, "California Paint")
}