Skip to content

Commit a8c11e8

Browse files
cheeseandcerealgkorland
authored andcommitted
feat: Add 'alter schema add' support (#38)
* add alter schema add support * do gendoc update
1 parent 26b64fc commit a8c11e8

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class Client(object):
102102
NUMERIC = 'NUMERIC'
103103

104104
CREATE_CMD = 'FT.CREATE'
105+
ALTER_CMD = 'FT.ALTER'
105106
SEARCH_CMD = 'FT.SEARCH'
106107
ADD_CMD = 'FT.ADD'
107108
DROP_CMD = 'FT.DROP'
@@ -200,6 +201,21 @@ def create_index(self, fields, no_term_offsets=False,
200201

201202
return self.redis.execute_command(*args)
202203

204+
def alter_schema_add(self, fields):
205+
"""
206+
Alter the existing search index by adding new fields. The index must already exist.
207+
208+
### Parameters:
209+
210+
- **fields**: a list of Field objects to add for the index
211+
"""
212+
213+
args = [self.ALTER_CMD, self.index_name, 'SCHEMA', 'ADD']
214+
215+
args += list(itertools.chain(*(f.redis_args() for f in fields)))
216+
217+
return self.redis.execute_command(*args)
218+
203219
def drop_index(self):
204220
"""
205221
Drop the index if it exists

test/test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,31 @@ def testTags(self):
494494
res = client.search(q)
495495
self.assertEqual(1, res.total)
496496

497+
def testAlterSchemaAdd(self):
498+
conn = self.redis()
499+
500+
with conn as r:
501+
# Creating a client with a given index name
502+
client = Client('alterIdx', port=conn.port)
503+
client.redis.flushdb()
504+
505+
# Creating the index definition and schema
506+
client.create_index((TextField('title'),))
507+
508+
# Using alter to add a field
509+
client.alter_schema_add((TextField('body'),))
510+
511+
# Indexing a document
512+
client.add_document('doc1', title = 'MyTitle', body = 'Some content only in the body')
513+
514+
# Searching with parameter only in the body (the added field)
515+
q = Query("only in the body")
516+
517+
# Ensure we find the result searching on the added body field
518+
res = client.search(q)
519+
self.assertEqual(1, res.total)
520+
521+
497522
if __name__ == '__main__':
498523

499524
unittest.main()

0 commit comments

Comments
 (0)