I'm working on a personal Android application in Kotlin, and I want to implement an InstantSearch. I have the following record construct in Algolia:
{
"creationTime": 1566396861797,
"description": "It's my second home now...",
"imagePath": "https://firebasestorage.googleapis.com/.......",
"memoryId": "-LmoWe8PEqCs0Szv9KKJ",
"memoryTitle": "Second visit",
"userId": "vIWJgFpnUSeZJw3wjrJeEAxbNoE2",
"imageLabels": [
"Property",
"Furniture",
"City",
"Human settlement",
"Real estate",
"Roof",
"Room",
"Skyline",
"Building",
"Metropolitan area",
"Sky",
"Balcony",
"Apartment"
],
"objectID": "-LmoWe8PEqCs0Szv9KKJ"
}
In my InstantSearch I want to provide a search that present only records with userId
value identical to this specific user that send the query.
First I added the userId to the attributes for faceting in the dashboard.
I don't know exactly how to continue from this point. I don't know how to filter records at query time.
Here is my ViewModel:
class MyViewModel : ViewModel() {
val client = ClientSearch(ApplicationID("*****"), APIKey("*******"), LogLevel.ALL)
val index = client.initIndex(IndexName("memory_diary"))
val searcher = SearcherSingleIndex(index)
val adapterMemory = MemoryAdapter()
val dataSourceFactory = SearcherSingleIndexDataSource.Factory(searcher) { hit ->
Memory(
hit.json.getPrimitive("userId").content,
hit.json.getPrimitive("objectID").content,
hit.json.getPrimitive("memoryTitle").content,
hit.json.getPrimitive("description").content,
hit.json.getPrimitive("creationTime").content.toLong(),
hit.json.getPrimitive("imagePath").content,
hit.json.getArray("imageLabels").content.map { jsnelm -> jsnelm.content }
)
}
val pagedListConfig = PagedList.Config.Builder().setPageSize(50).build()
val memories: LiveData<PagedList<Memory>> = LivePagedListBuilder(dataSourceFactory, pagedListConfig).build()
val searchBox = SearchBoxConnectorPagedList(searcher, listOf(memories))
val stats = StatsConnector(searcher)
val filterState = FilterState()
val connection = ConnectionHandler()
init {
connection += searchBox
connection += stats
connection += filterState.connectPagedList(memories)
}
override fun onCleared() {
super.onCleared()
searcher.cancel()
connection.disconnect()
}
}
I thougt maybe there is a function that filter records in SearcherSingleIndexDataSource.Factory
but I didn't find something like this.
So how can I do that?