algolia 过滤器在控制台中有效,但在实际搜索结果中无效

algolia filter works in console but is not working in actual search results

所以我的目标是不仅能够在控制台中,而且能够在实际显示的结果中过滤掉 Algolia 命中。所以我已经被这个问题困扰了一个月左右。我正在使用 AlgoliaSearchClient API 和 Swift 以及 InstantSearch 库。我最近想出了如何过滤命中,但它们只反映在控制台中。

这是我用来执行此操作的函数:

extension SchoolTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
    let settings = Settings()

        .set(\.searchableAttributes, to: [.default("eventName")])
        .set(\.attributesForFaceting, to: [.filterOnly("schoolID")])
        .set(\.attributesToRetrieve, to: ["*"])

     searchResultsIndex.setSettings(settings) { (result) in
        if case .success( _) = result {
            self.getTheSchoolsID { (schoolID) in
                if let id = schoolID {
                    let query = Query().set(\.filters, to: "schoolID:\(id)")
                    
                    searchResultsIndex.search(query: query) { (result) in
                            if case .success(let response) = result {
                                print("\(response.hits)")
                        }
                    }
                }
            }
        }
    }
}

}

这在控制台中完美运行,我只能看到来自当前用户 schoolID 的事件。现在,当我搜索时,在实际的搜索栏中,我可以看到每个用户的事件。 Algolia 团队成员告诉我使用下面的这行代码在实际搜索中也应用过滤器。

 getTheSchoolsID { (id) in
        if let id = id {
            self.searcher.indexQueryState.query.filters = "schoolID:\(id)"
            self.searcher.search()
        }
    }

我通过打印它仔细检查了 id 值,它与上面函数中的值完全相同。我不明白为什么 searchBar 没有过滤掉屏幕上的点击,但它在控制台中过滤掉了。 Algolia组员说不用再设置了,因为在其他函数里已经设置好了。

这是我用来配置 Algolia 搜索的全部功能:

func configureAlgoliaSearch() {
    
    getTheSchoolsID { (id) in
        if let id = id {
            self.searcher.indexQueryState.query.filters = "schoolID:\(id)"
            self.searcher.search()
        }
    }
    searchConnector.connect()
 
    navigationController?.navigationBar.prefersLargeTitles = false
    navigationItem.setHidesBackButton(true, animated: true)
    
    algoliaSearchController.searchResultsUpdater = self
    algoliaSearchController.searchBar.searchTextField.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    algoliaSearchController.obscuresBackgroundDuringPresentation = false
    algoliaSearchController.searchBar.placeholder = "Event Name"
    navigationItem.searchController = algoliaSearchController
    definesPresentationContext = true
}

有什么建议吗?

经过一个月的挣扎,我终于想出了一个我早就应该想出的解决方案。我决定为每个用户创建一个索引。这基本上让我无需过滤掉记录。

我需要做的就是在 SearchController 所在的 viewController 中声明几个变量。

 public let client = SearchClient(appID: ApplicationID(rawValue: Keys.algoliaAppID), apiKey: APIKey(rawValue: Keys.algoliaApiKey))

 lazy var algoliaSearchController: UISearchController = .init(searchResultsController: hitsViewController)
lazy var searchConnector: SingleIndexSearchConnector<NameOfEvent> = .init(searcher: searcher,searchController: algoliaSearchController, hitsController: hitsViewController)

let hitsViewController: SchoolInstantiatedHitsTableViewController = .init()

let searcher: SingleIndexSearcher = SingleIndexSearcher(appID: ApplicationID(rawValue: Keys.algoliaAppID), apiKey: APIKey(rawValue: Keys.searcherApiKey), indexName: IndexName(rawValue: Auth.auth().currentUser!.uid) )

连同此功能:

extension SchoolTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
    
    let searchBar = searchController.searchBar.searchTextField.text
    let index2 = client.index(withName: IndexName(rawValue: Auth.auth().currentUser!.uid))
    let settings = Settings()
        
        .set(\.searchableAttributes, to: [.default("eventName")])
        .set(\.attributesToRetrieve, to: ["*"])
    
    index2.setSettings(settings) { (result) in
        if case .success(_) = result {
            let query = Query(searchBar)
            
            index2.search(query: query) { (result) in
                if case .success(_ ) = result {
                    print("Success")
                }
            }
        }
    }
    
    
}

}

当然还有配置实际的 searchBar 本身:

func configureAlgoliaSearch() {

    searchConnector.connect()
    searcher.search()
    algoliaSearchController.delegate = self
    algoliaSearchController.searchResultsUpdater = self
    algoliaSearchController.searchBar.searchTextField.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    algoliaSearchController.obscuresBackgroundDuringPresentation = false
    algoliaSearchController.searchBar.placeholder = "Event Name"
    navigationItem.searchController = algoliaSearchController
    definesPresentationContext = true
    
}

现在有了这些功能,当我添加、删除或更新记录时,它们都对当前用户的索引唯一起作用。 Algolia 的文档起初非常混乱,在论坛上提问有点像和石头说话。无论如何,我希望这可以帮助将来的人。