Share the article
Today we’ll learn how to promote results by pinning given documents at the top of search results. This can be used in ecommerce to give more visibility to featured products or in-app search to prioritize user-specific content. We’ll use Node.js and React to implement promoted documents.
Need promoted documents inside the Meilisearch engine? Give your feedback!
This tutorial will use a sample movie dataset. Our objective will be to have “Finding Nemo” at the top of the results when typing “funny”, “children”, or “fish”.
Folder structure
In this guide, we’ll separate the frontend code from the backend code used to configure Meilisearch.
We’ll organize our project using the following folder structure:
my-app/
├─ node_modules/
├─ data/ <-- Backend code goes there
│ ├─ setup.js
│ ├─ movies.json
│ ├─ promoted-movies.json
├─ src/ <-- React code will go there
│ ├─ App.jsx
│ ├─ main.js
│ ├─ ...
├─ package.jsonFirst, create a directory for our application (eg. my-app) or use an existing one. In our application directory, let’s create a data folder for the backend code.
Download the movies dataset and save it as movies.json in the new data folder.
The promoted index holds only the documents you want pinned, plus a keywords field listing the queries that should surface them. Create data/promoted-movies.json with a single promoted movie:
[
{
"id": 12,
"title": "Finding Nemo",
"overview": "Nemo, an adventurous young clownfish, is unexpectedly taken from his Great Barrier Reef home to a dentist's office aquarium. It's up to his worrisome father Marlin and a friendly but forgetful fish Dory to bring Nemo home.",
"genres": ["Animation", "Family"],
"poster": "https://image.tmdb.org/t/p/w500/eHuGQ10FUzK1mdOY69wF5pGgEf5.jpg",
"release_date": 1054252800,
"keywords": ["funny", "children", "fish"]
}
]Alright. Let’s code!
Meilisearch setup
First, we need a Meilisearch instance. The quickest route is Meilisearch Cloud, which gives you a managed project with no setup. You can also run the open-source engine locally by following the self-hosting guide. We’ll write a setup script to configure our Meilisearch instance and seed the data.
This setup script uses the JavaScript SDK, but you can use any of Meilisearch SDKs.
In this tutorial, we're using the current Node LTS. Let's install the JavaScript SDK:
npm install meilisearchThen, let's create a setup.js file:
// data/setup.js
import { MeiliSearch } from 'meilisearch'
// Load the datasets
import movies from './movies.json' with { type: 'json' }
import promotedMovies from './promoted-movies.json' with { type: 'json' }
// Load credentials from environment
const credentials = {
host: 'your Meilisearch host',
apiKey: 'your Meilisearch Admin API key'
}
// Configuration
const INDEX_NAME = 'movies'
const PROMOTED_INDEX_NAME = `promoted-${INDEX_NAME}`
const searchableAttributes = ['title', 'overview', 'genres', 'release_date']
const displayedAttributes = ['id', 'title', 'overview', 'genres', 'poster', 'release_date']
const setup = async () => {
console.log('Seeding your Meilisearch instance')
const client = new MeiliSearch(credentials)
// Adding searchable attributes to movies index
await client.index(INDEX_NAME).updateSearchableAttributes(searchableAttributes)
// In the promoted movies index, only `keywords` is searchable
await client.index(PROMOTED_INDEX_NAME).updateSearchableAttributes(['keywords'])
// Both indexes have the same visible attributes
// `keywords` is hidden in the promoted movies index
await client.index(INDEX_NAME).updateDisplayedAttributes(displayedAttributes)
await client.index(PROMOTED_INDEX_NAME).updateDisplayedAttributes(displayedAttributes)
// Adding documents
await client.index(INDEX_NAME).addDocuments(movies)
await client.index(PROMOTED_INDEX_NAME).addDocuments(promotedMovies)
// Wait for tasks completion
await watchTasks(client, INDEX_NAME)
await watchTasks(client, PROMOTED_INDEX_NAME)
}
// Helper to watch tasks
const watchTasks = async (client, uid) => {
console.log(`Watching tasks on index ${uid}`)
const { results } = await client.index(uid).getTasks()
console.log(`${uid} index: waiting for tasks`)
await client.waitForTasks(results.map((task) => task.uid))
console.log(`${uid} index: tasks finished`)
}
await setup()This script creates our two indexes. Let’s recap what it does:
- It sets the same displayed attributes for both
moviesandpromoted-movies. - It sets the searchable attributes; only
keywordsis searchable in thepromoted-moviesindex. - It adds documents to our Meilisearch indexes.
To optimize indexing speed, always add documents after configuring settings.
We can now run the script with Node.js:
node setup.jsOur Meilisearch instance is now configured and seeded. It’s time to implement promoted results using React!
Displaying promoted results
First, we’ll navigate back to the root directory of our application (eg. my-app). We’ll use Vite to create the React app in this folder.
npm create vite@latest . -- --template reactIf the CLI asks to remove existing files from the current directory, answer 'No'.
Then, let’s install the additional dependencies for integrating InstantSearch with Meilisearch:
npm install @meilisearch/instant-meilisearch react-instantsearchLet’s edit our App component to display search results. We want the promoted results to show up before the rest. Let’s replace the App.jsx boilerplate code with our own:
// src/App.jsx
import { instantMeiliSearch } from '@meilisearch/instant-meilisearch'
import { InstantSearch, SearchBox, Hits, Index } from 'react-instantsearch'
const MEILI_HOST = 'your Meilisearch host'
const MEILI_SEARCH_KEY = 'your Meilisearch Search API key'
const { searchClient } = instantMeiliSearch(MEILI_HOST, MEILI_SEARCH_KEY)
const App = () => (
<div>
<h1>Hello React</h1>
<InstantSearch searchClient={searchClient}>
<SearchBox />
<h2>Results</h2>
<Index indexName='promoted-movies'>
<Hits />
</Index>
<h2>More results</h2>
<Index indexName='movies'>
<Hits />
</Index>
</InstantSearch>
</div>
);
export default App;We can now run the development server using npm run dev. In the browser, our app should display something like this:

