Usage
Search
Filters

Filters

You can use the filters interface to filter the search results.

Filters are available for numeric, boolean and string properties.

On string properties it perform an exact matching on tokens so it is advised to disable stemming for the properties you want to use filters on (when using the default tokenizer you can provide the stemmerSkipProperties configuration property).

If we consider the following schema:

const db = await create({
  schema: {
    id: 'string',
    title: 'string',
    year: 'number',
    meta: {
      rating: 'number',
      length: 'number',
      favorite: 'boolean',
      tags: 'string'
    },
  },
  components: {
    tokenizer: {
      stemming: true,
      stemmerSkipProperties: ['meta.tags']
    }
  }
})

We will be able to filter the search results by using the where property on numeric properties only:

const results = await search(db, {
  term: 'prestige',
  where: {
    year: {
      gte: 2000,
    },
    'meta.rating': {
      between: [5, 10],
    },
    'meta.favorite': true,
    length: {
      gt: 60,
    }
  },
})

Filters operators

As for now, the following operators are available for numeric properties:

OperatorDescriptionExample
gtGreater thanyear: { gt: 2000 }
gteGreater than or equal toyear: { gte: 2000 }
ltLess thanyear: { lt: 2000 }
lteLess than or equal toyear: { lte: 2000 }
eqEqual toyear: { eq: 2000 }
betweenBetween two values (inclusive)year: { between: [2000, 2008] }

For boolean properties, you can simply set the property to true or false:

const results = await search(db, {
  term: 'prestige',
  where: {
    'meta.favorite': true,
  },
})

For string properties, you can specify a single string:

const results = await search(db, {
  term: 'prestige',
  where: {
    'meta.tags': 'new',
  },
})

You can also specify a list of string, in this case it will return all documents that contain at least one of the values provided:

const results = await search(db, {
  term: 'prestige',
  where: {
    'meta.tags': ['favorite', 'new'],
  },
})