DEV Community

Cover image for LioranDB TypeScript Series #6: Secondary Indexes and Full-Text Indexes
Swaraj Puppalwar
Swaraj Puppalwar

Posted on

LioranDB TypeScript Series #6: Secondary Indexes and Full-Text Indexes

LioranDB TypeScript Series #6: Secondary Indexes and Full-Text Indexes

LioranDB TypeScript Series: Build with a developer-first document database powered by Rust and designed for TypeScript.

Indexes are where your data model meets your query patterns.

LioranDB V2 currently supports secondary indexes and text indexes through the TypeScript driver.

Create a secondary index

await products.createIndex("sku", {
  name: "sku_idx",
  unique: true,
});
Enter fullscreen mode Exit fullscreen mode

Now sku has an explicit index and the uniqueness constraint can protect the collection from duplicate values.

Compound indexes

Queries often depend on more than one field.

await products.createIndex({
  name: "category_price_idx",

  fields: [
    {
      field: "category",
      direction: "asc",
    },
    {
      field: "price",
      direction: "desc",
    },
  ],
});
Enter fullscreen mode Exit fullscreen mode

Design compound indexes around actual query patterns rather than creating every field combination imaginable.

Indexes aren't Pokémon. You don't need to collect them all.

Partial indexes

You can also index only documents matching a filter:

await products.createIndex({
  name: "active_price_idx",
  fields: ["price"],

  partialFilter: {
    active: true,
  },
});
Enter fullscreen mode Exit fullscreen mode

For the right workload, this can keep an index focused on the subset of documents that matters.

Text indexes

Create a text index:

await products.createTextIndex("title", {
  normalize: true,
  stopwords: ["set"],
});
Enter fullscreen mode Exit fullscreen mode

Then search:

const results = await products.find({
  $text: {
    $search: "Marker",
    $field: "title",
  },
}).toArray();
Enter fullscreen mode Exit fullscreen mode

Inspect indexes

const indexes = await products.listIndexes();

console.log(indexes);
Enter fullscreen mode Exit fullscreen mode

Index definitions can expose information including:

  • name
  • fields
  • uniqueness
  • sparse configuration
  • partial filters
  • build state
  • build progress
  • whether the index is a text index

Drop an index

await products.dropIndex("category_price_idx");
Enter fullscreen mode Exit fullscreen mode

Index design rule

Start from the query:

await products.find({
  category: "stationery",
  price: {
    $gte: 100,
  },
});
Enter fullscreen mode Exit fullscreen mode

Then ask what index structure helps that query.

Don't start by indexing everything and hoping the database sorts out the consequences later.

Pre-alpha note

Indexing behavior and capabilities will continue evolving as LioranDB moves from pre-alpha toward alpha.

Treat the current release as something to experiment with, benchmark against your own workload and give feedback on.

Resources

Documentation: https://docs.liorandb.com
Website: https://liorandb.com
Creator: https://github.com/UltronTheAI


Previous: Part 5 → Cursors & Text Search
Next: Part 7 → Aggregation Pipelines


Enter fullscreen mode Exit fullscreen mode

Top comments (0)