@mastra/pg 1.2.0-alpha.0 → 1.3.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +180 -0
- package/dist/docs/SKILL.md +28 -25
- package/dist/docs/{SOURCE_MAP.json → assets/SOURCE_MAP.json} +1 -1
- package/dist/docs/{memory/03-semantic-recall.md → references/docs-memory-semantic-recall.md} +33 -17
- package/dist/docs/{memory/01-storage.md → references/docs-memory-storage.md} +29 -39
- package/dist/docs/{memory/02-working-memory.md → references/docs-memory-working-memory.md} +16 -27
- package/dist/docs/{rag/01-overview.md → references/docs-rag-overview.md} +2 -4
- package/dist/docs/{rag/03-retrieval.md → references/docs-rag-retrieval.md} +26 -53
- package/dist/docs/{rag/02-vector-databases.md → references/docs-rag-vector-databases.md} +198 -202
- package/dist/docs/{memory/04-reference.md → references/reference-memory-memory-class.md} +28 -14
- package/dist/docs/references/reference-processors-message-history-processor.md +85 -0
- package/dist/docs/references/reference-processors-semantic-recall-processor.md +123 -0
- package/dist/docs/references/reference-processors-working-memory-processor.md +154 -0
- package/dist/docs/{rag/04-reference.md → references/reference-rag-metadata-filters.md} +26 -179
- package/dist/docs/references/reference-storage-composite.md +235 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-postgresql.md +529 -0
- package/dist/docs/{tools/01-reference.md → references/reference-tools-vector-query-tool.md} +137 -118
- package/dist/docs/{vectors/01-reference.md → references/reference-vectors-pg.md} +115 -14
- package/dist/index.cjs +1998 -217
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1998 -219
- package/dist/index.js.map +1 -1
- package/dist/storage/db/constraint-utils.d.ts +16 -0
- package/dist/storage/db/constraint-utils.d.ts.map +1 -0
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/domains/agents/index.d.ts +9 -12
- package/dist/storage/domains/agents/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +7 -1
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts +33 -0
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts +33 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +3 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +5 -6
- package/dist/docs/README.md +0 -36
- package/dist/docs/processors/01-reference.md +0 -296
- package/dist/docs/storage/01-reference.md +0 -905
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# MessageHistory
|
|
2
|
+
|
|
3
|
+
The `MessageHistory` is a **hybrid processor** that handles both retrieval and persistence of message history. On input, it fetches historical messages from storage and prepends them to the conversation. On output, it persists new messages to storage.
|
|
4
|
+
|
|
5
|
+
## Usage example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { MessageHistory } from "@mastra/core/processors";
|
|
9
|
+
|
|
10
|
+
const processor = new MessageHistory({
|
|
11
|
+
storage: memoryStorage,
|
|
12
|
+
lastMessages: 50,
|
|
13
|
+
});
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Constructor parameters
|
|
17
|
+
|
|
18
|
+
**options:** (`MessageHistoryOptions`): Configuration options for the message history processor
|
|
19
|
+
|
|
20
|
+
### Options
|
|
21
|
+
|
|
22
|
+
**storage:** (`MemoryStorage`): Storage instance for retrieving and persisting messages
|
|
23
|
+
|
|
24
|
+
**lastMessages?:** (`number`): Maximum number of historical messages to retrieve. If not specified, retrieves all messages
|
|
25
|
+
|
|
26
|
+
## Returns
|
|
27
|
+
|
|
28
|
+
**id:** (`string`): Processor identifier set to 'message-history'
|
|
29
|
+
|
|
30
|
+
**name:** (`string`): Processor display name set to 'MessageHistory'
|
|
31
|
+
|
|
32
|
+
**processInput:** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>`): Fetches historical messages from storage and adds them to the message list
|
|
33
|
+
|
|
34
|
+
**processOutputResult:** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise<MessageList>`): Persists new messages (user input and assistant response) to storage, excluding system messages
|
|
35
|
+
|
|
36
|
+
## Extended usage example
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Agent } from "@mastra/core/agent";
|
|
40
|
+
import { MessageHistory } from "@mastra/core/processors";
|
|
41
|
+
import { PostgresStorage } from "@mastra/pg";
|
|
42
|
+
|
|
43
|
+
const storage = new PostgresStorage({
|
|
44
|
+
connectionString: process.env.DATABASE_URL,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export const agent = new Agent({
|
|
48
|
+
name: "memory-agent",
|
|
49
|
+
instructions: "You are a helpful assistant with conversation memory",
|
|
50
|
+
model: "openai:gpt-4o",
|
|
51
|
+
inputProcessors: [
|
|
52
|
+
new MessageHistory({
|
|
53
|
+
storage,
|
|
54
|
+
lastMessages: 100,
|
|
55
|
+
}),
|
|
56
|
+
],
|
|
57
|
+
outputProcessors: [
|
|
58
|
+
new MessageHistory({
|
|
59
|
+
storage,
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Behavior
|
|
66
|
+
|
|
67
|
+
### Input processing
|
|
68
|
+
|
|
69
|
+
1. Retrieves `threadId` from the request context
|
|
70
|
+
2. Fetches historical messages from storage (ordered by creation date, descending)
|
|
71
|
+
3. Filters out system messages (they should not be stored in the database)
|
|
72
|
+
4. Merges historical messages with incoming messages, avoiding duplicates by ID
|
|
73
|
+
5. Adds historical messages with `source: 'memory'` tag
|
|
74
|
+
|
|
75
|
+
### Output processing
|
|
76
|
+
|
|
77
|
+
1. Retrieves `threadId` from the request context
|
|
78
|
+
2. Skips persistence if `readOnly` is set in memory config
|
|
79
|
+
3. Filters out incomplete tool calls from messages
|
|
80
|
+
4. Persists new user input and assistant response messages to storage
|
|
81
|
+
5. Updates the thread's `updatedAt` timestamp
|
|
82
|
+
|
|
83
|
+
## Related
|
|
84
|
+
|
|
85
|
+
- [Guardrails](https://mastra.ai/docs/agents/guardrails)
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# SemanticRecall
|
|
2
|
+
|
|
3
|
+
The `SemanticRecall` is a **hybrid processor** that enables semantic search over conversation history using vector embeddings. On input, it performs semantic search to find relevant historical messages. On output, it creates embeddings for new messages to enable future semantic retrieval.
|
|
4
|
+
|
|
5
|
+
## Usage example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { SemanticRecall } from "@mastra/core/processors";
|
|
9
|
+
import { openai } from "@ai-sdk/openai";
|
|
10
|
+
|
|
11
|
+
const processor = new SemanticRecall({
|
|
12
|
+
storage: memoryStorage,
|
|
13
|
+
vector: vectorStore,
|
|
14
|
+
embedder: openai.embedding("text-embedding-3-small"),
|
|
15
|
+
topK: 5,
|
|
16
|
+
messageRange: 2,
|
|
17
|
+
scope: "resource",
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Constructor parameters
|
|
22
|
+
|
|
23
|
+
**options:** (`SemanticRecallOptions`): Configuration options for the semantic recall processor
|
|
24
|
+
|
|
25
|
+
### Options
|
|
26
|
+
|
|
27
|
+
**storage:** (`MemoryStorage`): Storage instance for retrieving messages
|
|
28
|
+
|
|
29
|
+
**vector:** (`MastraVector`): Vector store for semantic search
|
|
30
|
+
|
|
31
|
+
**embedder:** (`MastraEmbeddingModel<string>`): Embedder for generating query embeddings
|
|
32
|
+
|
|
33
|
+
**topK?:** (`number`): Number of most similar messages to retrieve
|
|
34
|
+
|
|
35
|
+
**messageRange?:** (`number | { before: number; after: number }`): Number of context messages to include before/after each match. Can be a single number (same for both) or an object with separate values
|
|
36
|
+
|
|
37
|
+
**scope?:** (`'thread' | 'resource'`): Scope of semantic search. 'thread' searches within the current thread only. 'resource' searches across all threads for the resource
|
|
38
|
+
|
|
39
|
+
**threshold?:** (`number`): Minimum similarity score threshold (0-1). Messages below this threshold are filtered out
|
|
40
|
+
|
|
41
|
+
**indexName?:** (`string`): Index name for the vector store. If not provided, auto-generated based on embedder model
|
|
42
|
+
|
|
43
|
+
**logger?:** (`IMastraLogger`): Optional logger instance for structured logging
|
|
44
|
+
|
|
45
|
+
## Returns
|
|
46
|
+
|
|
47
|
+
**id:** (`string`): Processor identifier set to 'semantic-recall'
|
|
48
|
+
|
|
49
|
+
**name:** (`string`): Processor display name set to 'SemanticRecall'
|
|
50
|
+
|
|
51
|
+
**processInput:** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>`): Performs semantic search on historical messages and adds relevant context to the message list
|
|
52
|
+
|
|
53
|
+
**processOutputResult:** (`(args: { messages: MastraDBMessage[]; messageList?: MessageList; abort: (reason?: string) => never; tracingContext?: TracingContext; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>`): Creates embeddings for new messages to enable future semantic search
|
|
54
|
+
|
|
55
|
+
## Extended usage example
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Agent } from "@mastra/core/agent";
|
|
59
|
+
import { SemanticRecall, MessageHistory } from "@mastra/core/processors";
|
|
60
|
+
import { PostgresStorage } from "@mastra/pg";
|
|
61
|
+
import { PgVector } from "@mastra/pg";
|
|
62
|
+
import { openai } from "@ai-sdk/openai";
|
|
63
|
+
|
|
64
|
+
const storage = new PostgresStorage({
|
|
65
|
+
id: 'pg-storage',
|
|
66
|
+
connectionString: process.env.DATABASE_URL,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const vector = new PgVector({
|
|
70
|
+
id: 'pg-vector',
|
|
71
|
+
connectionString: process.env.DATABASE_URL,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const semanticRecall = new SemanticRecall({
|
|
75
|
+
storage,
|
|
76
|
+
vector,
|
|
77
|
+
embedder: openai.embedding("text-embedding-3-small"),
|
|
78
|
+
topK: 5,
|
|
79
|
+
messageRange: { before: 2, after: 1 },
|
|
80
|
+
scope: "resource",
|
|
81
|
+
threshold: 0.7,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
export const agent = new Agent({
|
|
85
|
+
name: "semantic-memory-agent",
|
|
86
|
+
instructions: "You are a helpful assistant with semantic memory recall",
|
|
87
|
+
model: "openai:gpt-4o",
|
|
88
|
+
inputProcessors: [
|
|
89
|
+
semanticRecall,
|
|
90
|
+
new MessageHistory({ storage, lastMessages: 50 }),
|
|
91
|
+
],
|
|
92
|
+
outputProcessors: [
|
|
93
|
+
semanticRecall,
|
|
94
|
+
new MessageHistory({ storage }),
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Behavior
|
|
100
|
+
|
|
101
|
+
### Input processing
|
|
102
|
+
|
|
103
|
+
1. Extracts the user query from the last user message
|
|
104
|
+
2. Generates embeddings for the query
|
|
105
|
+
3. Performs vector search to find semantically similar messages
|
|
106
|
+
4. Retrieves matched messages along with surrounding context (based on `messageRange`)
|
|
107
|
+
5. For `scope: 'resource'`, formats cross-thread messages as a system message with timestamps
|
|
108
|
+
6. Adds recalled messages with `source: 'memory'` tag
|
|
109
|
+
|
|
110
|
+
### Output processing
|
|
111
|
+
|
|
112
|
+
1. Extracts text content from new user and assistant messages
|
|
113
|
+
2. Generates embeddings for each message
|
|
114
|
+
3. Stores embeddings in the vector store with metadata (message ID, thread ID, resource ID, role, content, timestamp)
|
|
115
|
+
4. Uses LRU caching for embeddings to avoid redundant API calls
|
|
116
|
+
|
|
117
|
+
### Cross-thread recall
|
|
118
|
+
|
|
119
|
+
When `scope` is set to `'resource'`, the processor can recall messages from other threads. These cross-thread messages are formatted as a system message with timestamps and conversation labels to provide context about when and where the conversation occurred.
|
|
120
|
+
|
|
121
|
+
## Related
|
|
122
|
+
|
|
123
|
+
- [Guardrails](https://mastra.ai/docs/agents/guardrails)
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# WorkingMemory
|
|
2
|
+
|
|
3
|
+
The `WorkingMemory` is an **input processor** that injects working memory data as a system message. It retrieves persistent information from storage and formats it as instructions for the LLM, enabling the agent to maintain context about users across conversations.
|
|
4
|
+
|
|
5
|
+
## Usage example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { WorkingMemory } from "@mastra/core/processors";
|
|
9
|
+
|
|
10
|
+
const processor = new WorkingMemory({
|
|
11
|
+
storage: memoryStorage,
|
|
12
|
+
scope: "resource",
|
|
13
|
+
template: {
|
|
14
|
+
format: "markdown",
|
|
15
|
+
content: `# User Profile
|
|
16
|
+
- **Name**:
|
|
17
|
+
- **Preferences**:
|
|
18
|
+
- **Goals**:
|
|
19
|
+
`,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Constructor parameters
|
|
25
|
+
|
|
26
|
+
**options:** (`Options`): Configuration options for the working memory processor
|
|
27
|
+
|
|
28
|
+
### Options
|
|
29
|
+
|
|
30
|
+
**storage:** (`MemoryStorage`): Storage instance for retrieving working memory data
|
|
31
|
+
|
|
32
|
+
**template?:** (`WorkingMemoryTemplate`): Template defining the format and structure of working memory
|
|
33
|
+
|
|
34
|
+
**scope?:** (`'thread' | 'resource'`): Scope of working memory. 'thread' scopes to current thread, 'resource' shares across all threads for the resource
|
|
35
|
+
|
|
36
|
+
**useVNext?:** (`boolean`): Use the next-generation instruction format with improved guidelines
|
|
37
|
+
|
|
38
|
+
**readOnly?:** (`boolean`): When true, working memory is provided as read-only context. The data is injected into the conversation but without the updateWorkingMemory tool or update instructions. Useful for agents that should reference working memory without modifying it.
|
|
39
|
+
|
|
40
|
+
**templateProvider?:** (`{ getWorkingMemoryTemplate(args: { memoryConfig?: MemoryConfig }): Promise<WorkingMemoryTemplate | null> }`): Dynamic template provider for runtime template resolution
|
|
41
|
+
|
|
42
|
+
**logger?:** (`IMastraLogger`): Optional logger instance for structured logging
|
|
43
|
+
|
|
44
|
+
### WorkingMemoryTemplate
|
|
45
|
+
|
|
46
|
+
**format:** (`'markdown' | 'json'`): Format of the working memory content
|
|
47
|
+
|
|
48
|
+
**content:** (`string`): Template content defining the structure of working memory data
|
|
49
|
+
|
|
50
|
+
## Returns
|
|
51
|
+
|
|
52
|
+
**id:** (`string`): Processor identifier set to 'working-memory'
|
|
53
|
+
|
|
54
|
+
**name:** (`string`): Processor display name set to 'WorkingMemory'
|
|
55
|
+
|
|
56
|
+
**defaultWorkingMemoryTemplate:** (`string`): The default markdown template used when no custom template is provided
|
|
57
|
+
|
|
58
|
+
**processInput:** (`(args: { messages: MastraDBMessage[]; messageList: MessageList; abort: (reason?: string) => never; requestContext?: RequestContext }) => Promise<MessageList | MastraDBMessage[]>`): Retrieves working memory and adds it as a system message to the message list
|
|
59
|
+
|
|
60
|
+
## Extended usage example
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { Agent } from "@mastra/core/agent";
|
|
64
|
+
import { WorkingMemory, MessageHistory } from "@mastra/core/processors";
|
|
65
|
+
import { PostgresStorage } from "@mastra/pg";
|
|
66
|
+
|
|
67
|
+
const storage = new PostgresStorage({
|
|
68
|
+
connectionString: process.env.DATABASE_URL,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export const agent = new Agent({
|
|
72
|
+
name: "personalized-agent",
|
|
73
|
+
instructions: "You are a helpful assistant that remembers user preferences",
|
|
74
|
+
model: "openai:gpt-4o",
|
|
75
|
+
inputProcessors: [
|
|
76
|
+
new WorkingMemory({
|
|
77
|
+
storage,
|
|
78
|
+
scope: "resource",
|
|
79
|
+
template: {
|
|
80
|
+
format: "markdown",
|
|
81
|
+
content: `# User Information
|
|
82
|
+
- **Name**:
|
|
83
|
+
- **Location**:
|
|
84
|
+
- **Preferences**:
|
|
85
|
+
- **Communication Style**:
|
|
86
|
+
- **Current Projects**:
|
|
87
|
+
`,
|
|
88
|
+
},
|
|
89
|
+
}),
|
|
90
|
+
new MessageHistory({ storage, lastMessages: 50 }),
|
|
91
|
+
],
|
|
92
|
+
outputProcessors: [
|
|
93
|
+
new MessageHistory({ storage }),
|
|
94
|
+
],
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## JSON format example
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { WorkingMemory } from "@mastra/core/processors";
|
|
102
|
+
|
|
103
|
+
const processor = new WorkingMemory({
|
|
104
|
+
storage: memoryStorage,
|
|
105
|
+
scope: "resource",
|
|
106
|
+
template: {
|
|
107
|
+
format: "json",
|
|
108
|
+
content: JSON.stringify({
|
|
109
|
+
user: {
|
|
110
|
+
name: { type: "string" },
|
|
111
|
+
preferences: { type: "object" },
|
|
112
|
+
goals: { type: "array" },
|
|
113
|
+
},
|
|
114
|
+
}),
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Behavior
|
|
120
|
+
|
|
121
|
+
### Input processing
|
|
122
|
+
|
|
123
|
+
1. Retrieves `threadId` and `resourceId` from the request context
|
|
124
|
+
|
|
125
|
+
2. Based on scope, fetches working memory from either:
|
|
126
|
+
|
|
127
|
+
- Thread metadata (`scope: 'thread'`)
|
|
128
|
+
- Resource record (`scope: 'resource'`)
|
|
129
|
+
|
|
130
|
+
3. Resolves the template (from provider, options, or default)
|
|
131
|
+
|
|
132
|
+
4. Generates system instructions based on mode:
|
|
133
|
+
|
|
134
|
+
- **Normal mode**: Includes guidelines for storing/updating information, template structure, and current data
|
|
135
|
+
- **Read-only mode** (`readOnly: true`): Includes only the current data as context without update instructions
|
|
136
|
+
|
|
137
|
+
5. Adds the instruction as a system message with `source: 'memory'` tag
|
|
138
|
+
|
|
139
|
+
### Working memory updates
|
|
140
|
+
|
|
141
|
+
Working memory updates happen through the `updateWorkingMemory` tool provided by the Memory class, not through this processor. The processor only handles injecting the current working memory state into conversations.
|
|
142
|
+
|
|
143
|
+
### Default template
|
|
144
|
+
|
|
145
|
+
If no template is provided, the processor uses a default markdown template with fields for:
|
|
146
|
+
|
|
147
|
+
- First Name, Last Name
|
|
148
|
+
- Location, Occupation
|
|
149
|
+
- Interests, Goals
|
|
150
|
+
- Events, Facts, Projects
|
|
151
|
+
|
|
152
|
+
## Related
|
|
153
|
+
|
|
154
|
+
- [Guardrails](https://mastra.ai/docs/agents/guardrails)
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
> API reference for rag - 1 entries
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Reference: Metadata Filters
|
|
9
|
-
|
|
10
|
-
> Documentation for metadata filtering capabilities in Mastra, which allow for precise querying of vector search results across different vector stores.
|
|
1
|
+
# Metadata Filters
|
|
11
2
|
|
|
12
3
|
Mastra provides a unified metadata filtering syntax across all vector stores, based on MongoDB/Sift query syntax. Each vector store translates these filters into their native format.
|
|
13
4
|
|
|
@@ -35,188 +26,42 @@ const results = await store.query({
|
|
|
35
26
|
|
|
36
27
|
## Supported Operators
|
|
37
28
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
supportedBy: ["All except Couchbase"],
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
name: "$gte",
|
|
61
|
-
description: "Greater than or equal",
|
|
62
|
-
example: "{ rating: { $gte: 4.5 } }",
|
|
63
|
-
supportedBy: ["All except Couchbase"],
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
name: "$lt",
|
|
67
|
-
description: "Less than",
|
|
68
|
-
example: "{ stock: { $lt: 20 } }",
|
|
69
|
-
supportedBy: ["All except Couchbase"],
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
name: "$lte",
|
|
73
|
-
description: "Less than or equal",
|
|
74
|
-
example: "{ priority: { $lte: 3 } }",
|
|
75
|
-
supportedBy: ["All except Couchbase"],
|
|
76
|
-
},
|
|
77
|
-
]}
|
|
78
|
-
/>
|
|
79
|
-
|
|
80
|
-
<OperatorsTable
|
|
81
|
-
title="Array Operators"
|
|
82
|
-
operators={[
|
|
83
|
-
{
|
|
84
|
-
name: "$in",
|
|
85
|
-
description: "Matches any value in array",
|
|
86
|
-
example: '{ category: { $in: ["A", "B"] } }',
|
|
87
|
-
supportedBy: ["All except Couchbase"],
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
name: "$nin",
|
|
91
|
-
description: "Matches none of the values",
|
|
92
|
-
example: '{ status: { $nin: ["deleted", "archived"] } }',
|
|
93
|
-
supportedBy: ["All except Couchbase"],
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
name: "$all",
|
|
97
|
-
description: "Matches arrays containing all elements",
|
|
98
|
-
example: '{ tags: { $all: ["urgent", "high"] } }',
|
|
99
|
-
supportedBy: ["Astra", "Pinecone", "Upstash", "MongoDB"],
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
name: "$elemMatch",
|
|
103
|
-
description: "Matches array elements meeting criteria",
|
|
104
|
-
example: "{ scores: { $elemMatch: { $gt: 80 } } }",
|
|
105
|
-
supportedBy: ["libSQL", "PgVector", "MongoDB"],
|
|
106
|
-
},
|
|
107
|
-
]}
|
|
108
|
-
/>
|
|
109
|
-
|
|
110
|
-
<OperatorsTable
|
|
111
|
-
title="Logical Operators"
|
|
112
|
-
operators={[
|
|
113
|
-
{
|
|
114
|
-
name: "$and",
|
|
115
|
-
description: "Logical AND",
|
|
116
|
-
example: "{ $and: [{ price: { $gt: 100 } }, { stock: { $gt: 0 } }] }",
|
|
117
|
-
supportedBy: ["All except Vectorize, Couchbase"],
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
name: "$or",
|
|
121
|
-
description: "Logical OR",
|
|
122
|
-
example: '{ $or: [{ status: "active" }, { priority: "high" }] }',
|
|
123
|
-
supportedBy: ["All except Vectorize, Couchbase"],
|
|
124
|
-
},
|
|
125
|
-
{
|
|
126
|
-
name: "$not",
|
|
127
|
-
description: "Logical NOT",
|
|
128
|
-
example: "{ price: { $not: { $lt: 100 } } }",
|
|
129
|
-
supportedBy: [
|
|
130
|
-
"Astra",
|
|
131
|
-
"Qdrant",
|
|
132
|
-
"Upstash",
|
|
133
|
-
"PgVector",
|
|
134
|
-
"libSQL",
|
|
135
|
-
"MongoDB",
|
|
136
|
-
],
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
name: "$nor",
|
|
140
|
-
description: "Logical NOR",
|
|
141
|
-
example: '{ $nor: [{ status: "deleted" }, { archived: true }] }',
|
|
142
|
-
supportedBy: ["Qdrant", "Upstash", "PgVector", "libSQL", "MongoDB"],
|
|
143
|
-
},
|
|
144
|
-
]}
|
|
145
|
-
/>
|
|
146
|
-
|
|
147
|
-
<OperatorsTable
|
|
148
|
-
title="Element Operators"
|
|
149
|
-
operators={[
|
|
150
|
-
{
|
|
151
|
-
name: "$exists",
|
|
152
|
-
description: "Matches documents with field",
|
|
153
|
-
example: "{ rating: { $exists: true } }",
|
|
154
|
-
supportedBy: ["All except Vectorize, Chroma, Couchbase"],
|
|
155
|
-
},
|
|
156
|
-
]}
|
|
157
|
-
/>
|
|
158
|
-
|
|
159
|
-
<OperatorsTable
|
|
160
|
-
title="Custom Operators"
|
|
161
|
-
operators={[
|
|
162
|
-
{
|
|
163
|
-
name: "$contains",
|
|
164
|
-
description: "Text contains substring",
|
|
165
|
-
example: '{ description: { $contains: "sale" } }',
|
|
166
|
-
supportedBy: ["Upstash", "libSQL", "PgVector"],
|
|
167
|
-
},
|
|
168
|
-
{
|
|
169
|
-
name: "$regex",
|
|
170
|
-
description: "Regular expression match",
|
|
171
|
-
example: '{ name: { $regex: "^test" } }',
|
|
172
|
-
supportedBy: ["Qdrant", "PgVector", "Upstash", "MongoDB"],
|
|
173
|
-
},
|
|
174
|
-
{
|
|
175
|
-
name: "$size",
|
|
176
|
-
description: "Array length check",
|
|
177
|
-
example: "{ tags: { $size: { $gt: 2 } } }",
|
|
178
|
-
supportedBy: ["Astra", "libSQL", "PgVector", "MongoDB"],
|
|
179
|
-
},
|
|
180
|
-
{
|
|
181
|
-
name: "$geo",
|
|
182
|
-
description: "Geospatial query",
|
|
183
|
-
example: '{ location: { $geo: { type: "radius", ... } } }',
|
|
184
|
-
supportedBy: ["Qdrant"],
|
|
185
|
-
},
|
|
186
|
-
{
|
|
187
|
-
name: "$datetime",
|
|
188
|
-
description: "Datetime range query",
|
|
189
|
-
example: '{ created: { $datetime: { range: { gt: "2024-01-01" } } } }',
|
|
190
|
-
supportedBy: ["Qdrant"],
|
|
191
|
-
},
|
|
192
|
-
{
|
|
193
|
-
name: "$hasId",
|
|
194
|
-
description: "Vector ID existence check",
|
|
195
|
-
example: '{ $hasId: ["id1", "id2"] }',
|
|
196
|
-
supportedBy: ["Qdrant"],
|
|
197
|
-
},
|
|
198
|
-
{
|
|
199
|
-
name: "$hasVector",
|
|
200
|
-
description: "Vector existence check",
|
|
201
|
-
example: "{ $hasVector: true }",
|
|
202
|
-
supportedBy: ["Qdrant"],
|
|
203
|
-
},
|
|
204
|
-
]}
|
|
205
|
-
/>
|
|
29
|
+
### Basic Comparison
|
|
30
|
+
|
|
31
|
+
`$eq`Matches values equal to specified value{ age: { $eq: 25 } }Supported by: All except Couchbase`$ne`Matches values not equal{ status: { $ne: 'inactive' } }Supported by: All except Couchbase`$gt`Greater than{ price: { $gt: 100 } }Supported by: All except Couchbase`$gte`Greater than or equal{ rating: { $gte: 4.5 } }Supported by: All except Couchbase`$lt`Less than{ stock: { $lt: 20 } }Supported by: All except Couchbase`$lte`Less than or equal{ priority: { $lte: 3 } }Supported by: All except Couchbase
|
|
32
|
+
|
|
33
|
+
### Array Operators
|
|
34
|
+
|
|
35
|
+
`$in`Matches any value in array{ category: { $in: \["A", "B"] } }Supported by: All except Couchbase`$nin`Matches none of the values{ status: { $nin: \["deleted", "archived"] } }Supported by: All except Couchbase`$all`Matches arrays containing all elements{ tags: { $all: \["urgent", "high"] } }Supported by: Astra, Pinecone, Upstash, MongoDB`$elemMatch`Matches array elements meeting criteria{ scores: { $elemMatch: { $gt: 80 } } }Supported by: libSQL, PgVector, MongoDB
|
|
36
|
+
|
|
37
|
+
### Logical Operators
|
|
38
|
+
|
|
39
|
+
`$and`Logical AND{ $and: \[{ price: { $gt: 100 } }, { stock: { $gt: 0 } }] }Supported by: All except Vectorize, Couchbase`$or`Logical OR{ $or: \[{ status: "active" }, { priority: "high" }] }Supported by: All except Vectorize, Couchbase`$not`Logical NOT{ price: { $not: { $lt: 100 } } }Supported by: Astra, Qdrant, Upstash, PgVector, libSQL, MongoDB`$nor`Logical NOR{ $nor: \[{ status: "deleted" }, { archived: true }] }Supported by: Qdrant, Upstash, PgVector, libSQL, MongoDB
|
|
40
|
+
|
|
41
|
+
### Element Operators
|
|
42
|
+
|
|
43
|
+
`$exists`Matches documents with field{ rating: { $exists: true } }Supported by: All except Vectorize, Chroma, Couchbase
|
|
44
|
+
|
|
45
|
+
### Custom Operators
|
|
46
|
+
|
|
47
|
+
`$contains`Text contains substring{ description: { $contains: "sale" } }Supported by: Upstash, libSQL, PgVector`$regex`Regular expression match{ name: { $regex: "^test" } }Supported by: Qdrant, PgVector, Upstash, MongoDB`$size`Array length check{ tags: { $size: { $gt: 2 } } }Supported by: Astra, libSQL, PgVector, MongoDB`$geo`Geospatial query{ location: { $geo: { type: "radius", ... } } }Supported by: Qdrant`$datetime`Datetime range query{ created: { $datetime: { range: { gt: "2024-01-01" } } } }Supported by: Qdrant`$hasId`Vector ID existence check{ $hasId: \["id1", "id2"] }Supported by: Qdrant`$hasVector`Vector existence check{ $hasVector: true }Supported by: Qdrant
|
|
206
48
|
|
|
207
49
|
## Common Rules and Restrictions
|
|
208
50
|
|
|
209
51
|
1. Field names cannot:
|
|
52
|
+
|
|
210
53
|
- Contain dots (.) unless referring to nested fields
|
|
211
54
|
- Start with $ or contain null characters
|
|
212
55
|
- Be empty strings
|
|
213
56
|
|
|
214
57
|
2. Values must be:
|
|
58
|
+
|
|
215
59
|
- Valid JSON types (string, number, boolean, object, array)
|
|
216
60
|
- Not undefined
|
|
217
61
|
- Properly typed for the operator (e.g., numbers for numeric comparisons)
|
|
218
62
|
|
|
219
63
|
3. Logical operators:
|
|
64
|
+
|
|
220
65
|
- Must contain valid conditions
|
|
221
66
|
- Cannot be empty
|
|
222
67
|
- Must be properly nested
|
|
@@ -229,6 +74,7 @@ const results = await store.query({
|
|
|
229
74
|
- Invalid: `{ "field": { "$gt": { "$and": [{...}] } } }`
|
|
230
75
|
|
|
231
76
|
4. $not operator:
|
|
77
|
+
|
|
232
78
|
- Must be an object
|
|
233
79
|
- Cannot be empty
|
|
234
80
|
- Can be used at field level or top level
|
|
@@ -236,6 +82,7 @@ const results = await store.query({
|
|
|
236
82
|
- Valid: `{ "field": { "$not": { "$eq": "value" } } }`
|
|
237
83
|
|
|
238
84
|
5. Operator nesting:
|
|
85
|
+
|
|
239
86
|
- Logical operators must contain field conditions, not direct operators
|
|
240
87
|
- Valid: `{ "$and": [{ "field": { "$gt": 100 } }] }`
|
|
241
88
|
- Invalid: `{ "$and": [{ "$gt": 100 }] }`
|
|
@@ -265,7 +112,7 @@ const results = await store.query({
|
|
|
265
112
|
- Field names cannot contain dots (.) or start with $
|
|
266
113
|
- Field names limited to 512 characters
|
|
267
114
|
- Vectors must be re-upserted after creating new metadata indexes to be included in filtered results
|
|
268
|
-
- Range queries may have reduced accuracy with very large datasets (
|
|
115
|
+
- Range queries may have reduced accuracy with very large datasets (\~10M+ vectors)
|
|
269
116
|
|
|
270
117
|
### libSQL
|
|
271
118
|
|