-
Notifications
You must be signed in to change notification settings - Fork 89
docs(examples): add metadata filter operator snippets #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { runMetadataFilterExample } from "./shared"; | ||
|
|
||
| runMetadataFilterExample({ | ||
| operator: "$and", | ||
| description: "Use $and to require multiple metadata filters to match together.", | ||
| query: "running shoes available in New York", | ||
| filter: { | ||
| $and: [ | ||
| { field: "category", condition: { $eq: "shoes" } }, | ||
| { field: "city", condition: { $eq: "new-york" } }, | ||
| ], | ||
| }, | ||
| }).catch((error) => { | ||
| console.error(error); | ||
| process.exitCode = 1; | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { runMetadataFilterExample } from "./shared"; | ||
|
|
||
| runMetadataFilterExample({ | ||
| operator: "$eq", | ||
| description: "Use $eq to match documents whose metadata field has one exact value.", | ||
| query: "running shoes for city training", | ||
| filter: { | ||
| field: "category", | ||
| condition: { $eq: "shoes" }, | ||
| }, | ||
| }).catch((error) => { | ||
| console.error(error); | ||
| process.exitCode = 1; | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { runMetadataFilterExample } from "./shared"; | ||
|
|
||
| runMetadataFilterExample({ | ||
| operator: "$in", | ||
| description: "Use $in to match documents whose metadata value is in an allowed list.", | ||
| query: "portable gear for commuters", | ||
| filter: { | ||
| field: "city", | ||
| condition: { $in: ["new-york", "seattle"] }, | ||
| }, | ||
| }).catch((error) => { | ||
| console.error(error); | ||
| process.exitCode = 1; | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { runMetadataFilterExample } from "./shared"; | ||
|
|
||
| runMetadataFilterExample({ | ||
| operator: "$near", | ||
| description: "Use $near to match location metadata within a radius in meters.", | ||
| query: "city products near Times Square", | ||
| filter: { | ||
| field: "location", | ||
| condition: { $near: "40.7580,-73.9855,5000" }, | ||
| }, | ||
| }).catch((error) => { | ||
| console.error(error); | ||
| process.exitCode = 1; | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { MossClient, type DocumentInfo } from "@moss-dev/moss"; | ||
| import { config } from "dotenv"; | ||
|
|
||
| config(); | ||
|
|
||
| type MetadataFilter = Record<string, unknown>; | ||
|
|
||
| interface MetadataFilterExample { | ||
| operator: string; | ||
| description: string; | ||
| query: string; | ||
| filter: MetadataFilter; | ||
| } | ||
|
|
||
| const documents: DocumentInfo[] = [ | ||
| { | ||
| id: "shoe-nyc-001", | ||
| text: "Breathable road running shoes for daily city training.", | ||
| metadata: { | ||
| category: "shoes", | ||
| brand: "swiftfit", | ||
| city: "new-york", | ||
| location: "40.7580,-73.9855", | ||
| }, | ||
| }, | ||
| { | ||
| id: "shoe-denver-002", | ||
| text: "Trail running shoes built for rocky mountain paths.", | ||
| metadata: { | ||
| category: "shoes", | ||
| brand: "peakstride", | ||
| city: "denver", | ||
| location: "39.7392,-104.9903", | ||
| }, | ||
| }, | ||
| { | ||
| id: "bag-nyc-003", | ||
| text: "Lightweight commuter backpack with a laptop sleeve.", | ||
| metadata: { | ||
| category: "bags", | ||
| brand: "urbanpack", | ||
| city: "new-york", | ||
| location: "40.7505,-73.9934", | ||
| }, | ||
| }, | ||
| { | ||
| id: "bag-seattle-004", | ||
| text: "Compact travel duffel with a separate shoe compartment.", | ||
| metadata: { | ||
| category: "bags", | ||
| brand: "swiftfit", | ||
| city: "seattle", | ||
| location: "47.6205,-122.3493", | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| function requireEnv(name: string): string { | ||
| const value = process.env[name]?.trim(); | ||
|
|
||
| if (!value) { | ||
| throw new Error(`Missing ${name}. Add it to .env before running this example.`); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| export async function runMetadataFilterExample(example: MetadataFilterExample): Promise<void> { | ||
| const projectId = requireEnv("MOSS_PROJECT_ID"); | ||
| const projectKey = requireEnv("MOSS_PROJECT_KEY"); | ||
| const indexName = `metadata-filter-${example.operator.slice(1)}-${Date.now()}`; | ||
| const client = new MossClient(projectId, projectKey); | ||
| let indexCreated = false; | ||
|
|
||
| try { | ||
| console.log(`Moss metadata filter example: ${example.operator}`); | ||
| console.log(example.description); | ||
|
|
||
| console.log(`\nCreating temporary index: ${indexName}`); | ||
| await client.createIndex(indexName, documents, { modelId: "moss-minilm" }); | ||
| indexCreated = true; | ||
|
|
||
| console.log("Loading index locally for filtered query..."); | ||
| await client.loadIndex(indexName); | ||
|
|
||
| console.log(`\nQuery: ${example.query}`); | ||
| console.log(`Filter: ${JSON.stringify(example.filter)}`); | ||
|
|
||
| const results = await client.query(indexName, example.query, { | ||
| topK: 5, | ||
| alpha: 0.5, | ||
| filter: example.filter, | ||
| }); | ||
|
|
||
| console.log(`\nFound ${results.docs.length} result(s) in ${results.timeTakenInMs}ms:`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Results that omit optional Prompt for AI agents |
||
| results.docs.forEach((doc, index) => { | ||
| const preview = doc.text.length > 80 ? `${doc.text.slice(0, 80)}...` : doc.text; | ||
| console.log(`${index + 1}. [${doc.id}] score=${doc.score.toFixed(3)} ${preview}`); | ||
| console.log(` metadata=${JSON.stringify(doc.metadata ?? {})}`); | ||
| }); | ||
| } finally { | ||
| if (indexCreated) { | ||
| console.log(`\nDeleting temporary index: ${indexName}`); | ||
| await client.deleteIndex(indexName); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: When loading or querying fails and deletion also fails, the Prompt for AI agentsThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CONSIDER ```ts |
||
| } | ||
| } | ||
|
Comment on lines
+101
to
+106
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: A failure during upload, build, or polling can leave the remotely initialized temporary index behind because
indexCreatedremains false until the entire multi-stagecreateIndexcall resolves. Cleanup eligibility should be tracked once remote creation may have started, with a best-effort existence check/delete on failure.Prompt for AI agents