Data providers
The Mapbox Search SDK for Android lets developers to attach their own places, POIs, and address information to SearchEngine
via data providers. Engines will index the provided data and use this data during forward geocoding and category search requests.
Indexable record
IndexableRecord
stores place, POI, and address information that can later be used by both users and the Search SDK. The Search SDK uses information from IndexableRecord
to prepare the data provider engine and construct search results.
IndexableRecord
from SearchResult.indexableRecord
, which is non null if SearchResult
has been constructed from IndexableRecord
:kotlinval searchResult: SearchResult = // ...val record = searchResult?.indexableRecord
Data provider
IndexableDataProvider
has two main responsibilities:
- Store
IndexableRecords
and provide read and write access so the Search SDK is able to retrieve and add records. - Keep the data provider engine up to date to guarantee all indexable records are available during search.
IndexableDataProvider.priority
property.Data provider engine
IndexableDataProviderEngine
is a search index that can be used by multiple search engines during search requests after processing. If you want search engines to be able to search across your data, add all necessary indexable records to the engine during the IndexableDataProvider.registerIndexableDataProviderEngine()
operation or later, when data will become available:
class CustomDataProvider : IndexableDataProvider<CustomIndexableRecord> {
private val dataProviderEngines: MutableList<IndexableDataProviderEngine> = mutableListOf()
private val records: MutableList<IndexableRecord> = mutableListOf()
/**
* Invoked when `CustomDataProvider` is registered in a `SearchEngine`.
*/
override fun registerIndexableDataProviderEngine(
dataProviderEngine: IndexableDataProviderEngine,
executor: Executor,
callback: CompletionCallback<Unit>
): AsyncOperationTask {
synchronized(this) {
dataProviderEngines.add(dataProviderEngine)
dataProviderEngine.upsertAll(records)
}
executor.execute {
callback.onComplete(Unit)
}
return CompletedAsyncOperationTask
}
/**
* Invoked when search records become available.
*/
private fun onDataLoaded(customData: List<CustomIndexableRecord>) {
synchronized(this) {
records.addAll(customData)
dataProviderEngines.forEach { engine ->
engine.upsertAll(records)
}
}
}
// ...
private object CompletedAsyncOperationTask : AsyncOperationTask {
override val isDone: Boolean
get() = true
override val isCancelled: Boolean
get() = false
override fun cancel() {
// Do nothing
}
}
}
IndexableDataProviderEngine
is thread-safe and can be called from different threads.
Built-in data providers
The Mapbox Search SDK for Android has built-in data providers that are attached to each instance of the SearchEngine
created with SearchEngine.createSearchEngineWithBuiltInDataProviders(). If an instance of the SearchEngine
is instantiated with SearchEngine.createSearchEngine(), it will not have any data providers registered by default.
The Search SDK provides two built-in data providers:
Search history
- data provider which is designed to store the most recent search results selected on a device.Favorites
- data provider which is designed to store user's favorite places, addresses, POIs.
You will learn more about these data providers later in this guide.
Custom data providers
The Mapbox Search SDK for Android allows you create your own data providers if you want to introduce custom POIs or addresses to the search engine during the search.
Search across POIs provided by custom data provider
First, you should create a record class that will store all the data about places, POIs, and addresses that you have. Implement an IndexableRecord
interface. We recommend using a Kotlin data class
and @Parcelize
annotation from the kotlin-parcelize
plugin to ease implementation:
@Parcelize
data class CustomRecord(
override val id: String,
override val name: String,
override val coordinate: Point,
override val type: SearchResultType,
override val descriptionText: String?,
override val address: SearchAddress?,
override val routablePoints: List<RoutablePoint>?,
override val categories: List<String>?,
override val makiIcon: String?,
override val metadata: SearchResultMetadata?,
override val indexTokens: List<String>,
) : IndexableRecord
Then, implement an IndexableDataProvider
interface that will be used as a source of custom records and will update the search engine index associated with the provided IndexableDataProviderEngine
instance.
IndexableDataProviderEngine
. Make sure you've provided your data to the data provider engine once data is available. Read more about data provider engines in data provider engine section.And finally, register custom data provider in your SearchEngine
:
val searchEngine: SearchEngine = //...
val customDataProvider: CustomDataProvider = // ...
val task = searchEngine.registerDataProvider(
customDataProvider,
callback = object : CompletionCallback<Unit> {
override fun onComplete(result: Unit) {
}
override fun onError(e: Exception) {
}
}
)
SearchEngine
:kotlinval task = searchEngine.unregisterDataProvider( customDataProvider, callback = object : CompletionCallback<Unit> { override fun onComplete(result: Unit) { } override fun onError(e: Exception) { } })
Indexable records exclusion
Indexable records can be excluded from a search response by modifying SearchOptions.ignoreIndexableRecords and CategorySearchOptions.ignoreIndexableRecords, for example,
val searchOption = SearchOptions(ignoreIndexableRecords = true)
val categorySearchOptions = CategorySearchOptions(ignoreIndexableRecords = true)