简体   繁体   English

Algolia初始搜索参数instantsearch.js

[英]Algolia initial search parameters instantsearch.js

I have an index where I am trying to pass initial values for the query in algolia. 我有一个索引,我试图在algolia中传递查询的初始值。 I am using the instantsearch.js and right now it just loads everything from my index. 我正在使用instantsearch.js,现在它只是加载我的索引中的所有内容。 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) 例如, Select * from index where Category='Careers' (将职业作为负载上的值传递)

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) select * from index where Category = 'Careers' or 'Skills' or 'Interests' (with or statements)

Thanks! 谢谢!

I assume you are using a refinementList widget: https://community.algolia.com/instantsearch.js/documentation/#refinementlist . 我假设您正在使用refinementList小部件: 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: 您还需要在refinementList实例化中执行此操作:

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. 然后,默认情况下,所有预选类别项都将具有“预先选择”的css类。

Then you can use css and this class name to do: 然后你可以使用css和这个类名来做:

.pre-selected {
  display: none;
}

Let me know 让我知道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM