Using Distinct and Grouping Hit Results

Hello, I’m using the distinct feature and it is quite effective at reducing duplicate results, and I’m wondering how I can also group these results under one result.
For example:

  • Item A - 1 - uniqueKey1
  • Item A - 2 - uniqueKey1
  • Item A - 3 - uniqueKey1
  • Item A - 4 - uniqueKey1

Using Distinct on uniqueKey1 would bring back a nicer result like so:

  • Item A

What I am trying to achieve would be to then to group these items under the Item A when searching:

  • Item A
  • Item A - 1
  • Item A - 2
  • Item A - 3
  • Item A - 4

In your example for distinct, you use Burger King, so imagine having a list of fast food locations. You would find Burger King, and then under that you want to show each location associated with it.

Can anyone give me some direction in how I would achieve this result using instantsearch?

Algolia can not do this grouping. What we usually recommend is do do this grouping after fetching the results
with Algolia.

In the case of instantsearch you’ll will probably have to use the connector api

Thanks Maxime, is the connector api replacing v1’s custom widgets?
Or is just a more powerful option with v2?

Do you think I could implement grouping using custom widgets? Or is connector api the only way to go.

Also, how would using algolia’s ‘distinct’ factor into this?

Exactly the question I was looking for…

But how would this work for Vue InstantSearch and Laravel? Any ideas?

Anyone have success implementing the grouping of results?

I’ve been trying some things with limited success. For example, I was able to use lodash to group the results and then render those in a custom v-for loop. But I ran into a problem where the grouping was only showing the grouped output of the first page of results. So it wasn’t an accurate result set. Ive also achieved success going the correlated multi-index search route - not sure if that’s the most efficient, but I can share more if anyone is interested.

Curious to see what you all might have ended up doing and if you’d be willing to share the details and code.

If I understand correctly, I’ve created a codesandbox that should do what you desire:

To do this, I added records for categories and products with attributes such as the records below:

[
   {
      "name": "category 1", 
      "cat_id": 1, 
      "type": "category", 
   }, 
  { 
     "name": "product 4", 
     "cat_id": 1, 
     "type": "product", 
  }
]

In the Algolia dashboard in the Indices->Configuration->Deduplication and Grouping area, I set the Distinct value to 4 (this can be any number depending on how many products you want to show per category), and the Attribute for Distinction to ’ cat_id ’ for my example.


Screen-Shot-2019-07-05-at-1-15-26-PM.png

This grouped all the categories with their products.

To make sure the Categories show first in the list, I added a Custom Ranking in the Indices->Configuration->Ranking and Sorting area in the Algolia dashboard.

I added a sort-by attribute of ’ type ’ and set it to ascending so that the ’ category ’ records would show before the ’ product ’ records in each group.

Screen-Shot-2019-07-05-at-1-29-56-PM.png

To set the ‘distinct’ parameter to more than 4 you can use this:

$index->setSettings([
    'distinct' => 7
]);

Keep in mind that ‘distinct’ is a computationally expensive operation on large data sets, especially if distinct > 1.

I hope this helps!