Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algolia initial search parameters instantsearch.js

I have an index where I am trying to pass initial values for the query in algolia. I am using the instantsearch.js and right now it just loads everything from my index. How can I pass initial values to the index on page load?

For example Select * from index where Category='Careers' (passing careers as a value on the load)

I've searched the documentation for this and cant figure it out.

Also, I need to pass multiple values, so something like:

select * from index where Category = 'Careers' or 'Skills' or 'Interests' (with or statements)

Thanks!

like image 787
Matthew Harris Avatar asked Feb 16 '16 15:02

Matthew Harris


1 Answers

I assume you are using a refinementList widget: https://community.algolia.com/instantsearch.js/documentation/#refinementlist. On the 'category' attribute of your data.

If so, you can do this:

var preselectedCategories = ['Careers', 'Skills'];

var search = instantsearch(applicationID, apiKey, {
  ...other parameters,
  searchParameters: {
    disjunctiveFacetsRefinements: {
      category: preselectedCategories
    }
  }
})

You will also need to do this in the refinementList instantiation:

var refinementList = instantsearch.widgets.refinementList({
  transformData: {
    item: function(item) {
      if (preselectedCategories.indexOf(item.name) !== -1) {
        item.cssClasses.label += ' pre-selected';
      }

      return item;
    }
  }
});

Then all the preselected categories items will have the "pre-selected" css class by default.

Then you can use css and this class name to do:

.pre-selected {
  display: none;
}

Let me know

like image 133
vvo Avatar answered Oct 15 '22 02:10

vvo