MeiliSearch | Documentation | Slack | Roadmap | Website | FAQ
β‘ The MeiliSearch API client written for JavaScript
MeiliSearch JavaScript is the MeiliSearch API client for JavaScript developers.
MeiliSearch is an open-source search engine. Discover what MeiliSearch is!
- π Documentation
- π§ Installation
- π¬ Getting Started
- π€ Compatibility with MeiliSearch
- π‘ Learn More
- βοΈ Development Workflow and Contributing
- π API Resources
See our Documentation or our API References.
We only guarantee that the package works with node
>= 12 and node
< 15.
With npm
:
npm install meilisearch
With yarn
:
yarn add meilisearch
There are many easy ways to download and run a MeiliSearch instance.
For example, if you use Docker:
docker pull getmeili/meilisearch:latest # Fetch the latest version of MeiliSearch image from Docker Hub
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey
NB: you can also download MeiliSearch from Homebrew or APT.
Depending on the environment on which you are using MeiliSearch, imports may differ.
Usage in a ES module environment:
import { MeiliSearch } from 'meilisearch'
const client = new MeiliSearch({
host: 'http://127.0.0.1:7700',
apiKey: 'masterKey',
})
Usage in an HTML (or alike) file:
<script src='https://cdn.jsdelivr.net/npm/meilisearch@latest/dist/bundles/meilisearch.umd.js'></script>
<script>
const client = new MeiliSearch({
host: 'http://127.0.0.1:7700',
apiKey: 'masterKey',
})
</script>
Usage in a back-end node environment
const { MeiliSearch } = require('meilisearch')
const client = new MeiliSearch({
host: 'http://127.0.0.1:7700',
apiKey: 'masterKey',
})
To make this package work with React Native, please add the react-native-url-polyfill.
Usage in a back-end deno environment
import { MeiliSearch } from "https://esm.sh/meilisearch"
const client = new MeiliSearch({
host: 'http://127.0.0.1:7700',
apiKey: 'masterKey',
})
const { MeiliSearch } = require('meilisearch')
// Or if you are in a ES environment
import { MeiliSearch } from 'meilisearch'
;(async () => {
const client = new MeiliSearch({
host: 'http://127.0.0.1:7700',
apiKey: 'masterKey',
})
// An index is where the documents are stored.
const index = client.index('movies')
const documents = [
{ id: 1, title: 'Carol', genres: ['Romance', 'Drama'] },
{ id: 2, title: 'Wonder Woman', genres: ['Action', 'Adventure'] },
{ id: 3, title: 'Life of Pi', genres: ['Adventure', 'Drama'] },
{ id: 4, title: 'Mad Max: Fury Road', genres: ['Adventure', 'Science Fiction'] },
{ id: 5, title: 'Moana', genres: ['Fantasy', 'Action']},
{ id: 6, title: 'Philadelphia', genres: ['Drama'] },
]
// If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
let response = await index.addDocuments(documents)
console.log(response) // => { "updateId": 0 }
})()
With the updateId
, you can check the status (enqueued
, processing
, processed
or failed
) of your documents addition using the update endpoint.
// MeiliSearch is typo-tolerant:
const search = await index.search('philoudelphia')
console.log(search)
Output:
{
"hits": [
{
"id": "6",
"title": "Philadelphia",
"genres": ["Drama"]
}
],
"offset": 0,
"limit": 20,
"nbHits": 1,
"processingTimeMs": 1,
"query": "philoudelphia"
}
All the supported options are described in the search parameters section of the documentation.
await index.search(
'wonder',
{
attributesToHighlight: ['*'],
filter: 'id >= 1'
}
)
{
"hits": [
{
"id": 2,
"title": "Wonder Woman",
"genres": ["Action", "Adventure"],
"_formatted": {
"id": 2,
"title": "<em>Wonder</em> Woman",
"genres": ["Action", "Adventure"]
}
}
],
"offset": 0,
"limit": 20,
"nbHits": 1,
"processingTimeMs": 0,
"query": "wonder"
}
Placeholder search makes it possible to receive hits based on your parameters without having any query (q
). To enable faceted search on your dataset you need to add genres
in the settings.
await index.search(
'',
{
filter: ['genres = fantasy'],
facetsDistribution: ['genres']
}
)
{
"hits": [
{
"id": 2,
"title": "Wonder Woman",
"genres": ["Action","Adventure"]
},
{
"id": 5,
"title": "Moana",
"genres": ["Fantasy","Action"]
}
],
"offset": 0,
"limit": 20,
"nbHits": 2,
"processingTimeMs": 0,
"query": "",
"facetsDistribution": {
"genres": {
"Action": 2,
"Fantasy": 1,
"Adventure": 1
}
}
You can abort a pending search request by providing an AbortSignal to the request.
const controller = new AbortController()
index
.search('wonder', {}, {
signal: controller.signal,
})
.then((response) => {
/** ... */
})
.catch((e) => {
/** Catch AbortError here. */
})
controller.abort()
This package only guarantees the compatibility with the version v0.22.0 of MeiliSearch.
The following sections may interest you:
- Manipulate documents: see the API references or read more about documents.
- Search: see the API references or follow our guide on search parameters.
- Manage the indexes: see the API references or read more about indexes.
- Configure the index settings: see the API references or follow our guide on settings parameters.
This repository also contains more examples.
Any new contribution is more than welcome in this project!
If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!
- Make a search request:
client.index<T>('xxx').search(query: string, options: SearchParams = {}, config?: Partial<Request>): Promise<SearchResponse<T>>
- Make a search request using GET method (slower than the search method):
client.index<T>('xxx').searchGet(query: string, options: SearchParams = {}, config?: Partial<Request>): Promise<SearchResponse<T>>
- Get all indexes:
client.getIndexes(): Promise<IndexResponse[]>
- Create new index:
client.createIndex<T>(uid: string, options?: IndexOptions): Promise<Index<T>>
- Create a local reference to an index:
client.index<T>(uid: string): Index<T>
- Get an index:
client.getIndex<T>(uid: string): Promise<Index<T>>
- Get or create index if it does not exist
client.getOrCreateIndex<T>(uid: string, options?: IndexOptions): Promise<Index<T>>
- Get Index information:
index.getRawInfo(): Promise<IndexResponse>
- Update Index:
client.updateIndex(uid: string, options: IndexOptions): Promise<Index>
Or using the index object:
index.update(data: IndexOptions): Promise<Index>
- Delete Index:
client.deleteIndex(uid): Promise<void>
Or using the index object:
index.delete(): Promise<void>
- Get specific index stats
index.getStats(): Promise<IndexStats>
- Return Index instance with updated information:
index.fetchInfo(): Promise<Index>
- Get Primary Key of an Index:
index.fetchPrimaryKey(): Promise<string | undefined>
- Get One update info:
index.getUpdateStatus(updateId: number): Promise<Update>
- Get all updates info:
index.getAllUpdateStatus(): Promise<Update[]>
- Wait for pending update:
index.waitForPendingUpdate(updateId: number, { timeOutMs?: number, intervalMs?: number }): Promise<Update>
- Add or replace multiple documents:
index.addDocuments(documents: Document<T>[]): Promise<EnqueuedUpdate>
- Add or update multiple documents:
index.updateDocuments(documents: Document<T>[]): Promise<EnqueuedUpdate>
- Get Documents:
index.getDocuments(params: getDocumentsParams): Promise<Document<T>[]>
- Get one document:
index.getDocument(documentId: string): Promise<Document<T>>
- Delete one document:
index.deleteDocument(documentId: string | number): Promise<EnqueuedUpdate>
- Delete multiple documents:
index.deleteDocuments(documentsIds: string[] | number[]): Promise<EnqueuedUpdate>
- Delete all documents:
index.deleteAllDocuments(): Promise<Types.EnqueuedUpdate>
- Get settings:
index.getSettings(): Promise<Settings>
- Update settings:
index.updateSettings(settings: Settings): Promise<EnqueuedUpdate>
- Reset settings:
index.resetSettings(): Promise<EnqueuedUpdate>
- Get synonyms:
index.getSynonyms(): Promise<object>
- Update synonyms:
index.updateSynonyms(synonyms: Synonyms): Promise<EnqueuedUpdate>
- Reset synonyms:
index.resetSynonyms(): Promise<EnqueuedUpdate>
-
Get Stop Words
index.getStopWords(): Promise<string[]>
-
Update Stop Words
index.updateStopWords(stopWords: string[] | null ): Promise<EnqueuedUpdate>
-
Reset Stop Words
index.resetStopWords(): Promise<EnqueuedUpdate>
-
Get Ranking Rules
index.getRankingRules(): Promise<string[]>
-
Update Ranking Rules
index.updateRankingRules(rankingRules: string[] | null): Promise<EnqueuedUpdate>
-
Reset Ranking Rules
index.resetRankingRules(): Promise<EnqueuedUpdate>
-
Get Distinct Attribute
index.getDistinctAttribute(): Promise<string | void>
-
Update Distinct Attribute
index.updateDistinctAttribute(distinctAttribute: string | null): Promise<EnqueuedUpdate>
-
Reset Distinct Attribute
index.resetDistinctAttribute(): Promise<EnqueuedUpdate>
-
Get Searchable Attributes
index.getSearchableAttributes(): Promise<string[]>
-
Update Searchable Attributes
index.updateSearchableAttributes(searchableAttributes: string[] | null): Promise<EnqueuedUpdate>
-
Reset Searchable Attributes
index.resetSearchableAttributes(): Promise<EnqueuedUpdate>
-
Get Displayed Attributes
index.getDisplayedAttributes(): Promise<string[]>
-
Update Displayed Attributes
index.updateDisplayedAttributes(displayedAttributes: string[] | null): Promise<EnqueuedUpdate>
-
Reset Displayed Attributes
index.resetDisplayedAttributes(): Promise<EnqueuedUpdate>
-
Get Filterable Attributes
index.getFilterableAttributes(): Promise<string[]>
-
Update Filterable Attributes
index.updateFilterableAttributes(filterableAttributes: string[] | null): Promise<EnqueuedUpdate>
-
Reset Filterable Attributes
index.resetFilterableAttributes(): Promise<EnqueuedUpdate>
-
Get Sortable Attributes
index.getSortableAttributes(): Promise<string[]>
-
Update Sortable Attributes
index.updateSortableAttributes(sortableAttributes: string[] | null): Promise<EnqueuedUpdate>
-
Reset Sortable Attributes
index.resetSortableAttributes(): Promise<EnqueuedUpdate>
- Get keys
client.getKeys(): Promise<Keys>
- Return
true
orfalse
depending on the health of the server.
client.isHealthy(): Promise<boolean>
- Check if the server is healthy
client.health(): Promise<Health>
- Get database stats
client.getStats(): Promise<Stats>
- Get binary version
client.getVersion(): Promise<Version>
- Trigger a dump creation process
client.createDump(): Promise<Types.EnqueuedDump>
- Get the status of a dump creation process
client.getDumpStatus(dumpUid: string): Promise<Types.EnqueuedDump>
MeiliSearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides repository.