You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tag fields provide exact match search capabilities with high performance and memory efficiency. Use tag fields when you need to filter documents by specific values without the complexity of full-text search tokenization.
22
22
23
-
Tag fields interpret text as a simple list of *tags* delimited by a [separator](#separator-options) character (comma "`,`" by default). This approach enables simpler [tokenization]({{< relref "/develop/ai/search-and-query/advanced-concepts/escaping/#tokenization-rules-for-tag-fields" >}}) and encoding, making tag indexes much more efficient than full-text indexes. Note: even though tag and text fields both use text, they are two separate field types and so you don't query them the same way.
23
+
Tag fields interpret text as a simple list of *tags* delimited by a [separator](#separator-options) character. This approach enables simpler [tokenization]({{< relref "/develop/ai/search-and-query/advanced-concepts/escaping/#tokenization-rules-for-tag-fields" >}}) and encoding, making tag indexes much more efficient than full-text indexes. Note: even though tag and text fields both use text, they are two separate field types and so you don't query them the same way.
24
+
25
+
{{% alert title="Important: Different defaults for HASH vs JSON" color="warning" %}}
26
+
- The default separator for hash documents is a comma (`,`).
27
+
- There is no default separator for JSON documents. You must explicitly specify one if needed.
28
+
29
+
Specifying a tag from the text `"foo,bar"` behaves differently:
30
+
- For hash documents, two tags are created: `"foo"` and `"bar"`.
31
+
- For JSON documents, one tag is created: `"foo,bar"` (unless you add `SEPARATOR ","`).
Copy file name to clipboardExpand all lines: content/develop/ai/search-and-query/indexing/_index.md
+97-3Lines changed: 97 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -167,14 +167,71 @@ For more information about search queries, see [Search query syntax]({{< relref
167
167
[`FT.SEARCH`]({{< relref "commands/ft.search/" >}}) queries require `attribute` modifiers. Don't use JSONPath expressions in queries because the query parser doesn't fully support them.
168
168
{{% /alert %}}
169
169
170
+
## Understanding TAG field behavior: hash versus JSON
171
+
172
+
TAG fields behave differently depending on whether you're indexing hash or JSON documents. This difference is a common source of confusion.
173
+
174
+
### Hash documents
175
+
176
+
```sql
177
+
# HASH: Comma is the default separator
178
+
HSET product:1 category "Electronics,Gaming,PC"
179
+
FT.CREATE products ON HASH PREFIX 1 product: SCHEMA category TAG
180
+
181
+
# Result: Creates 3 separate tags: "Electronics", "Gaming", "PC"
182
+
FT.SEARCH products '@category:{Gaming}'# ✅ Finds the document
183
+
```
184
+
185
+
### JSON documents
186
+
187
+
```sql
188
+
# JSON: No default separator - the entire string becomes one tag
FT.CREATE products ON JSON PREFIX 1 product: SCHEMA $.category[*] AS category TAG
216
+
217
+
# Result: Creates 3 separate tags: "Electronics", "Gaming", "PC"
218
+
FT.SEARCH products '@category:{Gaming}'# ✅ Finds the document
219
+
```
220
+
170
221
## Index JSON arrays as TAG
171
222
172
-
The preferred method for indexing a JSON field with multivalued terms is using JSON arrays. Each value of the array is indexed, and those values must be scalars. If you want to index string or boolean values as TAGs within a JSON array, use the [JSONPath]({{< relref "/develop/data-types/json/path" >}}) wildcard operator.
223
+
For JSON documents, you have two approaches to create TAG fields with multiple values:
173
224
174
-
To index an item's list of available colors, specify the JSONPath `$.colors.*` in the `SCHEMA` definition during index creation:
225
+
### Approach 1: JSON arrays (recommended)
226
+
227
+
The preferred method for indexing multiple tag values is using JSON arrays. Each array element becomes a separate tag value. Use the [JSONPath]({{< relref "/develop/data-types/json/path" >}}) wildcard operator `[*]` to index array elements.
175
228
176
229
```sql
177
-
127.0.0.1:6379>FT.CREATE itemIdx2 ON JSON PREFIX 1 item: SCHEMA $.colors.*AS colors TAG $.name AS name TEXT $.description as description TEXT
230
+
# Create index with array indexing
231
+
127.0.0.1:6379>FT.CREATE itemIdx2 ON JSON PREFIX 1 item: SCHEMA $.colors[*] AS colors TAG $.name AS name TEXT $.description as description TEXT
232
+
233
+
# The JSON data uses arrays
234
+
# Each array element ("black", "silver") becomes a separate tag
178
235
```
179
236
180
237
Now you can search for silver headphones:
@@ -187,6 +244,43 @@ Now you can search for silver headphones:
187
244
2) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"]}"
188
245
```
189
246
247
+
### Approach 2: strings with explicit separators
248
+
249
+
You can also use comma-separated strings, but you must explicitly specify the `SEPARATOR`:
0 commit comments