|
5 | 5 | order: 6 |
6 | 6 | --- |
7 | 7 |
|
| 8 | +import { FileTree } from "~/components" |
| 9 | + |
8 | 10 | Metadata filtering narrows down search results based on metadata, so only relevant content is retrieved. The filter narrows down results prior to retrieval, so that you only query the scope of documents that matter. |
9 | 11 |
|
10 | 12 | Here is an example of metadata filtering using [Workers Binding](/autorag/usage/workers-binding/) but it can be easily adapted to use the [REST API](/autorag/usage/rest-api/) instead. |
@@ -34,13 +36,13 @@ const answer = await env.AI.autorag("my-autorag").search({ |
34 | 36 |
|
35 | 37 | You can currently filter by the `folder` and `timestamp` of an R2 object. Currently, custom metadata attributes are not supported. |
36 | 38 |
|
37 | | -### `folder` |
| 39 | +### Folder |
38 | 40 |
|
39 | 41 | The directory to the object. For example, the `folder` of the object at `llama/logistics/llama-logistics.mdx` is `llama/logistics/`. Note that the `folder` does not include a leading `/`. |
40 | 42 |
|
41 | 43 | Note that `folder` filter only includes files exactly in that folder, so files in subdirectories are not included. For example, specifying `folder: "llama/"` will match files in `llama/` but does not match files in `llama/logistics`. |
42 | 44 |
|
43 | | -### `timestamp` |
| 45 | +### Timestamp |
44 | 46 |
|
45 | 47 | The timestamp indicating when the object was last modified. Comparisons are supported using a 13-digit Unix timestamp (milliseconds), but values will be rounded to 10 digits (seconds). For example, `1735689600999` or `2025-01-01 00:00:00.999 UTC` will be rounded down to `1735689600000`, corresponding to `2025-01-01 00:00:00 UTC`. |
46 | 48 |
|
@@ -91,6 +93,50 @@ Note the following limitations with the compound operators: |
91 | 93 | - Only the `eq` operator is allowed. |
92 | 94 | - All conditions must filter on the **same key** (for example, all on `folder`) |
93 | 95 |
|
| 96 | +### "Starts with" filter for folders |
| 97 | + |
| 98 | +You can use "starts with" filtering on the `folder` metadata attribute to search for all files and subfolders within a specific path. |
| 99 | + |
| 100 | +For example, consider this file structure: |
| 101 | + |
| 102 | +<FileTree> |
| 103 | +- customer-a |
| 104 | + - profile.md |
| 105 | + - contracts |
| 106 | + - property |
| 107 | + - contract-1.pdf |
| 108 | +</FileTree> |
| 109 | + |
| 110 | +If you were to filter using an `eq` (equals) operator with `value: "customer-a/"`, it would only match files directly within that folder, like `profile.md`. It would not include files in subfolders like `customer-a/contracts/`. |
| 111 | + |
| 112 | +To recursively filter for all items starting with the path `customer-a/`, you can use the following compound filter: |
| 113 | + |
| 114 | +```js |
| 115 | +filters: { |
| 116 | + type: "and", |
| 117 | + filters: [ |
| 118 | + { |
| 119 | + type: "gt", |
| 120 | + key: "folder", |
| 121 | + value: "customer-a//", |
| 122 | + }, |
| 123 | + { |
| 124 | + type: "lte", |
| 125 | + key: "folder", |
| 126 | + value: "customer-a/z", |
| 127 | + }, |
| 128 | + ], |
| 129 | + }, |
| 130 | +``` |
| 131 | + |
| 132 | +This filter identifies paths starting with `customer-a/` by using: |
| 133 | + |
| 134 | +- The `and` condition to combine the effects of the `gt` and `lte` conditions. |
| 135 | +- The `gt` condition to include paths greater than the `/` ASCII character. |
| 136 | +- The `lte` condition to include paths less than and including the lower case `z` ASCII character. |
| 137 | + |
| 138 | +Together, these conditions effectively select paths that begin with the provided path value. |
| 139 | + |
94 | 140 | ## Response |
95 | 141 |
|
96 | 142 | You can see the metadata attributes of your retrieved data in the response under the property `attributes` for each retrieved chunk. For example: |
|
0 commit comments