Sorting results for InstantSearch

Hello everyone,

I hope you are doing fine. I want to sort my search results according to a particular attribute.
I just wanted to verify if my approach is fine:

 filterState[or: "groupName"].add(Filter.Facet(attribute: "challengeId", stringValue: challengeObject.id!))
 sortByConnector = .init(searcher: participationIndexSearcher, indicesNames: ["rating"], selected: 0)
 sortByConnector.connect()
        
 participationIndexSearcher.connectFilterState(filterState)
 participationIndexSearcher.search()

I am not sure, if even sortByConnector.connect() is needed but to explain you my goal, I want to sort after the ranking attribute.

Thanks in advance!
Best regards,
Kaan

Just to update on my post. I changed my code a bit

    private func setupInstantSearchSettings() {
        singleIndexHitsInteractor.connectController(self)
        sortByConnector = .init(searcher: participationIndexSearcher, indicesNames: [ 
       "default","most_rated","recent"], selected: 0)
    }

I created three replicas of my original index and configured the sortBy attribute directly on the dashboard.
The function setupInstantSearchSettings is called once in my view controller.
The actual search is performed in another method

 private func searchMostRatedParticipationContents() {
        filterState[or: "groupName"].add(Filter.Facet(attribute: "challengeId", stringValue: challengeObject.id!))
        
        sortByConnector.selectedIndexName = "most_rated"
        participationIndexSearcher.connectFilterState(filterState)
        participationIndexSearcher.search()
   }

Using the code above, I don’t get ay results at all.

Update again :smiley:
The reason why I was not get any results was, because following line was missing:

        singleIndexHitsInteractor.connectSearcher(participationIndexSearcher)

Now, I am getting results back but somehow, they are not sorted. The only way how I checked the sorting was to print out

hitsSource!.getCurrentHits()

I wanted to have a descending order of the rating attribute but it always prints the one where rating is has the value 0. There are also other rating with a higher value.

Hello guys, any news on this?

Hi @vladislav.fitc , could you help me here, please? :slight_smile: Thanks!

Hi @kaan548 ,

Could you try to test your application with the code of this demo? I hope it will help to detect what’s missing in your implementation.
Let me know if it helps.

Hi @vladislav.fitc , many thanks for coming back on this issue. I am still struggling with it.

I have checked already the demo code. I can see some difference such as:

    controller.sortByConnector.connectController(selectIndexAlertController, presenter: { self.title(for: $0.name) } )
    controller.sortByConnector.interactor.onSelectedComputed.subscribe(with: self) { (viewController, index) in
      index.flatMap { viewController.controller.indexes[$0] }.flatMap(viewController.setChangeIndexButton)
    }

But I assume that I don’t need a UI controller to switch between indices, right? Because Instead of displaying the selectIndexAlertController on the UI, I switch between indices by pressing other buttons on the screen.
Do you have any other suggestions?