Vue InstantSearch is a Vue.js library that lets you create a search results experience with Algolia.
This tutorial generates code to:
Display and format the search box and results
Use pre-built UI components (widgets) to filter results
Before you begin
This tutorial assumes you have Vue knowledge and have installed Vue InstantSearch .
Tutorial
In this tutorial, you’ll add an Algolia search interface to some starter code.
Add starter code
To generate some starter code, use the create-instantsearch-app
installed with Vue InstantSearch.
In a terminal, paste:
npx create-instantsearch-app ais-ecommerce-demo-app --template "Vue InstantSearch"
This generates a folder structure on your machine:
ais-ecommerce-demo-app/
├── node_modules/
├── src/
├──── App.vue
├──── main.js
├── package.json
├── README.md
└── yarn.lock
create-instantsearch-app
provides the index data, all necessary code, and predefined credentials (application ID and API key).
Initialization
If you’re using your app instead of create-instantsearch-app
,
initialize it by changing the contents of main.js
to include the Vue InstantSearch library:
import { createApp } from "vue" ;
import App from "./App.vue" ;
import InstantSearch from "vue-instantsearch/vue3/es" ;
const app = createApp ( App );
app . use ( InstantSearch );
app . mount ( "#app" );
Add the search user interface code
Open src/App.vue
and replace the whole file with the following:
< template >
< ais-instant-search :search-client = "searchClient" index-name = "demo_ecommerce" >
< ais-search-box />
< ais-hits >
< template v-slot : item = " { item } " >
< h2 > {{ item.name }} </ h2 >
</ template >
</ ais-hits >
</ ais-instant-search >
</ template >
< script >
import { liteClient as algoliasearch } from 'algoliasearch/lite' ;
import 'instantsearch.css/themes/algolia-min.css' ;
export default {
data () {
return {
searchClient: algoliasearch (
'B1G2GM9NG0' ,
'aadef574be1f9252bb48d4ea09b5cfe5'
),
};
} ,
} ;
</ script >
< style >
body {
font-family : sans-serif ;
padding : 1 em ;
}
</ style >
Understand the code
In src/App.js
, you’ll see several InstantSearch widgets :
ais-instant-search
is the root Vue InstantSearch component. All other widgets must be wrapped within this widget
ais-search-box
displays a search box for users to type queries into
ais-hits
displays the results from Algolia, based on the query.
Run the project
In your terminal, type:
cd ais-ecommerce-demo-app
npm start
Open your browser and go to http://localhost:3000 .
You’ll see this:
To enhance your search UI, add these widgets:
Open the file src/App.vue
and update it:
< template >
< ais-instant-search index-name = "demo_ecommerce" :search-client = "searchClient" >
< div class = "left-panel" >
< ais-clear-refinements />
< h2 > Brands </ h2 >
< ais-refinement-list attribute = "brand" searchable />
< ais-configure :hitsPerPage = "8" />
</ div >
< div class = "right-panel" >
< ais-search-box />
< ais-hits >
< template v-slot : item = " { item } " >
< h2 > {{ item.name }} </ h2 >
</ template >
</ ais-hits >
< ais-pagination />
</ div >
</ ais-instant-search >
</ template >
Alter the styling
The widgets have a predefined style but this can be altered.
To create a two-column layout, replace the content of the style tag with:
body {
font-family : sans-serif ;
padding : 1 em ;
}
.ais-Hits-list {
margin-top : 0 ;
margin-bottom : 1 em ;
}
.ais-InstantSearch {
display : grid ;
grid-template-columns : 1 fr 4 fr ;
grid-gap : 1 em ;
}
In your browser, after a page refresh, you’ll see this:
Customize hits
Open the file src/App.vue
and replace the content of the ais-hits
item slot with:
< template v-slot : item = " { item } " >
< h2 > {{ item.name }} </ h2 >
< img :src = "item.image" align = "left" :alt = "item.name" />
< div class = "hit-name" >
< ais-highlight attribute = "name" :hit = "item" ></ ais-highlight >
</ div >
< div class = "hit-description" >
< ais-highlight attribute = "description" :hit = "item" ></ ais-highlight >
</ div >
< div class = "hit-price" > {{ item.price }} </ div >
</ template >
Update the content of the style tag with:
body {
font-family : sans-serif ;
padding : 1 em ;
}
.ais-Hits-list {
margin-top : 0 ;
margin-bottom : 1 em ;
}
.ais-InstantSearch {
display : grid ;
grid-template-columns : 1 fr 4 fr ;
grid-gap : 1 em ;
}
.ais-Hits-item img {
margin-right : 1 em ;
}
.hit-name {
margin-bottom : 0.5 em ;
}
.hit-description {
color : #888 ;
font-size : 0.8 em ;
margin-bottom : 0.5 em ;
}
In your browser, after a page refresh, you’ll see this:
For more information about customization, see Customize an existing widget .
First:
Download the dataset
Set up an API client and send your data to Algolia
Configure the index with the following code:
PHP
Ruby
JavaScript
Python
Swift
C#
Java
Go
Scala
$index -> setSettings ( array (
"searchableAttributes" => [
"brand" ,
"name" ,
"categories" ,
"unordered(description)"
],
"customRanking" => [
"desc(popularity)"
],
"attributesForFaceting" => [
"searchable(brand)" ,
"type" ,
"categories" ,
"price"
]
));