Skip to content

Commit

Permalink
feat: FacetListInteractor - MultiIndexSearcher connection (#169)
Browse files Browse the repository at this point in the history
* feat: implement FacetListInteractor + MultiIndexSearcher connection + tests

* chore: fix lint issu
  • Loading branch information
VladislavFitz authored Mar 30, 2021
1 parent 1a4d56a commit ab7af08
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 2 deletions.
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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,29 @@ public extension FacetListInteractor {

struct SingleIndexSearcherConnection: Connection {

/// Logic applied to the facets
public let facetListInteractor: FacetListInteractor

/// Searcher that handles your searches
public let searcher: SingleIndexSearcher

/// Faceting attribute
public let attribute: Attribute

/**
- Parameters:
- facetListInteractor: Logic applied to the facets
- searcher: Searcher that handles your searches
- attribute: Faceting attribute
*/
public init(facetListInteractor: FacetListInteractor,
searcher: SingleIndexSearcher,
attribute: Attribute) {
self.facetListInteractor = facetListInteractor
self.searcher = searcher
self.attribute = attribute
}

public func connect() {

// When new search results then update items
Expand All @@ -38,8 +57,16 @@ public extension FacetListInteractor {

public extension FacetListInteractor {

@discardableResult func connectSearcher(_ searcher: SingleIndexSearcher, with attribute: Attribute) -> SingleIndexSearcherConnection {
let connection = SingleIndexSearcherConnection(facetListInteractor: self, searcher: searcher, attribute: attribute)
/**
- Parameters:
- searcher: Searcher that handles your searches
- attribute: Faceting attribute
*/
@discardableResult func connectSearcher(_ searcher: SingleIndexSearcher,
with attribute: Attribute) -> SingleIndexSearcherConnection {
let connection = SingleIndexSearcherConnection(facetListInteractor: self,
searcher: searcher,
attribute: attribute)
connection.connect()
return connection
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/InstantSearchCoreTests/Misc/SearchResultFacets2.json
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
}
},
}

Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,35 @@ class FacetListInteractorTests: XCTestCase {
}

}

func testConnectMultiIndexSearcher() {
let interactor = FacetListInteractor(selectionMode: .single)

let searcher = MultiIndexSearcher(appID: "", apiKey: "", indexNames: ["index1", "index2"])

interactor.connectSearcher(searcher, with: "kind", queryIndex: 1)

do {
let results1 = try SearchResponse(jsonFilename: "SearchResultFacets.json")
let results2 = try SearchResponse(jsonFilename: "SearchResultFacets2.json")

let searchResponses = SearchesResponse(results: [results1, results2])

searcher.onResults.fire(searchResponses)

let expectedFacets: Set<Facet> = [
.init(value: "gadgets", count: 111, highlighted: nil),
.init(value: "stuff", count: 98, highlighted: nil),
.init(value: "things", count: 28, highlighted: nil),
.init(value: "others", count: 16, highlighted: nil)
]

XCTAssertEqual(Set(interactor.items), expectedFacets)

} catch let error {
XCTFail(error.localizedDescription)
}

}

}

0 comments on commit ab7af08

Please sign in to comment.