Skip to main content

Filters

Filters allow you to narrow down search results based on specific property values. You can filter by numbers, strings, booleans, enums, and even perform geospatial filtering.

Basic Filtering

Use the where clause to filter search results:
import { create, insert, search } from '@orama/orama'

const db = create({
  schema: {
    title: 'string',
    description: 'string',
    price: 'number',
    category: 'string'
  }
})

insert(db, {
  title: 'Noise cancelling headphones',
  description: 'Best noise cancelling headphones on the market',
  price: 99.99,
  category: 'electronics'
})

const results = search(db, {
  term: 'Headphones',
  where: {
    price: {
      lt: 100
    }
  }
})

Filter Operators

Number Filters

Filter numeric properties using comparison operators:
const results = search(db, {
  term: 'headphones',
  where: {
    price: {
      lt: 100
    }
  }
})
gt
number
Greater than
gte
number
Greater than or equal to
lt
number
Less than
lte
number
Less than or equal to
eq
number
Equal to
between
[number, number]
Between two values (inclusive)

String Filters

Filter string properties by exact match or array of values:
const results = search(db, {
  term: 'product',
  where: {
    category: 'electronics'
  }
})

Boolean Filters

Filter by boolean values:
const db = create({
  schema: {
    title: 'string',
    inStock: 'boolean'
  }
})

const results = search(db, {
  term: 'headphones',
  where: {
    inStock: true
  }
})

Enum Filters

Filter enum properties with comparison operators:
const results = search(db, {
  term: 'movie',
  where: {
    genre: {
      eq: 'drama'
    }
  }
})
eq
string | number | boolean
Equal to the specified value
in
array
Match any value in the array
nin
array
Do not match any value in the array

Array Enum Filters

Filter on enum arrays with contains operators:
const db = create({
  schema: {
    title: 'string',
    tags: 'enum[]'
  }
})

insert(db, {
  title: 'Premium Headphones',
  tags: ['audio', 'wireless', 'noise-cancelling']
})
const results = search(db, {
  term: 'headphones',
  where: {
    tags: {
      containsAll: ['audio', 'wireless']
    }
  }
})
containsAll
array
Document must contain all specified values
containsAny
array
Document must contain at least one specified value

Logical Operators

AND Conditions

Combine multiple filter conditions (all must match):
const results = search(db, {
  term: 'headphones',
  where: {
    and: [
      {
        price: {
          gte: 50
        }
      },
      {
        price: {
          lt: 100
        }
      },
      {
        category: 'electronics'
      }
    ]
  }
})
Multiple properties in the same where object are implicitly combined with AND.

OR Conditions

Match any of the specified conditions:
const results = search(db, {
  term: 'headphones',
  where: {
    or: [
      {
        category: 'electronics'
      },
      {
        category: 'audio'
      },
      {
        price: {
          lt: 50
        }
      }
    ]
  }
})

NOT Conditions

Exclude documents matching the condition:
const results = search(db, {
  term: 'headphones',
  where: {
    not: {
      category: 'refurbished'
    }
  }
})

Complex Nested Conditions

Combine multiple logical operators:
const results = search(db, {
  term: 'headphones',
  where: {
    and: [
      {
        or: [
          { category: 'electronics' },
          { category: 'audio' }
        ]
      },
      {
        price: {
          lt: 100
        }
      },
      {
        not: {
          brand: 'generic'
        }
      }
    ]
  }
})

Filter-Only Searches

You can perform filter-only searches without a search term:
// Returns all documents with price less than 100
const results = search(db, {
  where: {
    price: {
      lt: 100
    }
  }
})
When no term is provided, Orama returns all documents matching the filter with a score of 0.

Nested Property Filters

Filter on nested object properties:
const db = create({
  schema: {
    title: 'string',
    meta: {
      rating: 'number',
      reviews: 'number'
    }
  }
})

const results = search(db, {
  term: 'headphones',
  where: {
    'meta.rating': {
      gte: 4.5
    }
  }
})

Combining Filters with Search Modes

const results = search(db, {
  term: 'headphones',
  mode: 'fulltext',
  where: {
    price: {
      lt: 100
    },
    inStock: true
  }
})
const results = search(db, {
  mode: 'vector',
  vector: {
    value: embeddings,
    property: 'embedding'
  },
  where: {
    price: {
      lt: 100
    }
  }
})
const results = search(db, {
  mode: 'hybrid',
  term: 'headphones',
  vector: {
    value: embeddings,
    property: 'embedding'
  },
  where: {
    category: 'electronics',
    price: {
      between: [50, 150]
    }
  }
})

Performance Considerations

1

Index Frequently Filtered Properties

Properties used in filters should be indexed for best performance:
create({
  schema: {
    price: 'number',    // Automatically indexed
    category: 'string'  // Automatically indexed
  }
})
2

Use Specific Operators

Use eq for exact matches when possible, as it’s faster than range queries
3

Minimize Complex Nesting

Deep nesting of logical operators can impact performance. Keep filters as simple as possible.
4

Filter Before Search

Filters are applied during search, reducing the result set efficiently

Common Use Cases

E-commerce

Filter products by price range, category, and availability

Content Filtering

Filter articles by date, author, or category

User Search

Find users by role, status, or registration date

Inventory

Filter items by stock status, location, or quantity

Geosearch

Filter by geographic location and radius

Facets

Generate filter options from search results

Sorting

Sort filtered results by properties

Full-Text Search

Combine filters with text search

Build docs developers (and LLMs) love