Skip to content

Commit d582ac9

Browse files
authored
Add FT.alias functionality (#99)
* index alias add * add tests for v < 2.0 on aliases * search 2.0 handles aliases differently
1 parent 64217cd commit d582ac9

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

redisearch/client.py

+38
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ class Client(object):
174174
MGET_CMD = 'FT.MGET'
175175
CONFIG_CMD = 'FT.CONFIG'
176176
TAGVALS_CMD = 'FT.TAGVALS'
177+
ALIAS_ADD_CMD = 'FT.ALIASADD'
178+
ALIAS_UPDATE_CMD = 'FT.ALIASUPDATE'
179+
ALIAS_DEL_CMD = 'FT.ALIASDEL'
177180

178181
NOOFFSETS = 'NOOFFSETS'
179182
NOFIELDS = 'NOFIELDS'
@@ -659,3 +662,38 @@ def tagvals(self, tagfield):
659662
cmd = self.redis.execute_command(self.TAGVALS_CMD, self.index_name, tagfield)
660663
return cmd
661664

665+
def aliasadd(self, alias):
666+
"""
667+
Alias a search index - will fail if alias already exists
668+
669+
### Parameters
670+
671+
- **alias**: Name of the alias to create
672+
"""
673+
674+
cmd = self.redis.execute_command(self.ALIAS_ADD_CMD, alias, self.index_name)
675+
return cmd
676+
677+
def aliasupdate(self, alias):
678+
"""
679+
Updates an alias - will fail if alias does not already exist
680+
681+
### Parameters
682+
683+
- **alias**: Name of the alias to create
684+
"""
685+
686+
cmd = self.redis.execute_command(self.ALIAS_UPDATE_CMD, alias, self.index_name)
687+
return cmd
688+
689+
def aliasdel(self, alias):
690+
"""
691+
Removes an alias to a search index
692+
693+
### Parameters
694+
695+
- **alias**: Name of the alias to delete
696+
"""
697+
698+
cmd = self.redis.execute_command(self.ALIAS_DEL_CMD, alias)
699+
return cmd

test/test.py

+82
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,88 @@ def testSummarize(self):
564564
self.assertEqual('ACT I SCENE I. London. The palace. Enter <b>KING</b> <b>HENRY</b>, LORD JOHN OF LANCASTER, the EARL of WESTMORELAND, SIR... ',
565565
doc.txt)
566566

567+
def testAlias(self):
568+
conn = self.redis()
569+
with conn as r:
570+
if check_version_2(r):
571+
572+
index1 = Client('testAlias', port=conn.port)
573+
index1.redis.flushdb()
574+
index2 = Client('testAlias2', port=conn.port)
575+
576+
index1.redis.hset("index1:lonestar", mapping = {'name': 'lonestar'})
577+
index2.redis.hset("index2:yogurt", mapping = {'name': 'yogurt'})
578+
579+
time.sleep(2)
580+
581+
def1 =IndexDefinition(prefix=['index1:'],score_field='name')
582+
def2 =IndexDefinition(prefix=['index2:'],score_field='name')
583+
584+
index1.create_index((TextField('name'),),definition=def1)
585+
index2.create_index((TextField('name'),),definition=def2)
586+
587+
res = index1.search('*').docs[0]
588+
self.assertEqual('index1:lonestar', res.id)
589+
590+
# create alias and check for results
591+
index1.aliasadd("spaceballs")
592+
alias_client = Client('spaceballs', port=conn.port)
593+
res = alias_client.search('*').docs[0]
594+
self.assertEqual('index1:lonestar', res.id)
595+
596+
# We should throw an exception when trying to add an alias that already exists
597+
with self.assertRaises(Exception) as context:
598+
index2.aliasadd('spaceballs')
599+
self.assertEqual('Alias already exists', str(context.exception))
600+
601+
#update alias and ensure new results
602+
index2.aliasupdate("spaceballs")
603+
alias_client2 = Client('spaceballs', port=conn.port)
604+
res = alias_client2.search('*').docs[0]
605+
self.assertEqual('index2:yogurt', res.id)
606+
607+
index2.aliasdel("spaceballs")
608+
with self.assertRaises(Exception) as context:
609+
alias_client2.search('*').docs[0]
610+
self.assertEqual('spaceballs: no such index', str(context.exception))
611+
612+
else:
613+
614+
# Creating a client with one index
615+
index1 = Client('testAlias', port=conn.port)
616+
index1.redis.flushdb()
617+
618+
index1.create_index((TextField('txt'),))
619+
index1.add_document('doc1', txt = 'text goes here')
620+
621+
index2 = Client('testAlias2', port=conn.port)
622+
index2.create_index((TextField('txt'),))
623+
index2.add_document('doc2', txt = 'text goes here')
624+
625+
626+
# add the actual alias and check
627+
index1.aliasadd('myalias')
628+
alias_client = Client('myalias', port=conn.port)
629+
res = alias_client.search('*').docs[0]
630+
self.assertEqual('doc1', res.id)
631+
632+
# We should throw an exception when trying to add an alias that already exists
633+
with self.assertRaises(Exception) as context:
634+
index2.aliasadd('myalias')
635+
self.assertEqual('Alias already exists', str(context.exception))
636+
637+
# update the alias and ensure we get doc2
638+
index2.aliasupdate('myalias')
639+
alias_client2 = Client('myalias', port=conn.port)
640+
res = alias_client2.search('*').docs[0]
641+
self.assertEqual('doc2', res.id)
642+
643+
# delete the alias and expect an error if we try to query again
644+
index2.aliasdel('myalias')
645+
with self.assertRaises(Exception) as context:
646+
alias_client2.search('*').docs[0]
647+
self.assertEqual('myalias: no such index', str(context.exception))
648+
567649
def testTags(self):
568650
conn = self.redis()
569651

0 commit comments

Comments
 (0)