Arrow keys: ← previous · → next 25 of 35
@nodedk/search
0.1.4 experimental

search#

Read-only Meilisearch client with JSON filters and sorting.

Requirements#

A Meilisearch instance. Configure filter fields in filterableAttributes and sort fields in sortableAttributes.

Install#

npm i @nodedk/search

Usage#

var search = require('@nodedk/search')

async function main() {
  var client = search({
    host: 'http://127.0.0.1:7700',
    apiKey: process.env.MEILI_API_KEY
  })
  var companies = client.index('companies')
  var result = await companies.search('rema', {
    limit: 25,
    offset: 0,
    filter: {
      active: true,
      employees: { gte: 10 },
      tags: ['retail', 'grocery']
    },
    sort: { employees: 'desc' },
    fields: ['name', 'orgnr', 'employees']
  })

  console.log(result.hits)
  console.log(result.estimatedTotalHits)
}

main().catch(console.error)

API#

search(clientOptions = {})#

Creates a client with an index(uid) method. Options go to @nodedk/meilisearch; supply host and apiKey for your instance.

client.index(uid)#

Takes an index name and returns { search } synchronously.

index.search(query = '', options = {})#

Returns the Meilisearch search promise. An empty query lists matching documents. Supported options:

  • limit: Maximum number of hits to return.
  • offset: Number of hits to skip.
  • filter: JSON filter object.
  • sort: JSON object with asc or desc directions.
  • fields: Document fields to return.

The original Meilisearch response is returned unchanged. Omitted options use the server defaults; other option keys are ignored. Filters accept strings, finite numbers, booleans, and null. Empty filter arrays, empty operator objects, unknown operators, and invalid sort directions throw TypeError.

Examples#

Filters#

Different fields and multiple operators are combined with AND:

var filter = {
  active: true,
  employees: { gte: 10, lte: 500 }
}

Array values use OR for the same field:

var filter = {
  tags: ['retail', 'grocery']
}

This matches documents whose tags field contains either value. Supported operators are eq, ne, gt, gte, lt, and lte.

Filter fields must be configured in the index's filterableAttributes.

Sorting#

var sort = {
  employees: 'desc',
  name: 'asc'
}

Sort fields must be configured in the index's sortableAttributes. Object order determines sort priority.

Fields#

  • Omit fields to return all configured displayed attributes.
  • Use fields: ['name', 'orgnr'] to return selected attributes.
  • Use fields: [] to return no document attributes.

Created by Vidar Eldøy