Filtering allows you to narrow search results by fields such as category, date, or user permissions. Vector operations filter fields stored inside metadata; document operations filter fields stored directly on the document.
On an index with full-text search enabled for body, combine text scoring with a document field filter:
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone();
const index = pc.index({ name: 'documents-example', namespace: 'articles' });
const results = await index.documents.search({
scoreBy: [{ type: 'text', fields: ['body'], query: 'apple' }],
filter: { category: { $eq: 'gardening' } },
topK: 5,
includeFields: ['title', 'body'],
});
console.log(results.matches);
See Working with Documents for index setup, full-text search, and filtering document fetches, updates, and deletes. The remaining examples in this guide use vector metadata.
For more information, see Filter by metadata.
Pinecone supports the following filter operators:
$eq - Equal to (number, string, boolean)$ne - Not equal to (number, string, boolean)$gt - Greater than (number)$gte - Greater than or equal to (number)$lt - Less than (number)$lte - Less than or equal to (number)$in - In array (string, number)$nin - Not in array (string, number)import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({ apiKey: 'YOUR_API_KEY' });
const indexModel = await pc.indexes.describe('my-index');
const index = pc.index({ host: indexModel.host });
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
genre: { $eq: 'drama' },
},
});
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({ apiKey: 'YOUR_API_KEY' });
const indexModel = await pc.indexes.describe('my-index');
const index = pc.index({ host: indexModel.host });
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
year: { $gte: 2000, $lte: 2010 },
},
});
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({ apiKey: 'YOUR_API_KEY' });
const indexModel = await pc.indexes.describe('my-index');
const index = pc.index({ host: indexModel.host });
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
genre: { $in: ['comedy', 'documentary', 'drama'] },
},
});
By default, multiple filter conditions are combined with AND:
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({ apiKey: 'YOUR_API_KEY' });
const indexModel = await pc.indexes.describe('my-index');
const index = pc.index({ host: indexModel.host });
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
genre: { $eq: 'drama' },
year: { $gte: 2000 },
rating: { $gte: 7.0 },
},
});
You can also explicitly use $and:
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
$and: [
{ genre: { $eq: 'drama' } },
{ year: { $gte: 2000 } },
{ rating: { $gte: 7.0 } },
],
},
});
Use $or to match vectors that satisfy any of multiple conditions:
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({ apiKey: 'YOUR_API_KEY' });
const indexModel = await pc.indexes.describe('my-index');
const index = pc.index({ host: indexModel.host });
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
$or: [
{ genre: { $eq: 'comedy' } },
{ genre: { $eq: 'drama' } },
{ rating: { $gte: 8.5 } },
],
},
});
You can nest $and and $or operators to create complex filters:
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({ apiKey: 'YOUR_API_KEY' });
const indexModel = await pc.indexes.describe('my-index');
const index = pc.index({ host: indexModel.host });
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
$and: [
{
$or: [{ genre: { $eq: 'comedy' } }, { genre: { $eq: 'drama' } }],
},
{ year: { $gte: 2000 } },
{ rating: { $gte: 7.0 } },
],
},
});
Combine different field types and operators:
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({ apiKey: 'YOUR_API_KEY' });
const indexModel = await pc.indexes.describe('my-index');
const index = pc.index({ host: indexModel.host });
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
category: { $in: ['electronics', 'computers'] },
price: { $gte: 100, $lte: 500 },
inStock: { $eq: true },
brand: { $ne: 'generic' },
},
});
Passing a metadata type to pc.index<T>() types the metadata you write with upsert and update, and the metadata you read back on query matches.
Filters are not covered by that type. The filter property is typed as object, so TypeScript does not check the field names or comparison values inside a filter — a misspelled field name still compiles. Check filter fields against your metadata schema yourself.
import { Pinecone } from '@pinecone-database/pinecone';
const pc = new Pinecone({ apiKey: 'YOUR_API_KEY' });
type MovieMetadata = {
genre: 'comedy' | 'horror' | 'drama' | 'action';
year: number;
rating: number;
inTheaters: boolean;
};
const index = pc.index<MovieMetadata>({ name: 'movies' });
const results = await index.query({
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
filter: {
genre: { $eq: 'drama' },
year: { $gte: 2020 },
},
});
metadataConfig with indexed fields for high-cardinality metadata$ne and $nin may be less efficient than positive filtersFor more details on metadata configuration, see: