RefinementList on Attribute based on another attribut?

Hi,
I have this dataset (noneditable)

[
    {
        "name": offer1,
        "variations": [
             {
                 "size": "S",
                 "stock": 0,
             },
             {
                 "size": "M",
                 "stock": 1,
             },
             {
                 "size": "L",
                 "stock": 0,
             },
        ]
    },
    ....
]

I would like to create a refinement widget for “size” but if i do so… the result will be :

S (1)
M (1)
L (1)

This not correct because i have to filter by “stock” too. What i am looking for is to refine by “size” with stock > 0. Result only:

M (1)

I had searched about creating a custom refinementList widget but i dont understand where i could find other facets results in widget settings. And I’m pretty bad at javascript.

How do i implement this ?

Any helps could be great !

Thanks

PS: I use instantsearch.js (vanilla)

Hi @f.sadem, Would it be possible to filter the results by stock first or do these out of stock items need to show in the search results.? The refinements, by default, show facets and counts based on the records in the results set.

Hi @cindy.cullen,
It’s not possible to filter the results by stock first and i can’t change the dataset structure unfortunatly.

Someone else have any idea ?

Thanks

Filters don’t have a relationship with each other, which makes this complicated to achieve, but something which is possible is this:

  1. adding a range widget
  2. refining that widget only if the refinement list is refined

Hi @haroen,

Thanks for your reply, that’s interesting.
The problem comes when i don’t refine… it will be wrong and display size “S” and “L” in my example.

Thanks

oh, you always want to show only those with a value of > 0? That’s actually much simpler:

in your case that’s variations.stock > 0 and the refinement list on variations.size

@haroen I think you missunderstood… I don’t want to filter by variation.stock > 0 because it will change the global search result !
In my dataset “variations” is a part of an “offer” (i don’t have an “offer” for each “variation”). If i filter by “variation.stock > 0”, it will remove all offers where a variation is out of stock (no matters there is another size in stock).

I have to keep the search result like it is because i have a lots of other widget to refine the search.

It’s a widget filter thing… not global search.

Am i clearer in my explaination ?

Thanks

1 Like

that’s clearer, although I don’t think it’s possible with our current design of the widgets. An option you can try out is:

  1. custom refinement list widget
  2. instead of reading facets, do searchForFacetValues
  3. add a filter to that searchForFacetValues
  4. display the results

Alright, i undestand what i have to do… but could you help me building this custom widget ?

I already read several times helper docs and algolia docs but creating widget is fucking painful for a simple instantsearch user (and docs is very poor in example).

So where to start ? connector or directly with init/render

Thanks

For this you’d start by comparing your code to the real connectRefinementList: https://github.com/algolia/instantsearch.js/blob/master/src/connectors/refinement-list/connectRefinementList.js and use the helpers like this

and use that in init & render (calling render again once the results have been found)

Sorry @haroen, i didn’t succeed to manage this problem.

I think i don’t have the skill to understand what’s going on in https://github.com/algolia/instantsearch.js/blob/master/src/connectors/refinement-list/connectRefinementList.js

For me it’s a mess

then I’m not sure it will be possible to create a custom widget / request with your data. I don’t think a simpler solution can be found, since this is a fundamental change on how facets get requested. Sorry!

OK, Thanks for the time you spend on it.