The perfect starting point to integrate Algolia within your Kotlin project
Documentation • Community Forum • Stack Overflow • Report a bug • FAQ • Support
✨
Features
- The Kotlin client is compatible with Kotlin
1.4.0
and higher. - It is compatible with Kotlin project on the JVM, such as backend and Android applications.
- It relies on the open source Kotlin libraries for seamless integration into Kotlin projects:
- Kotlin multiplatform.
- Kotlinx serialization for json parsing.
- Kotlinx coroutines for asynchronous operations.
- Ktor HTTP client.
- The Kotlin client integrates the actual Algolia documentation in each source file: Request parameters, response fields, methods and concepts; all are documented and link to the corresponding url of the Algolia doc website.
- The client is thread-safe. You can use
SearchClient
,AnalyticsClient
, andInsightsClient
in a multithreaded environment.
💡
Getting Started
Install the Kotlin client by adding the following dependency to your gradle.build
file:
repositories {
mavenCentral()
}
dependencies {
// Search API Client
implementation "com.algolia:algoliasearch-client-kotlin:$kotlin_client_version"
// alternately - for Gradle version < 6.0, use the following
// implementation "com.algolia:algoliasearch-client-kotlin-jvm:$kotlin_client_version"
// Choose one of the following http client
implementation "io.ktor:ktor-client-apache:$ktor_version"
implementation "io.ktor:ktor-client-okhttp:$ktor_version"
implementation "io.ktor:ktor-client-android:$ktor_version"
implementation "io.ktor:ktor-client-cio:$ktor_version"
implementation "io.ktor:ktor-client-jetty:$ktor_version"
}
For full documentation, visit the Algolia Kotlin API Client.
1.4.0
the library is compatible only with kotlin version 1.3.70
or higher; for previous versions of kotlin, please use version 1.3.1
of the library.
1.4.x
or below to the latest version.
Coroutines
All methods performing HTTP calls in the Kotlin client are suspending functions. This means these functions can only be called from a coroutine.
In the example below, a coroutine is launched in the main thread. The context is switched to a thread pool to perform the search HTTP call off the main thread. The response can be manipulated from the main thread.
class Searcher : CoroutineScope {
override val coroutineContext = Job()
fun search() {
launch(Dispatchers.Main) {
val response = withContext(Dispatchers.Default) { index.search() }
}
}
}
The developer is responsible for implementing the asynchronous logic that matches their specific requirements. The Kotlin client doesn't execute HTTP calls on any particular thread, it is up to the developer to define it explicitly using coroutines. Learn more about coroutines.
Waiting for operations
Waiting for an asynchronous server task is made available via a function literal with receiver.
Use the apply or run functions on your index or client.
index.apply {
setSettings(Settings()).wait()
}
client.run {
multipleBatchObjects(listOf<BatchOperationIndex>()).waitAll()
}
The wait
functions are suspending, and should only be called from a coroutine.
Type safety
Response and parameters objects are typed to provide extensive compile time safety coverage.
Example for creating a Client instance without mixing the application ID and the API key.
val appID = ApplicationID("YourApplicationID")
val apiKey = APIKey("YourAdminAPIKey")
val client = ClientSearch(appID, apiKey)
Example for attributes:
val color = Attribute("color")
val category = Attribute("category")
Query(
attributesToRetrieve = listOf(color, category)
)
Sealed class are used to represent enumerated types. It allows to quickly discover possible values for each type thanks to IDE autocomplete support. All sealed class have an Other
class case for avoiding runtime crashes in case of unforeseen value.
val query = Query()
query.sortFacetsBy = SortFacetsBy.Count
// query.sortFacetsBy = SortFacetsBy.Alpha
// query.sortFacetsBy = SortFacetsBy.Other("unforeseen value")
R8 / Proguard rules
If you use this library in an Android project which uses R8, there is nothing you have to do. The specific rules are already bundled into the JAR, which can be interpreted by R8 automatically.
If however, you don’t use R8 you have to apply the rules from this file.
Guides
❓
Troubleshooting
Encountering an issue? Before reaching out to support, we recommend heading to our FAQ where you will find answers for the most common issues and gotchas with the client.
📄
License
Algolia Kotlin API Client is an open-sourced software licensed under the MIT license.