aboutsummaryrefslogtreecommitdiff
path: root/apps/docs/concepts/filtering.mdx
blob: d222e8498c6fbe116b8de379db6966b29d2aeca9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
---
title: "Organizing & Filtering Memories"
sidebarTitle: "Multi-Tenancy / Filtering"
description: "Use container tags and metadata to organize and retrieve memories"
icon: "users"
---

Supermemory provides two ways to organize your memories:

<CardGroup cols={2}>
  <Card title="Container Tags" icon="folder">
    **Organize memories** into isolated spaces by user, project, or workspace
  </Card>
  <Card title="Metadata Filtering" icon="database">
    **Query memories** by custom properties like category, status, or date
  </Card>
</CardGroup>

Both can be used independently or together for precise filtering.

---

## Container Tags

Container tags create isolated memory spaces. Use them to separate memories by user, project, or any logical boundary.

### Adding Memories with Tags

```typescript
await client.add({
  content: "Meeting notes from Q1 planning",
  containerTags: ["user_123"]
});
```

### Searching with Tags

```typescript
const results = await client.search.documents({
  q: "planning notes",
  containerTags: ["user_123"]
});
```

<Note>
Container tags use **exact array matching**. A memory tagged `["user_123", "project_a"]` won't match a search for just `["user_123"]`.
</Note>

### Recommended Patterns

| Pattern | Example | Use Case |
|---------|---------|----------|
| User isolation | `user_{userId}` | Per-user memories |
| Project grouping | `project_{projectId}` | Project-specific content |
| Hierarchical | `org_{orgId}_team_{teamId}` | Multi-level organization |

<AccordionGroup>
  <Accordion title="More Container Tag Examples">
    ```typescript
    // Multi-tenant SaaS - isolate by organization and user
    await client.add({
      content: "Company policy document",
      containerTags: ["org_acme_user_john"]
    });

    // Search only within that user's org context
    const results = await client.search.documents({
      q: "vacation policy",
      containerTags: ["org_acme_user_john"]
    });

    // Project-based isolation
    await client.add({
      content: "Sprint 5 retrospective notes",
      containerTags: ["project_mobile_app"]
    });

    // Time-based segmentation
    await client.add({
      content: "Q1 2024 financial report",
      containerTags: ["user_cfo_2024_q1"]
    });
    ```

    **API field differences:**
    | Endpoint | Field | Type |
    |----------|-------|------|
    | `/v3/search` | `containerTags` | Array |
    | `/v4/search` | `containerTag` | String |
    | `/v3/documents/list` | `containerTags` | Array |
  </Accordion>
</AccordionGroup>

---

## Metadata

Metadata lets you attach custom properties to memories and filter by them later.

### Adding Memories with Metadata

```typescript
await client.add({
  content: "Technical design document for auth system",
  containerTags: ["user_123"],
  metadata: {
    category: "engineering",
    priority: "high",
    year: 2024
  }
});
```

### Searching with Metadata Filters

Filters must be wrapped in `AND` or `OR` arrays:

```typescript
const results = await client.search.documents({
  q: "design document",
  containerTags: ["user_123"],
  filters: {
    AND: [
      { key: "category", value: "engineering" },
      { key: "priority", value: "high" }
    ]
  }
});
```

### Filter Types

| Type | Example | Description |
|------|---------|-------------|
| String equality | `{ key: "status", value: "published" }` | Exact match |
| String contains | `{ filterType: "string_contains", key: "title", value: "react" }` | Substring match |
| Numeric | `{ filterType: "numeric", key: "priority", value: "5", numericOperator: ">=" }` | Number comparison |
| Array contains | `{ filterType: "array_contains", key: "tags", value: "important" }` | Check array membership |

### Combining Filters

Use `AND` and `OR` for complex queries:

```typescript
const results = await client.search.documents({
  q: "meeting notes",
  filters: {
    AND: [
      { key: "type", value: "meeting" },
      {
        OR: [
          { key: "team", value: "engineering" },
          { key: "team", value: "product" }
        ]
      }
    ]
  }
});
```

### Excluding Results

Use `negate: true` to exclude matches:

```typescript
const results = await client.search.documents({
  q: "documentation",
  filters: {
    AND: [
      { key: "status", value: "draft", negate: true }
    ]
  }
});
```

<AccordionGroup>
  <Accordion title="More Metadata Filter Examples">
    **String contains (substring search):**
    ```typescript
    // Find documents with "machine learning" in the description
    const results = await client.search.documents({
      q: "AI research",
      filters: {
        AND: [
          {
            filterType: "string_contains",
            key: "description",
            value: "machine learning",
            ignoreCase: true
          }
        ]
      }
    });
    ```

    **Numeric comparisons:**
    ```typescript
    // Find high-priority items created after a specific date
    const results = await client.search.documents({
      q: "tasks",
      filters: {
        AND: [
          {
            filterType: "numeric",
            key: "priority",
            value: "7",
            numericOperator: ">="
          },
          {
            filterType: "numeric",
            key: "created_timestamp",
            value: "1704067200",  // Unix timestamp
            numericOperator: ">="
          }
        ]
      }
    });
    ```

    **Array contains (check array membership):**
    ```typescript
    // Find documents where a specific user is a participant
    const results = await client.search.documents({
      q: "meeting notes",
      filters: {
        AND: [
          {
            filterType: "array_contains",
            key: "participants",
            value: "[email protected]"
          }
        ]
      }
    });
    ```

    **Complex nested filters:**
    ```typescript
    // (category = "tech" OR category = "science") AND status != "archived"
    const results = await client.search.documents({
      q: "research papers",
      filters: {
        AND: [
          {
            OR: [
              { key: "category", value: "tech" },
              { key: "category", value: "science" }
            ]
          },
          { key: "status", value: "archived", negate: true }
        ]
      }
    });
    ```

    **Numeric operator negation mapping:**
    When using `negate: true`, operators flip:
    - `<` becomes `>=`
    - `<=` becomes `>`
    - `>` becomes `<=`
    - `>=` becomes `<`
    - `=` becomes `!=`
  </Accordion>

  <Accordion title="Real-World Patterns">
    **User's work documents from 2024:**
    ```typescript
    const results = await client.search.documents({
      q: "quarterly report",
      containerTags: ["user_123"],
      filters: {
        AND: [
          { key: "category", value: "work" },
          { key: "type", value: "report" },
          { filterType: "numeric", key: "year", value: "2024", numericOperator: "=" }
        ]
      }
    });
    ```

    **Team meeting notes with specific participants:**
    ```typescript
    const results = await client.search.documents({
      q: "sprint planning",
      containerTags: ["project_alpha"],
      filters: {
        AND: [
          { key: "type", value: "meeting" },
          {
            OR: [
              { filterType: "array_contains", key: "participants", value: "alice" },
              { filterType: "array_contains", key: "participants", value: "bob" }
            ]
          }
        ]
      }
    });
    ```

    **Exclude drafts and deprecated content:**
    ```typescript
    const results = await client.search.documents({
      q: "documentation",
      filters: {
        AND: [
          { key: "status", value: "draft", negate: true },
          { filterType: "string_contains", key: "content", value: "deprecated", negate: true },
          { filterType: "array_contains", key: "tags", value: "archived", negate: true }
        ]
      }
    });
    ```
  </Accordion>
</AccordionGroup>

---

## Quick Reference

### When Adding Memories

```typescript
await client.add({
  content: "Your content here",
  containerTags: ["user_123"],        // Isolation
  metadata: { key: "value" }          // Custom properties
});
```

### When Searching

```typescript
const results = await client.search.documents({
  q: "search query",
  containerTags: ["user_123"],        // Must match exactly
  filters: {                          // Optional metadata filters
    AND: [{ key: "status", value: "published" }]
  }
});
```

### Metadata Key Rules

- Allowed characters: `a-z`, `A-Z`, `0-9`, `_`, `-`, `.`
- Max length: 64 characters
- No spaces or special characters

---

## Next Steps

<CardGroup cols={2}>
  <Card title="Search" icon="search" href="/search">
    Apply filters in search queries
  </Card>
  <Card title="Add Memories" icon="plus" href="/add-memories">
    Add content with container tags and metadata
  </Card>
</CardGroup>