Scheduled Visibility for Search Results

Hello -

We are a content company, and we produce articles where articles can be published immediately, or for a future date.

Our website handles this by simply looking at the publish_date and displaying content if today() > publish_date.

We’ve configured Algolia to index content once it’s published or scheduled.

We don’t want to set up a cron job to constantly watch for scheduled articles to go from scheduled to published… Instead, we’d like to offload this logic to Algolia or our InstantSearch.js presentation layer.

What’s the best approach here? I’m able to hide search results in the presentation templating layer (display: none if today() < publish_date), but that screws with result counters (10 hits, when only 5 are displayed).

Does Algolia support querying the index like so? “<query> AND today() > publish_date

Any other suggestions?

Thanks so much!

Hi @tswientek, thanks for asking!

Indeed, filtering in the presentation layer would work, but has the downside you mentioned of making counts invalid.

You’re on the right track, what you are looking for is using both a query and a facet filter, something like this:

index.search({
  query: 'query',
  filters: today() + ' > publish_date'
}).then(res => {
  // console.log(res);
});

This way you will get both filtered results and valid results/facet counts.
Let me know if this helps or if you have further questions :slight_smile:

1 Like

Hi @pln -

Thanks for the tip! I’m adding our solution, in case anyone else runs across this in the future.

Since we’re using the InstantSearch.js library, we were able to inject a filter in the configuration widget. This effectively filters the results on Algolia’s end, and ensures facet counts are consistent.

configure: function() {
    var filters = '(object_publish_date<' + Math.round((new Date()).getTime() / 1000) + ')';
    return instantsearch.widgets.configure({
        filters: filters
    });
},

InstantSearch.js configure widget: https://community.algolia.com/instantsearch.js/v2/widgets/configure.html

And you can find the configurable search parameters here, https://www.algolia.com/doc/api-reference/search-api-parameters/.

1 Like

Hi @tswientek, thanks for sharing this! Using the Configure widget indeed seems to be the proper way to achieve this, as it separates concerns well :slight_smile: