algolia / instantsearch-core-swift

⚡️ InstantSearch Core library for Swift and Objective-C
https://community.algolia.com/instantsearch-core-swift/
Apache License 2.0
18 stars 10 forks source link

feat(related-items): implement connectSearcher for related Items #109

Closed spinach closed 4 years ago

spinach commented 4 years ago

Follows similar API as the JS related Item widget.

This is how the DX looks, using keypath for specifying the score for each optional filter

struct Product: Codable {
    let name: String
    let brand: String
    let type: String
    let categories: [String]
    let image: URL
  }

   let matchingPatterns: [MatchingPattern<Product>] =
      [
        MatchingPattern(attribute: "brand", score: 3, filterPath: \.brand),
        MatchingPattern(attribute: "type", score: 10, filterPath: \.type),
        MatchingPattern(attribute: "categories", score: 2, filterPath: \.categories),
      ]

    let searcher = SingleIndexSearcher(index: .test)
    let product = Product.init()
    let hitsInteractor = HitsInteractor<JSON>.init()
    let hit: Hit<Product> = .init(objectID: "objectID", object: product)
    hitsInteractor.connectSearcher(searcher, withRelatedItemsTo: hit, with: matchingPatterns)
   searcher.search()