Skip to content

Commit 9d0ea7e

Browse files
authored
Merge branch 'master' into fix_textfield
2 parents cc2ef1c + 051fcdc commit 9d0ea7e

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

API.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,41 @@ Add a single document to the index.
195195
NOTE: Geo points shoule be encoded as strings of "lon,lat"
196196

197197

198+
### aggregate
199+
```py
200+
201+
def aggregate(self, query)
202+
203+
```
204+
205+
206+
207+
Issue an aggregation query
208+
209+
### Parameters
210+
211+
**query**: This can be either an `AggeregateRequest`, or a `Cursor`
212+
213+
An `AggregateResult` object is returned. You can access the rows from its
214+
`rows` property, which will always yield the rows of the result
215+
216+
217+
### alter\_schema\_add
218+
```py
219+
220+
def alter_schema_add(self, fields)
221+
222+
```
223+
224+
225+
226+
Alter the existing search index by adding new fields. The index must already exist.
227+
228+
### Parameters:
229+
230+
- **fields**: a list of Field objects to add for the index
231+
232+
198233
### batch\_indexer
199234
```py
200235

redisearch/client.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ class TagField(Field):
8484
See http://redisearch.io/Tags/
8585
"""
8686
def __init__(self, name, separator = ',', no_index=False):
87-
Field.__init__(self, name, Field.TAG)
88-
if separator != ',':
89-
self.args.append(Field.SEPARATOR)
90-
self.args.append(separator)
87+
args = [Field.TAG, Field.SEPARATOR, separator]
9188

9289
if no_index:
93-
self.args.append(Field.NOINDEX)
90+
args.append(Field.NOINDEX)
91+
92+
Field.__init__(self, name, *args)
9493

9594

9695
class Client(object):
@@ -102,6 +101,7 @@ class Client(object):
102101
NUMERIC = 'NUMERIC'
103102

104103
CREATE_CMD = 'FT.CREATE'
104+
ALTER_CMD = 'FT.ALTER'
105105
SEARCH_CMD = 'FT.SEARCH'
106106
ADD_CMD = 'FT.ADD'
107107
DROP_CMD = 'FT.DROP'
@@ -200,6 +200,21 @@ def create_index(self, fields, no_term_offsets=False,
200200

201201
return self.redis.execute_command(*args)
202202

203+
def alter_schema_add(self, fields):
204+
"""
205+
Alter the existing search index by adding new fields. The index must already exist.
206+
207+
### Parameters:
208+
209+
- **fields**: a list of Field objects to add for the index
210+
"""
211+
212+
args = [self.ALTER_CMD, self.index_name, 'SCHEMA', 'ADD']
213+
214+
args += list(itertools.chain(*(f.redis_args() for f in fields)))
215+
216+
return self.redis.execute_command(*args)
217+
203218
def drop_index(self):
204219
"""
205220
Drop the index if it exists
@@ -367,4 +382,4 @@ def aggregate(self, query):
367382
rows = raw[1:]
368383

369384
res = AggregateResult(rows, cursor, schema)
370-
return res
385+
return res

test/test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,30 @@ def testTextFieldSortableNostem(self):
510510
self.assertIn(b'SORTABLE', response['fields'][0])
511511
self.assertIn(b'NOSTEM', response['fields'][0])
512512

513+
def testAlterSchemaAdd(self):
514+
conn = self.redis()
515+
516+
with conn as r:
517+
# Creating a client with a given index name
518+
client = Client('alterIdx', port=conn.port)
519+
client.redis.flushdb()
520+
521+
# Creating the index definition and schema
522+
client.create_index((TextField('title'),))
523+
524+
# Using alter to add a field
525+
client.alter_schema_add((TextField('body'),))
526+
527+
# Indexing a document
528+
client.add_document('doc1', title = 'MyTitle', body = 'Some content only in the body')
529+
530+
# Searching with parameter only in the body (the added field)
531+
q = Query("only in the body")
532+
533+
# Ensure we find the result searching on the added body field
534+
res = client.search(q)
535+
self.assertEqual(1, res.total)
536+
513537
if __name__ == '__main__':
514538

515539
unittest.main()

0 commit comments

Comments
 (0)