Multi-index search with InstantSearch.js
On this page
You are currently reading the documentation for InstantSearch.js v4. Read our migration guide to learn how to upgrade from v3 to v4. You can still access the V3 documentation for this page.
Multi-index search (federated search) is a method for searching multiple data sources simultaneously. This means that when users enter a search term, Algolia will look for and display results from all these data sources.
This doesn’t necessarily mean that the results from Algolia indices are combined since their contents could be quite different. Your approach may be to display the results from each index separately. You could display the top-rated items from a movie index alongside the list of results from a book index. Or you could display category matches alongside the list of results from a product index
Search multiple indices with InstantSearch
The following demo uses an InstantSearch search box to display hits from two different indices:
This example uses a single searchBox
to search multiple indices. It adds the first hits
widget at the top level, while the second one is scoped under an index
widget. The second hits
widget just displays results from the Algolia index referenced by the index
widget that precedes it. The first hits
widget displays results from the top level instant_search
index.
All examples in this guide assume you’ve included InstantSearch.js in your web page from a CDN. If, instead, you’re using it with a package manager, adjust how you import InstantSearch.js and its widgets for more information.
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
const search = instantsearch({
indexName: 'instant_search',
searchClient,
});
search.addWidgets([
instantsearch.widgets.searchBox({
container: '#searchbox',
}),
instantsearch.widgets.hits({
container: '#hits-instant-search',
templates: {
item: (hit, { html, components }) =>
html`<div>${components.Highlight({ hit, attribute: 'name' })}</div>`,
},
}),
instantsearch.widgets
.index({ indexName: 'instant_search_price_desc' })
.addWidgets([
instantsearch.widgets.hits({
container: '#hits-instant-search-price-desc',
templates: {
item: (hit, { html, components }) =>
html`<div>${components.Highlight({ hit, attribute: 'name' })}</div>`,
},
}),
]),
]);
search.start();
You can scope widgets under an index
. The following example displays a different number of hits for the two sets of results. The instant_search
index displays 8 results and instant_search_price_desc
16 results. To restrict the number of results per page, use the configure
widget. Each widget is scoped under the targeted level.
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
const search = instantsearch({
indexName: 'instant_search',
searchClient,
});
search.addWidgets([
instantsearch.widgets.configure({
hitsPerPage: 8,
}),
instantsearch.widgets.searchBox({
container: '#searchbox',
}),
instantsearch.widgets.hits({
container: '#hits-instant-search',
templates: {
item: (hit, { html, components }) =>
html`<div>${components.Highlight({ hit, attribute: 'name' })}</div>`,
},
}),
instantsearch.widgets
.index({ indexName: 'instant_search_price_desc' })
.addWidgets([
instantsearch.widgets.configure({
hitsPerPage: 16,
}),
instantsearch.widgets.hits({
container: '#hits-instant-search-price-desc',
templates: {
item: (hit, { html, components }) =>
html`<div>${components.Highlight({ hit, attribute: 'name' })}</div>`,
},
}),
]),
]);
search.start();
Search multiple indices with Autocomplete
You can use the Autocomplete library in your InstantSearch app to build a dynamic multi-source search experience. For example, you may want to display Query Suggestions together with recent searches, create a multi-column layout that mixes facets and item previews, or even dynamically change sources based on the query.
Autocomplete isn’t limited to Algolia indices: you can use static sources or fetch data from other APIs.
Category display
Algolia can help you display both category matches and results if you:
- Add categories to your Query Suggestions either inline or listed below a result. For example, you might see the following in your Query Suggestions list “game of thrones in Books”
- Use multi-index search to display categories from a separate category index. This is useful if you want to display categories and Query Suggestions at the same time. Clicking such a result typically redirects to a category page. The following is a sample dataset for a product index and a category index.
Example product index
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
{
"name": "Fashion Krisp",
"description": "A pair of red shoes with a comfortable fit.",
"image": "/fashion-krisp.jpg",
"price": 18.98,
"likes": 284,
"category": "Fashion > Women > Shoes > Court shoes"
},
{
"name": "Jiver",
"description": "A blue shirt made of cotton.",
"image": "/jiver.jpg",
"price": 17.70,
"likes": 338,
"category": "Fashion > Men > Shirts > Dress shirt"
}
]
Example category index
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
"name": "Court shoes",
"category": "Fashion > Women > Shoes > Court shoes",
"description": "A dress shoe with a low-cut front and a closed heel.",
"url": "/women/shoes/court/"
},
{
"name": "Dress shirt",
"category": "Fashion > Men > Shirts > Dress shirt",
"description": "A long-sleeved, button-up formal shirt that is typically worn with a suit or tie.",
"url": "/men/shirts/dress/"
}
]