Stevie Star
Stevie Star

Reputation: 2371

Javascript Algolia - How to initiate a new search with an empty search string?

I have a piece of functionality in my Angular app in which I have some searchable items that exist within my Algolia database. The thing is...they aren't searchable by any string. They are only searchable via facets.

The problem is that when I run my initial .search() function with an empty string + my search filters/facets, I get a list returned and everything is all fine and dandy. HOWEVER, when I go to run the function again to refresh the list, it just comes back with the same results and actually never fires a new request, unless I change one of the filters/facets.

Is there any way to force a search query any time I want, without having to specify a "new" search criteria?

Here is my search function:

searchForAuditions() {

    // Setup the filters/options
    let options = {
      highlightPreTag: '<span class="highlighted">',
      highlightPostTag: '</span>',
      hitsPerPage: 10,
    };

    // Add clause to make sure "is_deleted" is false
    let isDeleted = ` is_deleted: "false"`;

    let facets = `${isDeleted}`;

    // Replace all trailing spaces and split it into an array
    let facetWords = facets.replace(/\s+$/, '').split(" ");

    // Remove trailing "AND"
    if((facetWords[facetWords.length - 1] === "AND") || (facetWords[facetWords.length - 1] === "OR")) {
      facetWords.splice(-1, 1);
      facets = facetWords.join(' ');
    }

    if(facets) {
      options['filters'] = facets;
    }

    this.auditionsIndex.search('', options).then(result => {
      this.results = result.hits;
    })
  }

Thanks in advance!

Upvotes: 0

Views: 778

Answers (1)

Thusitha
Thusitha

Reputation: 3511

From the algolia JavaScript client documentation,

To avoid performing the same API calls twice search results will be stored in a cache that will be tied to your JavaScript client and index objects. Whenever a call for a specific query (and filters) is made, we store the results in a local cache. If you ever call the exact same query again, we read the results from the cache instead of doing an API call.

This is particularly useful when your users are deleting characters from their current query, to avoid useless API calls. Because it is stored as a simple JavaScript object in memory, the cache is automatically reset whenever you reload the page.

To resolve this you should use

index.clearCache() or client.clearCache()

Read more about the inbuilt cache system here.
https://github.com/algolia/algoliasearch-client-javascript#cache

Upvotes: 1

Related Questions