Displaying promoted results before other results
Congrats. We’ve successfully displayed our promoted results before the rest of the results.
Got stuck? Don’t hesitate to ask for help in our Discord community.
Frequently asked questions (FAQs)
How do promoted search results work in Meilisearch?
Meilisearch has no dedicated "promote this document" setting, so you model promotion as data. You keep a second, much smaller index holding only the documents you want pinned, with a keywords field listing the queries that should surface them. Both indexes are queried in the same request, and the frontend renders the promoted hits above the regular ones.
Why does the promoted index only make keywords searchable?
Because you want full control over when a promoted document appears. If title and overview were searchable in the promoted index, "Finding Nemo" would surface for any query matching its description, not only the queries you chose. Restricting the searchable attributes to keywords means the document is promoted for exactly the terms you list and nothing else.
Does promoting documents this way affect the relevancy of normal results?
No. The two indexes are ranked independently, so the promoted index cannot reorder or suppress anything in your main index. The main results come back exactly as they would without promotion, and the promoted hits are simply rendered before them.
Can you implement promoted results on the backend instead?
Yes. Instead of querying two indexes from the frontend, you can pin documents server-side with any of the Meilisearch SDKs and merge them into a single response. That takes more backend code but returns one result set, which is easier to paginate.
Going further
This tutorial explored one approach for implementing promoted results. An alternative technique would be implementing document pinning in the backend, using one of Meilisearch SDKs. This approach has the benefit of allowing to merge results in a single response (as if they were coming from a single index).
Both techniques can achieve similar results. We also plan to integrate promoted documents in the Meilisearch engine. Share your use case in our product discussions to help us prioritize it.
For more things Meilisearch, you can subscribe to our newsletter. You can learn more about our product by checking out the roadmap and participating in our product discussions.
For anything else, join our developers' community on Discord.
I’ll see you there.





