Skip to main content
Use metadata filters to narrow search results to chunks matching specific criteria. Reducing the search space improves relevance and performance. For multi-tenant applications, see Data Segregation.

Filter operators

OperatorDescriptionSupported types
$eqEqual to a specified valueString, number, boolean
$neNot equal to a specified valueString, number, boolean
$gtGreater than a specified valueNumber
$gteGreater than or equal to a specified valueNumber
$ltLess than a specified valueNumber
$lteLess than or equal to a specified valueNumber
$inMatches any value in an arrayString, number
$ninMatches none of the values in an arrayString, number
$allMatches arrays containing all specified elementsArray
$elemMatchMatches if any array element meets the criteriaArray
$existsMatches documents where the field existsBoolean
$andJoins clauses with logical AND-
$orJoins clauses with logical OR-
$notNegates a filter expression-
$norMatches documents that fail all specified conditions-

Basic filtering

Pass a filter object to return only documents with matching metadata values.
const results = await ns.search("disease prevention", {
  filter: {
    category: "digestive system",
  },
});

Using comparison operators

const results = await ns.search("premium products", {
  filter: {
    price: { $gte: 100 },
  },
});

Range queries

Combine operators to filter within a range.
const results = await ns.search("mid-range items", {
  filter: {
    price: { $gte: 50, $lte: 200 },
  },
});

Using array operators

Match documents where a field equals any value in an array.
const results = await ns.search("documentation", {
  filter: {
    category: { $in: ["guides", "tutorials"] },
  },
});
Exclude documents matching any value in an array.
const results = await ns.search("active projects", {
  filter: {
    status: { $nin: ["archived", "deleted"] },
  },
});

Combining filters

Use $and and $or to combine multiple conditions.
// OR: match either condition
const results = await ns.search("tasks", {
  filter: {
    $or: [
      { priority: "high" },
      { status: "overdue" },
    ],
  },
});

// AND: match all conditions
const results = await ns.search("reports", {
  filter: {
    $and: [
      { year: { $gte: 2023 } },
      { department: "engineering" },
    ],
  },
});

Checking field existence

Filter documents based on whether a metadata field exists.
const results = await ns.search("reviewed content", {
  filter: {
    reviewedBy: { $exists: true },
  },
});

Next steps