2017-01-09 59 views
0

我們使用Algolia來索引各種來源和十幾種不同產品的內容。現在我有一個索引,產品是一個配置的方面。使用InstantSearch.js排除過濾器?

我需要找到一種方法來將搜索僅限於用戶擁有的產品。

我正在使用InstantSearch庫,我一直在閱讀文檔和各種在線論壇,以獲取有關如何完成此操作的信息。

這是我正在努力工作的代碼。

var client = algoliasearch("myAppId", "myApiKey") 
var index = client.initIndex('myIndex'); 

var search = instantsearch({ 
appId: 'myAppId', 
apiKey: 'myApiKey', 
indexName: 'myIndex', 
urlSync: {}, 
attributesToHighlight: 'full' 
}); 

search.addWidget(
instantsearch.widgets.refinementList({ 
container: '#products', 
attributeName: 'products', 
operator: 'or', 
limit: 100, 
sortBy: ['name:asc'], 
templates: { 
} 
}) 
); 

search.addWidget({ 
init: function (options) { 
    options.helper.addFacetRefinement('products', 'Product A'); 
} 
}); 

search.start(); 

但是,當我執行此我得到一個錯誤,指出「未捕獲的錯誤:產品未在輔助配置的方面屬性定義」。

我錯過了什麼步驟?或者我以錯誤的方式處理這個問題?

任何指導讚賞。

〜Greg

回答

0

我找到了我的需求的答案。我不得不在searchsearch配置調用中添加一個searchParameters選項。我必須編寫一些代碼來隱藏我的精簡列表小部件中的不需要的產品。

第一步是創建一個我想要隱藏的產品的數組。

var productsToExclude = ['product-a','product-b']; 

我不得不通過「searchParameters」即時搜索配置選項來隱藏項目列表。

var search = instantsearch({ 
appId: 'myAppId', 
apiKey: 'myApiKey', 
indexName: 'myIndex', 
urlSync: {}, 
attributesToHighlight: 'full', 
searchParameters: { facetsExcludes: { "products": productsToExclude}} 
}); 

而且我還必須編寫一些代碼來隱藏refinementList小部件中的項目。

var onRenderHandler = function() { 
for (var p in productsToExclude) { 
$("input[type=checkbox][value='" + productsToExclude[p] + "']").parent().hide(); 
} 
}; 
search.on('render', onRenderHandler);