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
and encoding in the index, which is more efficient than full-text indexing.
21
+
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.
26
22
27
-
The values in tag fields cannot be accessed by general field-less searchand can be used only with a special syntax.
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.
28
24
29
-
The main differences between tag and full-text fields are:
Tag fields excel in scenarios requiring exact matching rather than full-text search. Choose tag fields when you need to index categorical data such as:
1. You can create up to 1024 tag fields per index.
48
+
## Technical details
44
49
45
-
## Creating a tag field
50
+
### Index structure
51
+
-**Compressed storage**: Only document IDs encoded as deltas (1-2 bytes per entry)
52
+
-**No frequencies**: Unlike TEXT fields, tag indexes don't store term frequencies
53
+
-**No positions**: No offset vectors or field flags stored
54
+
-**Limit**: You can create up to 1024 tag fields per index
46
55
47
-
Tag fields can be added to the schema with the following syntax:
56
+
### Tokenization differences
57
+
-**Simple splitting**: Text is split only at separator characters
58
+
-**No stemming**: Words are indexed exactly as written
59
+
-**Case handling**: Optional case-sensitive or case-insensitive matching
60
+
-**No stop words**: All tag values are indexed regardless of content
61
+
62
+
## Create a tag field
63
+
64
+
Add tag fields to your schema using this syntax:
48
65
49
66
```
50
67
FT.CREATE ... SCHEMA ... {field_name} TAG [SEPARATOR {sep}] [CASESENSITIVE]
51
68
```
52
69
53
-
For hashes, SEPARATOR can be any printable ASCII character; the default is a comma (`,`). For JSON, there is no default separator; you must declare one explicitly if needed.
70
+
### Separator options
54
71
55
-
For example:
72
+
-**Hash documents**: Default separator is comma (`,`). You can use any printable ASCII character
73
+
-**JSON documents**: No default separator - you must specify one explicitly if needed
74
+
-**Custom separators**: Use semicolon (`;`), pipe (`|`), or other characters as needed
Notice that including multiple tags in the same clause creates a union of all documents that contain any of the included tags. To create an intersection of documents containing all of the given tags, you should repeat the tag filter several times.
175
+
### OR vs AND logic
110
176
111
-
For example, imagine an index of travelers, with a tag field for the cities each traveler has visited:
177
+
**Single clause (OR logic)**: Find documents with ANY of the specified tags
178
+
```sql
179
+
@cities:{ New York | Los Angeles | Barcelona }
180
+
# Returns: Documents with New York OR Los Angeles OR Barcelona
181
+
```
112
182
183
+
**Multiple clauses (AND logic)**: Find documents with ALL of the specified tags
184
+
```sql
185
+
@cities:{ New York } @cities:{ Los Angeles } @cities:{ Barcelona }
186
+
# Returns: Documents with New York AND Los Angeles AND Barcelona
113
187
```
114
-
FT.CREATE myIndex ON HASH PREFIX 1 traveler: SCHEMA name TEXT cities TAG
188
+
189
+
### Practical example
190
+
191
+
Consider a travel database:
192
+
193
+
```sql
194
+
FT.CREATE travelers ON HASH PREFIX 1 traveler: SCHEMA
195
+
name TEXT
196
+
cities TAG
115
197
116
198
HSET traveler:1 name "John Doe" cities "New York, Barcelona, San Francisco"
199
+
HSET traveler:2 name "Jane Smith" cities "New York, Los Angeles, Tokyo"
117
200
```
118
201
119
-
For this index, the following query will return all the people who visited at least one of the following cities:
120
-
202
+
**Find travelers who visited any of these cities:**
203
+
```sql
204
+
FT.SEARCH travelers "@cities:{ New York | Los Angeles | Barcelona }"
205
+
# Returns: Both John and Jane
121
206
```
122
-
FT.SEARCH myIndex "@cities:{ New York | Los Angeles | Barcelona }"
207
+
208
+
**Find travelers who visited all of these cities:**
209
+
```sql
210
+
FT.SEARCH travelers "@cities:{ New York } @cities:{ Barcelona }"
211
+
# Returns: Only John (has both New York and Barcelona)
123
212
```
124
213
125
-
But the next query will return all people who have visited all three cities:
214
+
## Handle special characters
126
215
127
-
```
128
-
FT.SEARCH myIndex "@cities:{ New York } @cities:{Los Angeles} @cities:{ Barcelona }"
129
-
```
216
+
Tag fields can contain any punctuation except the field separator, but you need to escape certain characters in queries.
130
217
131
-
##Including punctuation and spaces in tags
218
+
### Defining tags with special characters
132
219
133
-
A tag field can contain any punctuation characters except for the field separator.
134
-
You can use punctuation without escaping when you *define* a tag field,
135
-
but you typically need to escape certain characters when you *query* the field
136
-
because the query syntax itself uses the same characters.
137
-
(See [Query syntax]({{< relref "/develop/ai/search-and-query/advanced-concepts/query_syntax#tag-filters" >}})
138
-
for the full set of characters that require escaping.)
220
+
You can store tags with punctuation without escaping:
139
221
140
-
For example, given the following index:
222
+
```sql
223
+
FT.CREATE products ON HASH PREFIX 1 test: SCHEMA tags TAG
141
224
225
+
HSET test:1 tags "Andrew's Top 5,Justin's Top 5,5-Star Rating"
- Learn about [tokenization rules]({{< relref "/develop/ai/search-and-query/advanced-concepts/escaping#tokenization-rules-for-tag-fields" >}}) for tag fields
293
+
- Explore [field and type options]({{< relref "/develop/ai/search-and-query/indexing/field-and-type-options" >}}) for other field types
294
+
- See [query syntax]({{< relref "/develop/ai/search-and-query/advanced-concepts/query_syntax" >}}) for advanced query patterns
0 commit comments