algoliainstantsearch.jsinstantsearch

How to concatenate results on algolia


I would like to be able to concatenate data. Here is an example :

[ { "type": "pen", "color": "blue", }, { "type": "glass", "color": "red", }, { "type": "pen", "color": "green", }, { "type": "glass", "color": "violet", }, { "type": "pen", "color": "yellow", }, { "type": "glass", "color": "orange", } ]

Then, if I search for pen, I will get 3 results, one result for each colors. I would like only one result.

I tried to use a distinct but the algolia distinct is more like a filter, and not a concat. If I do a distinct on the attribute pen, i will get one result that might look like :

{ "type": "pen", "color": "blue", },

What I would like to obtain is :

{ "type": "pen", "color_list": ["blue", "green", "yellow"] },

I cannot find anything like that in the algolia/instant-search doc, so I wonder how to do it. Is there a real tool that can do this, or at least a hacky solution that could do the trick ?

Thanks


Solution

  • Algolia won't concatenate for you, however what's usually done is to have your records hold two information:

    Which results in:

    [{
      "type": "pen",
      "color": "red",
      "color_list": ["red", "blue", "green"]
    },
    {
      "type": "pen",
      "color": "green",
      "color_list": ["red", "blue", "green"]
    },
    ...]
    

    Set up color as a searchable attribute and use distinct to de-duplicate results; you can then have quite relevant results for searches such as "green pen", which would return the pen record with "color": "green" (thus you'd have a product picture with the right color for instance) but allow you to display color swatches of all the other available colours.

    I hope this helps! Please comment if you want to detail your use case more specifically :)