🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Guides / Building Search UI / Going further

Conditional requests with Vue InstantSearch

By default, InstantSearch sends an initial request to Algolia’s servers with an empty query. This connection helps speed up later requests.

However, sometimes you don’t want to perform more network calls than are necessary. For example, you may want to limit the number of search requests and reduce your overall Algolia usage. This guide helps you build a UI that prevents this initial request.

How a search client works

InstantSearch is the UI layer that sits on top of Algolia’s search client layer, with a state managed by the Algolia search helper layer. These three layers are interchangeable so that you can use the InstantSearch widgets with a different search client.

The search client queries Algolia’s servers whenever the user refines the search. You can build a custom search client to add custom behavior—for example, to perform backend searches with InstantSearch.

Implementing a proxy

To prevent the initial empty query, you must wrap a custom search client around Algolia’s, and pass it to ais-instant-search. The custom search client acts as a proxy for search requests:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<template>
  <ais-instant-search
    :search-client="searchClient"
    index-name="instant_search"
  >
    <ais-search-box />
    <ais-hits />
  </ais-instant-search>
</template>

<script>
import algoliasearch from 'algoliasearch/lite';

const algoliaClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

const searchClient = {
  ...algoliaClient,
  search(requests) {
    return algoliaClient.search(requests);
  },
};

export default {
  data() {
    return {
      searchClient,
    };
  },
};
</script>

This proxy lets you add some logic before calling the search method. In this case, you only call it when performing a query.

Detecting empty search requests

If you don’t want to perform a search request when the query is empty (""), you first need to detect it:

1
2
3
4
5
6
7
8
9
10
const searchClient = {
  ...algoliaClient,
  search(requests) {
    if (requests.every(({ params }) => !params.query)) {
      // Here we have to do something else
    }

    return algoliaClient.search(requests);
  },
};

Sometimes, one user action results in multiple queries, for example, clicking on an ais-refinement-list or using multiple ais-index widgets. Make sure that every query is empty before intercepting the function call.

When an empty search is detected, you must return a formatted response as an array of objects of the same length as the requests array. Each object requires at least these properties: hits, nbHits, and processingTimeMS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const searchClient = {
  ...algoliaClient,
  search(requests) {
    if (requests.every(({ params }) => !params.query)) {
      return Promise.resolve({
        results: requests.map(() => ({
          hits: [],
          nbHits: 0,
          nbPages: 0,
          page: 0,
          processingTimeMS: 0,
          hitsPerPage: 0,
          exhaustiveNbHits: false,
          query: '',
          params: '',
        })),
      });
    }

    return algoliaClient.search(requests);
  },
};

Now you can use the proxy with the ais-instant-search widget, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<template>
  <ais-instant-search
    :search-client="searchClient"
    index-name="instant_search"
  >
    <p>The request is only triggered once the query is not empty.</p>
    <ais-search-box />
    <ais-hits />
  </ais-instant-search>
</template>

<script>
import algoliasearch from 'algoliasearch/lite';

const algoliaClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

const searchClient = {
  ...algoliaClient,
  search(requests) {
    if (requests.every(({ params }) => !params.query)) {
      return Promise.resolve({
        results: requests.map(() => ({
          hits: [],
          nbHits: 0,
          nbPages: 0,
          page: 0,
          processingTimeMS: 0,
          hitsPerPage: 0,
          exhaustiveNbHits: false,
          query: '',
          params: '',
        })),
      });
    }

    return algoliaClient.search(requests);
  },
};

export default {
  data() {
    return {
      searchClient,
    };
  },
};
</script>
Did you find this page helpful?