The perfect starting point to integrate Algolia within your Kotlin project
Documentation •
Community Forum •
Stack Overflow •
Report a bug •
Support
✨ Features
💡 Getting Started
Install the Kotlin client by adding the following dependency to your gradle.build file:
repositories {
mavenCentral()
}
dependencies {
implementation "com.algolia:algoliasearch-client-kotlin:$version"
}
Also, choose and add to your dependencies one of Ktor http client engines.
Alternatively, you can use algoliasearch-client-kotlin-bom.
For full documentation, visit the Algolia Kotlin API Client.
Optional Kotlin DSL
The client includes an optional Kotlin DSL to build search parameters, index settings, rules, and filters. The DSL is experimental: opt in with @OptIn(AlgoliaExperimentalDsl::class). It builds the same models as the data-class constructors, which remain fully supported.
[!WARNING]
The DSL isn't source compatible with version 2. Many names are the same, but some behaviors changed. See Migrating from version 2.
Search parameters and filters
import com.algolia.client.dsl.*
@OptIn(AlgoliaExperimentalDsl::class)
val response = client.searchSingleIndex("products") {
query = "shoes"
hitsPerPage = 20
filters {
orFacet { facet("brand", "Acme"); facet("brand", "Globex") }
facet("category", , isNegated = )
}
optionalFilters {
or { facet(, , score = ); facet(, ) }
}
}
query { } builds a SearchParamsObject without sending it. browse { } does the same for client.browse:
@OptIn(AlgoliaExperimentalDsl::class)
client.browse("products", browse { query = "shoes"; filters { facet("brand", "Acme") } })
List parameters take + entries. A block replaces the list, and an empty block sends []:
import com.algolia.client.dsl.*
import com.algolia.client.model.search.SupportedLanguage
@OptIn(AlgoliaExperimentalDsl::class)
val params = query {
restrictSearchableAttributes { +"title"; +"description" }
queryLanguages { +SupportedLanguage.En }
}
Deleting records by filter
client.deleteBy(indexName) { filters { … } } deletes the matching records. Unlike search, the delete DSL never widens its scope: it throws IllegalArgumentException before sending the request if a filter block or group is empty, or if there's no filter or geo condition at all.
Index settings and rules
import com.algolia.client.dsl.*
import com.algolia.client.dsl.rule.*
@OptIn(AlgoliaExperimentalDsl::class)
val indexSettings = settings {
searchableAttributes {
ordered("name")
unordered("description")
}
}
@OptIn(AlgoliaExperimentalDsl::class)
val promo = rule("promo") {
consequence {
promote { objectID("featured-1", position = 0) }
hide { +"discontinued-1" }
}
}
A rule requires consequence.
Reusable fragments
Store fragments as lambdas on the DSL receivers and apply them anywhere:
import com.algolia.client.dsl.*
import com.algolia.client.dsl.filter.*
@OptIn(AlgoliaExperimentalDsl::class)
val locale: DSLFilters.() -> Unit = { orFacet { facet("locale", "en-US") } }
@OptIn(AlgoliaExperimentalDsl::class)
val base: DSLQuery.() -> Unit = { hitsPerPage = 20; filters(locale) }
The receivers are DSLQuery, DSLBrowse, DSLDeleteBy, DSLSettings, DSLQueryAdditions, DSLDeleteByAdditions, , and . Some are typealiases of generated classes, so Java code shows the generated names, such as .
Composing queries
DSLQueryComposer builds one SearchParamsObject from fragments contributed by different parts of your code:
@OptIn(AlgoliaExperimentalDsl::class)
val composer = DSLQueryComposer(base = { hitsPerPage = 20 })
composer.add { filters { facet("brand", "Acme") } }
composer.add { ruleContexts { +"mobile" } }
composer.override { sumOrFiltersScores = true }
client.searchSingleIndex(indexName, composer.build())
Migrating from version 2
DSLFilters, DSLFacetFilters, the filter and group types, and the list and settings receivers keep their version 2 names. DSLConditions, DSLPromotions, and DSLObjectIDs keep their names but have different members.
Behavior changes
These compile but can change what's sent to the engine:
See the Kotlin upgrade guide.
❓ 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.
Use the Dockerfile
If you want to contribute to this project without installing all its dependencies, you can use our Docker image. Please check our dedicated guide to learn more.
📄 License
The Algolia Kotlin API Client is an open-sourced software licensed under the MIT license.