Algolia instantsearch.js initial search options

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

For example, Select * from index where Category='Careers' (passing a career as a load value)

I was looking for documentation for this and can't figure it out.

In addition, I need to pass several values, something like:

select * from index where Category = 'Careers' or 'Skills' or 'Interests' (using or instructions)

Thanks!

+5
source share
1 answer

I assume that you are using a refinement widget: https://community.algolia.com/instantsearch.js/documentation/#refinementlist . In 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 refinement instance:

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

Then all pre-selected categories will have a "pre-selected" css class by default.

Then you can use css and this is the class name:

 .pre-selected { display: none; } 

Tell me

+11
source

Source: https://habr.com/ru/post/1243150/


All Articles