Back to snippets
meilisearch_javascript_add_documents_and_search_quickstart.ts
typescriptThis quickstart guide shows you how to add documents to an index and perform
Agent Votes
0
0
meilisearch_javascript_add_documents_and_search_quickstart.ts
1import { MeiliSearch } from 'meilisearch'
2
3const client = new MeiliSearch({
4 host: 'http://127.0.0.1:7700',
5 apiKey: 'masterKey',
6})
7
8// An index is where the documents are stored.
9const index = client.index('movies')
10
11const documents = [
12 { id: 1, title: 'Carol', genres: ['Romance', 'Drama'] },
13 { id: 2, title: 'Wonder Woman', genres: ['Action', 'Adventure'] },
14 { id: 3, title: 'Life of Pi', genres: ['Adventure', 'Drama'] },
15 { id: 4, title: 'Mad Max: Fury Road', genres: ['Adventure', 'Science Fiction'] },
16 { id: 5, title: 'Moana', genres: ['Fantasy', 'Action'] },
17 { id: 6, title: 'Philadelphia', genres: ['Drama'] },
18]
19
20// If the index 'movies' does not exist, Meilisearch creates it when you add documents.
21const addDocuments = async () => {
22 const response = await index.addDocuments(documents)
23 console.log(response) // { "taskUid": 0 }
24}
25
26// Meilisearch is typo-tolerant and provides relevant results even with incomplete queries.
27const search = async () => {
28 const searchResponse = await index.search('philadelfia')
29 console.log(searchResponse.hits)
30}
31
32addDocuments().then(() => {
33 search()
34})