RelatedItems
About this widget
The RelatedItems component computes search parameters to fetch related items.
You can pass a hit as the reference for computing search parameters and retrieving the related items.
To add RelatedItems to your search experience, use the following components:
Searcher: a newHitsSearcherthat handles your searches for related items.HitsView: adata classrepresenting a search result.T: anIndexabledata class representing the hit to get related items.
This component acts similarly to the Hits component, but it only modifies the results.
Examples
You can use deserialize to convert raw hits into your own data class using the Serializable annotation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class MyActivity : AppCompatActivity() {
  val searcher = HitsSearcher(
      applicationID = ApplicationID("YourApplicationID"),
      apiKey = APIKey("YourSearchOnlyAPIKey"),
      indexName = IndexName("YourIndexName")
  )
  val connection = ConnectionHandler()
  val adapter = ProductAdapter()
  val product = Product(
    objectID = ObjectID("objectID123"),
    name = "productName",
    brand = "Amazon",
    categories = listOf("Streaming Media Players", "TV & Home Theater")
  )
  val matchingPatterns = listOf(
      MatchingPattern(Attribute("brand"), 1, Product::brand),
      MatchingPattern(Attribute("categories"), 2, Product::categories)
  )
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      connection += searcher.connectRelatedHitsView(adapter, product, matchingPatterns) { response ->
          response.hits.deserialize(Product.serializer())
      }
      searcher.searchAsync()
  }
  override fun onDestroy() {
      super.onDestroy()
      connection.disconnect()
      searcher.cancel()
  }
}
@Serializable
data class Product(
    override val objectID: ObjectID,
    val name: String,
    val brand: String,
    val categories: List<String>
) : Indexable
class ProductAdapter : RecyclerView.Adapter<ProductViewHolder>(), HitsView<Product> {
    private var products: List<Product> = listOf()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
        return ProductViewHolder(parent.inflate(R.layout.list_item_product))
    }
    override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
        val item = products[position]
        holder.bind(item)
    }
    override fun setHits(hits: List<Product>) {
        products = hits
        notifyDataSetChanged()
    }
    override fun getItemCount(): Int {
        return products.size
    }
}
class ProductViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
    fun bind(product: Product) {
        view.itemName.text = product.name
    }
}
Parameters
| Parameter | Description | ||||
|---|---|---|---|---|---|
          
            hit
          
         | 
        
           
                
                type: T : Indexable
                
               
              
                
                        Required
                
               
          The reference hit to compute the search parameters to send to Algolia.  | 
      ||||
| 
           
Copy
 
       | 
      |||||
          
            matchingPatterns
          
         | 
        
           
                
                type: List<MatchingPattern<T>>
                
               
              
                
                        Required
                
               
          A schema that creates scored filters based on the hit’s attributes. In the example below, the  
Copy
 
The hit above would generate the following search parameters: 
Copy
 
 | 
      ||||
          
            presenter
          
         | 
        
           
                
                type: (ResponseSearch) -> List<T>
                
               
              
                
                        Required
                
               
          A function that transforms the search response into a list of results of your   | 
      ||||
| 
           
Copy
 
       | 
      |||||