0

所以我的目标是不仅能够在控制台中过滤掉 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
}

有什么建议么?

4

1 回答 1

0

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

我需要做的就是在 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 的文档一开始很混乱,在论坛上提问有点像对着石头说话。无论如何,我希望这可以帮助将来的人。

于 2021-03-10T17:13:50.990 回答