Swift - Filtering results using instant search query

Hello everyone,

I hope you are doing well in these strange corona times.

I am using instant search to profit from the infinite scroll :slight_smile: However, I actually want to limit my search and filter for particular objects within an index.

Currently, my code looks like this:

// START variable for interacting with the algolia server
var queryInputInteractor = QueryInputInteractor()
var singleIndexHitsInteractor: HitsInteractor = .init(showItemsOnEmptyQuery: true
)
let hitsTableController: HitsTableController<HitsInteractor> = .init(tableView: UITableView())
// END variable for interacting with the algolia server

…

 func setupInstantSearchSettings() {
    queryInputInteractor.connectSearcher(commentIndexSearcher)
    queryInputInteractor.connectController(searchBarController)
    
    singleIndexHitsInteractor.connectController(hitsTableController)
    singleIndexHitsInteractor.connectSearcher(commentIndexSearcher)
    
    commentIndexSearcher.search()

…

extension CommentsViewController:UISearchBarDelegate {

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    let categoryQuery = Query()
    categoryQuery.query = "participationId:\(participationObject.id)"
           
    commentIndexSearcher.indexQueryState = .init(index: commentIndex, query: categoryQuery)
    commentIndexSearcher.search()
}

}

Especially, when it comes to the last code snippet? It is executed but I don’t get any results? Why?

Hello @kaan548

If I understand well, the records in your index contain participationId field and you are trying to filter your records by its value?
If so, make sure your have added participationId as an attribute for faceting and set it as a filter, not as a query text of your Query object.
So, replace your line

categoryQuery.query = “participationId:(participationObject.id)”

with

categoryQuery.filters = “participationId:(participationObject.id)”

I hope this answers your question.

Thanks! That helped. You are the best :slight_smile: