-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: FacetListInteractor - MultiIndexSearcher connection (#169)
* feat: implement FacetListInteractor + MultiIndexSearcher connection + tests * chore: fix lint issu
- Loading branch information
1 parent
1a4d56a
commit ab7af08
Showing
4 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
Sources/InstantSearchCore/FacetList/FacetListInteractor+MultiIndexSearcher.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// FacetListInteractor+MultiIndexSearcher.swift | ||
// | ||
// | ||
// Created by Vladislav Fitc on 30/03/2021. | ||
// | ||
|
||
import Foundation | ||
import AlgoliaSearchClient | ||
public extension FacetListInteractor { | ||
|
||
struct MultiIndexSearcherConnection: Connection { | ||
|
||
/// Logic applied to the facets | ||
public let facetListInteractor: FacetListInteractor | ||
|
||
/// Searcher that handles your searches | ||
public let searcher: MultiIndexSearcher | ||
|
||
/// Faceting attribute | ||
public let attribute: Attribute | ||
|
||
/// Index of query in the multi-index search | ||
public let queryIndex: Int | ||
|
||
/** | ||
- Parameters: | ||
- facetListInteractor: Logic applied to the facets | ||
- searcher: Searcher that handles your searches | ||
- attribute: Faceting attribute | ||
- queryIndex: Index of query in the multi-index search | ||
*/ | ||
public init(facetListInteractor: FacetListInteractor, | ||
searcher: MultiIndexSearcher, | ||
attribute: Attribute, | ||
queryIndex: Int) { | ||
self.facetListInteractor = facetListInteractor | ||
self.searcher = searcher | ||
self.attribute = attribute | ||
self.queryIndex = queryIndex | ||
} | ||
|
||
public func connect() { | ||
|
||
// When new search results then update items | ||
|
||
searcher.onResults.subscribePast(with: facetListInteractor) { [attribute] interactor, response in | ||
interactor.items = response.results[queryIndex].disjunctiveFacets?[attribute] ?? response.results[queryIndex].facets?[attribute] ?? [] | ||
} | ||
|
||
searcher.indexQueryStates[queryIndex].query.updateQueryFacets(with: attribute) | ||
|
||
} | ||
|
||
public func disconnect() { | ||
searcher.onResults.cancelSubscription(for: facetListInteractor) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
public extension FacetListInteractor { | ||
|
||
/** | ||
- Parameters: | ||
- searcher: Searcher that handles your searches | ||
- attribute: Faceting attribute | ||
- queryIndex: Index of query in the multi-index search | ||
*/ | ||
@discardableResult func connectSearcher(_ searcher: MultiIndexSearcher, | ||
with attribute: Attribute, | ||
queryIndex: Int) -> MultiIndexSearcherConnection { | ||
let connection = MultiIndexSearcherConnection(facetListInteractor: self, | ||
searcher: searcher, | ||
attribute: attribute, | ||
queryIndex: queryIndex) | ||
connection.connect() | ||
return connection | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
Tests/InstantSearchCoreTests/Misc/SearchResultFacets2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"hits": [], | ||
"nbHits": 596, | ||
"page": 0, | ||
"nbPages": 60, | ||
"hitsPerPage": 10, | ||
"processingTimeMS": 4, | ||
"facets": { | ||
"kind": { | ||
"gadgets": 111, | ||
"stuff": 98, | ||
"things": 28, | ||
"others": 16 | ||
} | ||
}, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters