How to restrict the search results using algolia

Hi. I am trying to implement search in my project using laravel/scout and algolia search.
Primarily i am able to search and get the results but i need the results to be in such a way that they must satisfy my condition and then give the results.
Consider this scenario:- i have index called users and they have columns like username, name, email, password and is_active. At this point when i search in my project it displays all the users which is correct. but i wanted it to be further filtered for example - there are 5 users where 3 of the users is_active column value is 1 and the other 2 users is_active column value is 0. So now when i try to search in my project i should not get the users whose is_active column values are 0. I am using autocomplete.js(front-end search). I have tried filters but no use it is not working.

var index = client.initIndex(‘users’);

autocomplete(’#aa-search-input’,
{}, [{
source: autocomplete.sources.hits(index, {hitsPerPage: 3}),
displayKey: ‘name’,
templates: {
header: ‘Users’,
suggestion: function(suggestion) {
if (suggestion.is_active == 1) {
return ‘’ +
suggestion._highlightResult.name.value + ‘
’+
suggestion._highlightResult.username.value + ‘
’;
}
}
}
}]);
The above is my code. Any thing that can be done to achieve my desired results.
Thanks you.

Hi @Arun_jose,

You can go further by supplying the filters property on the configuration object.
Your instantiation of autocomplete will looks like this:

autocomplete("#aa-search-input", {}, [
  {
    source: autocomplete.sources.hits(index, {
      hitsPerPage: 3,
      // filters added
      filters: "is_active = 1"
    }),
    displayKey: "name",
    templates: {
      header: "User",
      suggestion: function(suggestion) {
        return (
          suggestion._highlightResult.name.value +
          suggestion._highlightResult.username.value
        );
      }
    }
  }
]);

Hope that helps, let me know if you have questions :wink:

Edit: Update the filters from is_active:true to is_active = 1

Thank you @samuel.vaillant.
It worked like a charm but with one correction
Instead of using filters: “is_active:true” i used filters: “is_active=1”
Thanks for your help.

Sorry to ask you again i have the same problem again but right now it is little bit different.

 var index = client.initIndex('users');
  var project = client.initIndex('projects');

  autocomplete('#aa-search-input',
  {}, [{
    source: autocomplete.sources.hits(index, {
      hitsPerPage: 3,
      filters: "is_active=1",
    }),
    displayKey: 'name',
    templates: {
      header: '<div class="aa-suggestions-category">Users</div>',
      suggestion: function(suggestion) {
          return '<span>' +
          suggestion._highlightResult.name.value + '</span><span>'+
          suggestion._highlightResult.username.value + '</span>';
        }
    }
  },
  {
    source: autocomplete.sources.hits(project, {
      hitsPerPage: 3,
      filters: 'status=3 OR status=4 OR status=5'
    }),
    displayKey: 'title',
    templates: {
      header: '<div class="aa-suggestions-category">Projects</div>',
      suggestion: function(suggestion) {
        return '<span>' +
        suggestion._highlightResult.title.value + '</span>';
      }
    }
  }])

The filter works for the user but it is not working for the projects.

Hi @Arun_jose,

Thanks for your feedback, you are right about the filters form.

Regarding your second issue, are you sure that the values in your records are numbers for the status attribute? I tried on my side, the filters should work if the values are numbers.

Hope that helps!

Thank you @samuel.vaillant,

No the values were not number it was in string. I just changed my column data-type and it worked.
Thank you.

Hi @samuel.vaillant

When i type one letter on search box, I also need to filter my results by my shop_url .That value is string (eg.shop1,shop2). facetFilters and filter does not work for me.
I hope your reply.
Thanks

This is part of code.

var shopurl1=document.getElementById(‘aa_search_shopurl’).value;

autocomplete(’#aa-search-input’,{hint:true}, {

source: autocomplete.sources.hits(index,{hitsPerPage: 10,
                           facetFilters:'(shop_url: shopurl1)'}),

//value to be displayed in input control after user's suggestion selection
displayKey:'product_name',
//hash of templates used when rendering dataset
templates: {

    //'suggestion' templating function used to render a single suggestion
    suggestion: function(suggestion) {
         var result = '<span>' +
        suggestion._highlightResult.product_name.value 
        + '</span><span> ' + 
        suggestion.new_price + 'MMK</span>';

      return result;
   
   },
    empty: function(result){

        return 'Sorry,No results for "'+result.query + '"';
    }

}

Hi @bitsmanager.develope,

Could you provide us a working example of the issue? It will help a lot to better understand the problem. We provide template in order to avoid you the boilerplate part. You can find it on CodeSandbox. Thanks!