Filter by date: instantsearch

Hi,
I am trying to filter by date:
-Today
-Morning
-This month
-Another date
I use numericRefinementList, my question is how do I send these parameters to instantsearch? Or from there get the values ​​for today, tomorrow …
Thanks

Hi @mrrabbit

I think you could work something out with numericRefinementList that you mentioned.

Good news is that any Date object can be convert into a timestamp that’ll work just fine as a numerical value.
But I’m not sure I understand how you want to apply the filter. So here is a suggestion, please provide a bit more context if that’s not what you want to achieve.

Let’s say you have an index with two objects ;

[{
   eventName: "Search party", 
   date: "2017/09/01 9:42:00",
   timestamp; 1504251720 
 },
 {
   eventName: "Search party", 
   date: "2017/10/01 9:42:00",
   timestamp; 1506843720 
 }]

You could set up your widget like

search.addWidget(
  instantsearch.widgets.numericRefinementList({
    container: '#calendar',
    attributeName: 'timestamp',
    options: [
      {name: 'All'},
      {start: **[TS of today 00:00:00]** end:**[TS of today 23:59:59]**, name: 'Today'},
      {start: **[TS of today + 1 month ]**, name: 'In one month'},
      {start: **[TS of today- 1 month]**, end: **[Today]**, name: 'Happened last month'}
    ],
    templates: {
      header: 'Calendar'
    }
  })
);

Hope this helps :wink:

2 Likes

Hey, as @pierre.aurele.martin said, you should use a timestamp and get the value of the start of your date range ( tomorrow 0:00) to the end of your range (tomorrow 23:59), and then check with a numericRefinementList if it’s between those values.

If you need any more guidance, feel free to ask

Is this still possible using the numericMenu?

I tried adding your snippet below but get syntax errors around **[TS] any suggestions?

      {name: 'All'},
      {start: **[TS of today 00:00:00]** end:**[TS of today 23:59:59]**, name: 'Today'},
      {start: **[TS of today + 1 month ]**, name: 'In one month'},
      {start: **[TS of today- 1 month]**, end: **[Today]**, name: 'Happened last month'}

Hey @brendan.betheldo, this should definitely be possible using a numeric refinement. In the spots where @pierre.aurele.martin wrote [TS of today 00:00:00], you’ll have to convert this to an actual numeric unix timestamp. Similarly, your records should have their dates indexed as timestamps to work correctly. If you’re using Javascript, you can use moment.js to convert dates to timestamps.

Hope this helps!

Hey @maria.schreiber, I’m such an idiot, no idea what I was thinking before! Currently I am receiving the timestamps in a unix format but under a different key which I’ve replaced in the attributeName value would this still be ok?

Thanks so much for your help.

No worries! You can definitely use a different attribute as long as it’s the one declared on ‘attributeName’.

Let us know if it’s still not working as expected!

1 Like