API Reference / API Methods / The Scala API client

Algolia’s Scala API client lets you use Algolia’s APIs from your Scala application. Compared to using the APIs directly, using the Scala API client has these benefits:

  • Network retry strategy. The API client automatically retries connecting to another Algolia server, if the connection fails. Thanks to that, using the API clients is covered by Algolia’s SLA.

  • Reindexing. The API clients let you reindex your records in a single operation.

Algolia’s Scala API client is open source. You can find the source code on GitHub.

Install the Scala API client

The Algolia Scala API client requires Scala versions 2.11, 2.12, or 2.13.

Install via Maven

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>

Install with 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"

Test your installation

If you haven’t already, create an Algolia account and create a new Algolia app to get started.

To test whether you can connect to Algolia, run a simple program that adds a record to a new index, searches the index, and print the results.

  1. Create a new Scala object in a file Program.scala. If you’re signed in, the code samples below show your Algolia application ID and API key. If you’re not signed in, replace YourApplicationID with your Algolia application ID and YourAdminAPIKey with your Admin API key. You can find both in your Algolia account.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    
    // Program.scala
    package org.example
    
    import algolia.AlgoliaClient
    import algolia.AlgoliaDsl._
    import algolia.objects.Query
    
    import scala.concurrent.Await._
    import scala.concurrent.ExecutionContext.Implicits.global
    import scala.concurrent.Future
    import scala.concurrent.duration.Duration
    import scala.util.Success
    
    // A simple record for your index
    case class Record(name: String, objectID: String)
    
    object Program extends App {
       // Connect and authenticate with your Algolia app
       val client = new AlgoliaClient("YourApplicationID", "YourAdminAPIKey")
    
       // Create a new index and add a record
       val indexing = client.execute(index into "test_index" `object` Record("test_record", "1"))
       ready(indexing, Duration.Inf)
    
       // Search the index and print the results
       val searchFuture: Future[Seq[Record]] = client
          .execute { search into "test_index" query Query(query = Some("test_record")) }
          .map { search => search.as[Record] }
    
       searchFuture onComplete {
          case Success(results) => println(results(0))
       }
    }
    
  2. Build and run the project. For example, start the interactive sbt tool in your terminal, type compile to build the project, and type run to run the project.

  3. If the program ran successfully, you should see:

    1
    
    Record(test_record, 1)
    

You can inspect your index now in the Algolia dashboard.

Next steps

Now you can interact with the Algolia Search API, you can look at the available methods, for example, for search or indexing.

Other APIs, for example for Algolia Recommend or Analytics, come with their own clients. To get an overview, see Initialize the API client.

Did you find this page helpful?