@@ -94,15 +94,17 @@ except ResponseError
94
94
Use an instance of ` IndexDefinition ` to define a search index. You only need
95
95
to do this when you create an index.
96
96
97
- Index definitions follow Hashes in your Redis databases. The * prefix* list
98
- you give to ` IndexDefinition ` tells RediSearch which Hash keys to add to that
99
- index.
97
+ RediSearch indexes follow Hashes in your Redis databases by watching * key
98
+ prefixes* . If a Hash whose key starts with one of the search index's
99
+ configured key prefixes is added, updated, or deleted from Redis, RediSearch
100
+ will make those changes in the index. You configure a search index's key
101
+ prefixes using the ` prefix ` parameter of the ` IndexDefinition ` initializer.
100
102
101
103
** NOTE** : Once you create an index, RediSearch will continuously index these
102
- * keys when their Hashes change.
104
+ keys when their Hashes change.
103
105
104
- An ` IndexDefinition ` also takes a * schema* . The schema specifies which fields
105
- to index from within the Hashes that the index follows. The field types are:
106
+ ` IndexDefinition ` also takes a * schema* . The schema specifies which fields to
107
+ index from within the Hashes that the index follows. The field types are:
106
108
107
109
* TextField
108
110
* TagField
@@ -125,7 +127,7 @@ SCHEMA = (
125
127
TextField(" body" )
126
128
)
127
129
128
- client = Client(" myIndex " )
130
+ client = Client(" my-index " )
129
131
130
132
definition = IndexDefinition(prefix = [' blog:' ])
131
133
@@ -166,15 +168,17 @@ client.add_document(
166
168
167
169
### Querying
168
170
169
- Use the ` search() ` method to query a RediSearch index.
171
+ #### Basic queries
170
172
171
- Without specifying a field, your query will perform a full-text search
172
- across all ` TEXT ` fields in the index.
173
+ Use the ` search() ` method to perform basic full-text and field-specific
174
+ searches. This method doesn't take many of the options available to the
175
+ RediSearch ` FT.SEARCH ` command -- read the section on building complex
176
+ queries later in this document for information on how to use those.
173
177
174
178
``` py
175
- # Full-text search
176
179
res = client.search(" evil wizards" )
177
180
```
181
+ #### Result objects
178
182
179
183
Results are wrapped in a ` Result ` object that includes the number of results
180
184
and a list of matching documents.
@@ -186,17 +190,31 @@ and a list of matching documents.
186
190
" Wizard Story 2: Evil Wizards Strike Back"
187
191
```
188
192
189
- You can use the ` Query ` object to build complex queries. For an explanation of
190
- these options, see the [ RediSearch documentation ] ( https://oss.redislabs.com/redisearch/Commands/#ftsearch )
191
- for the ` FT.SEARCH ` command.
193
+ #### Building complex queries
194
+
195
+ You can use the ` Query ` object to build complex queries:
192
196
193
197
``` py
194
198
q = Query(" evil wizards" ).verbatim().no_content().with_scores().paging(0 , 5 )
195
199
res = client.search(q)
196
200
```
197
201
198
- The string you pass into the ` search() ` method or ` Query() ` initializer has
199
- the full range of query syntax available in RediSearch.
202
+ For an explanation of these options, see the [ RediSearch
203
+ documentation] ( https://oss.redislabs.com/redisearch/Commands/#ftsearch ) for
204
+ the ` FT.SEARCH ` command.
205
+
206
+ #### Query syntax
207
+
208
+ The default behavior of queries is to run a full-text search across all
209
+ ` TEXT ` fields in the index for the intersection of all terms in the query.
210
+
211
+ So the example given in the "Basic queries" section of this README,
212
+ ` client.search("evil wizards") ` , run a full-text search for the intersection
213
+ of "evil" and "wizard" in all ` TEXT ` fields.
214
+
215
+ Many more types of queries are possible, however! The string you pass into
216
+ the ` search() ` method or ` Query() ` initializer has the full range of query
217
+ syntax available in RediSearch.
200
218
201
219
For example, a full-text search against a specific ` TEXT ` field in the index
202
220
looks like this:
@@ -216,7 +234,6 @@ To learn more, see the [RediSearch
216
234
documentation] ( https://oss.redislabs.com/redisearch/Query_Syntax/ ) on query
217
235
syntax.
218
236
219
-
220
237
### Aggregations
221
238
222
239
This library contains a programmatic interface to run [ aggregation
@@ -248,8 +265,8 @@ result = client.aggregate(request)
248
265
249
266
#### A redis-cli equivalent query
250
267
251
- The complete aggregation query given as an example in this document looks
252
- like the following when entered using the redis-cli as native Redis commands :
268
+ The aggregation query just given is equivalent to the following
269
+ ` FT.AGGREGATE ` command entered directly into the redis-cli:
253
270
254
271
``` sql
255
272
FT .AGGREGATE books- idx *
@@ -325,16 +342,14 @@ AggregateRequest('*').group_by([], reducers.max("@num_published"))
325
342
```
326
343
327
344
** NOTE** : Aggregation queries require at least one ` group_by() ` method call.
328
- As just mentioned, use ` group_by([], ... ` to run a reducer function over all
329
- results (in other words, to avoid grouping the results).
330
345
331
346
#### Sorting and limiting
332
347
333
348
Using an ` AggregateRequest ` instance, you can sort with the ` sort_by() ` method
334
349
and limit with the ` limit() ` method.
335
350
336
351
For example, finding the average rating of books published each year, sorting
337
- by the average rating for the year, and taking the first ten:
352
+ by the average rating for the year, and returning only the first ten results :
338
353
339
354
``` py
340
355
from redisearch import Client
@@ -351,11 +366,14 @@ request = AggregateRequest('*').group_by(
351
366
c.aggregate(request)
352
367
```
353
368
369
+ ** NOTE** : The first option to ` limit() ` is a zero-based offset, and the second
370
+ option is the number of results to return.
371
+
354
372
#### Filtering
355
373
356
- Filtering is a way to reject results of an aggregation after your reducer functions run.
357
- For example, calculating the average rating of books published each year and only
358
- returning years with an average rating higher than 3:
374
+ Use filtering to reject results of an aggregation query after your reducer
375
+ functions run. For example, calculating the average rating of books published
376
+ each year and only returning years with an average rating higher than 3:
359
377
360
378
``` py
361
379
from redisearch.aggregation import AggregateRequest, Asc
0 commit comments