Install the Scala API client

Install

With Maven, add the following dependency to your pom.xml file:

<dependency>
    <groupId>com.algolia</groupId>
    <artifactId>algoliasearch-scala_2.11</artifactId>
    <version>[1,)</version>
</dependency>

For snapshots, add the sonatype repository:

<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:

libraryDependencies += "com.algolia" %% "algoliasearch-scala" % "[1,)"

For snapshots, add the sonatype repository:

resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

Supported platforms

This API client only supports Scala 2.11 & 2.12.

Language-specific notes

WARNING: 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:

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 Algolia search.

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 is more than one way to use this DSL. For example, to get an object by its objectID:

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 will be parametrized by a case class. For example:

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 by json4s.

If you want to get objects just search for it and unwrap the result:

case class Contact(firstname: String,
                   lastname: String,
                   followers: Int,
                   compagny: 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.):

case class EnhanceContact(firstname: String,
                          lastname: String,
                          followers: Int,
                          compagny: 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, just pass an instance of your case class to the DSL:

client.execute {
    index into "contacts" `object` Contact("Jimmie", "Barninger", 93, "California Paint")
}

Init Index

To begin, you will need to initialize the client. In order to do this you will need your Application ID and API Key. You can find both on your Algolia account.

// No initIndex
val client = new AlgoliaClient("YourApplicationID", "YourAPIKey")

You need to replace your_index_name by the name of the index you want to use. If you want to target an existing index you can find the name from the dashboard. If the index does not exist you can choose any name and it will be created when you perform an add objects or a set settings operation.

If an api key is displayed in the previous snippet it is your ADMIN API Key. To maintain security, never use your ADMIN API Key on your frontend or share it with anyone. In your frontend, use the SEARCH ONLY API Key or any other key that has search only rights.

Make sure you don’t use any sensitive or personally identifiable information (PII) as your index name, including customer names, user IDs, or email addresses. Index names appear in network requests and should be considered publicly available.