Exclude search results based off tag or custom fields

Is it possible to exclude search results based off tags or custom fields. We have a few pages that we do not want to be included within our search results but I cannot figure out how I can do this.

Hi @rameden :wave:

You can achieve that by using the filters query param, which allows you to exclude tags or facet values from the results. For instance, if you have records with a hidden tag value (e.g. {..., _tags: ['hidden']}), you can filter them out that way:

index.search({
  query: '',
  filters: 'NOT _tags:hidden'
})

Alternatively you can also add any attribute name to the attributeForFaceting index setting, which will let you apply filters on this attribute.

Lastly, if you need to make this more secure, you can use secured API keys to generate API keys that embed filter restriction to prevent users to tinker with those restrictions.

Let us know if you have any further question!

Best regards,

Alex

Hi Alex,

Thanks so much for you response. Sorry I am super delayed on getting back to you. As I am using wordpress and have the plugin “child themed” where would I go about adding this filter in?

How do you attach an embedded filter restriction to the api key? I am not seeing the way to do this on the api key generation modal.

I have updated the var search (Instantiate instantsearch.js) to be the following:

/* Instantiate instantsearch.js */
var search = instantsearch({
    appId: algolia.application_id,
    apiKey: algolia.search_api_key,
    indexName: algolia.indices.searchable_posts.name,
    urlSync: {
	    mapping: {'q': 's'},
	    trackedParameters: ['query']
    },
    searchParameters: {
        facetingAfterDistinct: true,
            highlightPreTag: '__ais-highlight__',
            highlightPostTag: '__/ais-highlight__',
            filters: 'NOT _tags:hidden'
    }
});

However it does not seem to be filtering based off the tag of hidden. Is this correct?

searchParameters:
   facetingAfterDistinct:true
   filters:"tags:hidden"
   hierarchicalFacets:(2) [{…}, {…}]
   highlightPostTag:"__/ais-highlight__"
   highlightPreTag:"__ais-highlight__"
   hitsPerPage:10
   index:"wp_searchable_posts" 
   maxValuesPerFacet:10
   query:""

Above is the console log of the var search.

I was able to filter by tag by adding:

filters: 'NOT taxonomies.post_tag:hidden',

to the search = instantsearch -> searchParameters

Full code looks like:

/* Instantiate instantsearch.js */
var search = instantsearch({
appId: algolia.application_id,
apiKey: algolia.search_api_key,
indexName: algolia.indices.searchable_posts.name,
urlSync: {
    mapping: {'q': 's'},
    trackedParameters: ['query']
},
searchParameters: {
    facetingAfterDistinct: true,
        highlightPreTag: '__ais-highlight__',
        highlightPostTag: '__/ais-highlight__',
        filters: 'NOT taxonomies.post_tag:hidden',
}
});

I am going to be doing more testing on what can be filter using this method. Please feel free to post question on this and hopefully I can help.

You can use secured API keys for that purpose.


@rameden glad to hear things are working, let us know if you have any further issues!