Need help with meilisearch-js?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

meilisearch
246 Stars 40 Forks MIT License 1.4K Commits 22 Opened issues

Description

Javascript client for the MeiliSearch API

Services available

!
?

Need anything else?

Contributors list

MeiliSearch-JavaScript

MeiliSearch JavaScript

MeiliSearch | Documentation | Slack | Roadmap | Website | FAQ

npm version Tests Prettier License Bors enabled

⚡ 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!

Table of Contents <!-- omit in toc -->

📖 Documentation

See our Documentation or our API References.

🔧 Installation

We only guarantee that the package works with

node
>= 12 and
node
< 15.

With

npm
:
npm install meilisearch

With

yarn
:
yarn add meilisearch

🏃‍♀️ Run MeiliSearch <!-- omit in toc -->

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.

Import <!-- omit in toc -->

Depending on the environment in which you are using MeiliSearch, imports may differ.

Import Syntax <!-- omit in toc -->

Usage in an ES module environment:

import { MeiliSearch } from 'meilisearch'

const client = new MeiliSearch({ host: 'http://127.0.0.1:7700', apiKey: 'masterKey', })

Include Script Tag <!-- omit in toc -->

Usage in an HTML (or alike) file:


Require Syntax <!-- omit in toc -->

Usage in a back-end node environment

const { MeiliSearch } = require('meilisearch')

const client = new MeiliSearch({ host: 'http://127.0.0.1:7700', apiKey: 'masterKey', })

React Native <!-- omit in toc -->

To make this package work with React Native, please add the react-native-url-polyfill.

Deno<!-- omit in toc -->

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', })

🎬 Getting Started

Add Documents <!-- omit in toc -->

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.

Basic Search <!-- omit in toc -->

// 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"
}

Custom Search <!-- omit in toc -->

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": "Wonder Woman",
        "genres": ["Action", "Adventure"]
      }
    }
  ],
  "offset": 0,
  "limit": 20,
  "nbHits": 1,
  "processingTimeMs": 0,
  "query": "wonder"
}

Placeholder Search <!-- omit in toc -->

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
    }
  }
}

Abortable Search <!-- omit in toc -->

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()

🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the version v0.23.0 of MeiliSearch.

💡 Learn More

The following sections may interest you:

This repository also contains more examples.

⚙️ Development Workflow and Contributing

Any new contribution is more than welcome to this project!

If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!

📜 API Resources

Search <!-- omit in toc -->

client.index('xxx').search(query: string, options: SearchParams = {}, config?: Partial): Promise>

client.index('xxx').searchGet(query: string, options: SearchParams = {}, config?: Partial): Promise>

Indexes <!-- omit in toc -->

client.getIndexes(): Promise

client.createIndex(uid: string, options?: IndexOptions): Promise>
  • Create a local reference to an index:

client.index(uid: string): Index

client.getOrCreateIndex(uid: string, options?: IndexOptions): Promise>

client.updateIndex(uid: string, options: IndexOptions): Promise
Or using the index object:
index.update(data: IndexOptions): Promise

client.deleteIndex(uid): Promise
Or using the index object:

index.delete(): Promise

index.getStats(): Promise
  • Return Index instance with updated information:

index.fetchInfo(): Promise
  • Get Primary Key of an Index:

index.fetchPrimaryKey(): Promise

Updates <!-- omit in toc -->

index.getUpdateStatus(updateId: number): Promise

index.getAllUpdateStatus(): Promise
  • Wait for pending update:

index.waitForPendingUpdate(updateId: number, { timeOutMs?: number, intervalMs?: number }): Promise

Documents <!-- omit in toc -->

index.addDocuments(documents: Document[]): Promise

index.addDocumentsInBatches(documents: Document[], batchSize = 1000): Promise

index.updateDocuments(documents: Document[]): Promise

index.updateDocumentsInBatches(documents: Document[], batchSize = 1000): Promise

index.getDocuments(params: getDocumentsParams): Promise[]>

index.getDocument(documentId: string): Promise>

index.deleteDocument(documentId: string | number): Promise

index.deleteDocuments(documentsIds: string[] | number[]): Promise

index.deleteAllDocuments(): Promise

Settings <!-- omit in toc -->

index.getSettings(): Promise

index.updateSettings(settings: Settings): Promise

index.resetSettings(): Promise

Synonyms <!-- omit in toc -->

index.getSynonyms(): Promise

index.updateSynonyms(synonyms: Synonyms): Promise

index.resetSynonyms(): Promise

Stop-words <!-- omit in toc -->

Ranking rules <!-- omit in toc -->

Distinct Attribute <!-- omit in toc -->

Searchable Attributes <!-- omit in toc -->

Displayed Attributes <!-- omit in toc -->

Filterable Attributes <!-- omit in toc -->

Sortable Attributes <!-- omit in toc -->

Keys <!-- omit in toc -->

client.getKeys(): Promise

isHealthy <!-- omit in toc -->

client.isHealthy(): Promise

Health <!-- omit in toc -->

client.health(): Promise

Stats <!-- omit in toc -->

client.getStats(): Promise

Version <!-- omit in toc -->

client.getVersion(): Promise

Dumps <!-- omit in toc -->

client.createDump(): Promise

client.getDumpStatus(dumpUid: string): Promise


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.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.