我有一个使用Algolia的RoR应用程序。一位客户删除了几笔经数据库验证的商品,但搜索结果中仍然存在。
这是application.js文件:
var search = instantsearch({
appId: ‘numbersletters',
apiKey: options.publicSearchKey,
indexName: options.indexName,
routing: true,
searchFunction: function(helper){
if(options.noBlankSearch && helper.state.query === ''){
$('.search-header').hide();
$('#search-map').hide();
$('#algolia-search-results').hide();
$('#refinement-sidebar').collapse('hide');
$('.search-facet').hide();
if($('#search-box-error').hasClass('initialized')){
$('#search-box-error').removeClass('hidden');
}
return;
}
$('#search-box-error').addClass('hidden');
helper.search();
$('.search-header').show();
$('.search-facet').show();
// media query here to collapse the sidebar if screen == xs
if(!window.matchMedia('(min-width: 768px)').matches){
$('#refinement-sidebar').collapse('hide');
$('#xs-sidebar-filter-toggle').removeClass('hidden')
}
$('#algolia-search-results').show();
}
});
search.addWidget(
instantsearch.widgets.searchBox({
container: '#algolia-search-input',
magnifier: false,
reset: false,
autofocus: false,
searchOnEnterKeyPressOnly: true
})
);
search.addWidget(
instantsearch.widgets.hits({
container: '#algolia-search-results',
transformData: {
item: function(data){
if($.inArray(data.objectID, options.favoritePropertyIds) > -1){
data.isFavoriteProperty = true;
data.isFavoritePropertyIcon = 'heart';
}
else {
data.isFavoriteProperty = false;
data.isFavoritePropertyIcon = 'heart-o';
}
data.property_type_icon = data.property_type === 'Home' ? 'home' : 'tree';
data.separatorRowClass = '';
if(((data.__hitIndex + 1) % 3) === 0){
data.separatorRowClass = 'row';
}
data.rates = '';
if(data.nightly_rate != undefined){
data.rates = '<p class="cost">$';
data.rates += data.nightly_rate;
data.rates += '<sub> per night</sub></p>';
}
if(data.monthly_rate != undefined){
data.rates += '<p class="cost">$';
data.rates += data.monthly_rate;
data.rates += '<sub> per month</sub></p>';
}
data.stars = '';
if(data.review_rating > 0){
data.stars = _starsForRating(data);
}
data.eventBanner = '';
if(data.event_id != undefined){
data.eventBanner = '<img class="img-responsive event-id-img" src=' +
options.eventBanners[data.event_id - 1] +
' alt="' +
data.event_name +
'">';
}
console.log(data);
return data;
},
empty: function(data){
// special logic for xs screens
if(!window.matchMedia('(min-width: 768px)').matches){
$('#refinement-sidebar').collapse('hide');
$('#xs-sidebar-filter-toggle').addClass('hidden')
}
return data;
}
},
templates: {
item: document.getElementById('search-results-template').innerHTML,
empty: '<div class="text-center">We did not find any results for:<br/><em>\"{{query}}\"</em></div>'
}
})
);
search.addWidget(
instantsearch.widgets.refinementList({
container: '#utilities-refinement',
attributeName: 'utilities',
sortBy: ['name: asc'],
limit: 10,
operator: 'and',
templates: {
item: facetTemplateCheckbox,
header: '<div class="facet-title">Utilities</div>'
}
})
);
search.addWidget(
instantsearch.widgets.refinementList({
container: '#features-refinement',
attributeName: 'features',
sortBy: ['count:desc', 'name:asc'],
limit: 10,
operator: 'and',
templates: {
item: facetTemplateCheckbox,
header: '<div class="facet-title">Features</div>'
}
})
);
search.start();
$('#search-box-error').addClass('initialized');
$('#search-submit').on('click', function () {
search.helper
.clearCache()
.search();
});
}
};
$(document).ready(app.init);
我在点击时尝试了各种方法,重点是在执行搜索之前进行刷新。
我尝试了以下更改:
search.refresh();
search.helper.search();
search.helper.refresh();
search.helper.search();
search.client.clearCache();
search.helper.search();
search.helper.client.clearCache();
search.helper.search();
最后是:
search.helper
.clearCache()
.search();
似乎没有任何效果。有没有办法使用香草js / jquery强制刷新Algolia即时搜索?