Skip to content

Add FT.alias functionality #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions redisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ class Client(object):
MGET_CMD = 'FT.MGET'
CONFIG_CMD = 'FT.CONFIG'
TAGVALS_CMD = 'FT.TAGVALS'
ALIAS_ADD_CMD = 'FT.ALIASADD'
ALIAS_UPDATE_CMD = 'FT.ALIASUPDATE'
ALIAS_DEL_CMD = 'FT.ALIASDEL'

NOOFFSETS = 'NOOFFSETS'
NOFIELDS = 'NOFIELDS'
Expand Down Expand Up @@ -659,3 +662,38 @@ def tagvals(self, tagfield):
cmd = self.redis.execute_command(self.TAGVALS_CMD, self.index_name, tagfield)
return cmd

def aliasadd(self, alias):
"""
Alias a search index - will fail if alias already exists

### Parameters

- **alias**: Name of the alias to create
"""

cmd = self.redis.execute_command(self.ALIAS_ADD_CMD, alias, self.index_name)
return cmd

def aliasupdate(self, alias):
"""
Updates an alias - will fail if alias does not already exist

### Parameters

- **alias**: Name of the alias to create
"""

cmd = self.redis.execute_command(self.ALIAS_UPDATE_CMD, alias, self.index_name)
return cmd

def aliasdel(self, alias):
"""
Removes an alias to a search index

### Parameters

- **alias**: Name of the alias to delete
"""

cmd = self.redis.execute_command(self.ALIAS_DEL_CMD, alias)
return cmd
82 changes: 82 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,88 @@ def testSummarize(self):
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... ',
doc.txt)

def testAlias(self):
conn = self.redis()
with conn as r:
if check_version_2(r):

index1 = Client('testAlias', port=conn.port)
index1.redis.flushdb()
index2 = Client('testAlias2', port=conn.port)

index1.redis.hset("index1:lonestar", mapping = {'name': 'lonestar'})
index2.redis.hset("index2:yogurt", mapping = {'name': 'yogurt'})

time.sleep(2)

def1 =IndexDefinition(prefix=['index1:'],score_field='name')
def2 =IndexDefinition(prefix=['index2:'],score_field='name')

index1.create_index((TextField('name'),),definition=def1)
index2.create_index((TextField('name'),),definition=def2)

res = index1.search('*').docs[0]
self.assertEqual('index1:lonestar', res.id)

# create alias and check for results
index1.aliasadd("spaceballs")
alias_client = Client('spaceballs', port=conn.port)
res = alias_client.search('*').docs[0]
self.assertEqual('index1:lonestar', res.id)

# We should throw an exception when trying to add an alias that already exists
with self.assertRaises(Exception) as context:
index2.aliasadd('spaceballs')
self.assertEqual('Alias already exists', str(context.exception))

#update alias and ensure new results
index2.aliasupdate("spaceballs")
alias_client2 = Client('spaceballs', port=conn.port)
res = alias_client2.search('*').docs[0]
self.assertEqual('index2:yogurt', res.id)

index2.aliasdel("spaceballs")
with self.assertRaises(Exception) as context:
alias_client2.search('*').docs[0]
self.assertEqual('spaceballs: no such index', str(context.exception))

else:

# Creating a client with one index
index1 = Client('testAlias', port=conn.port)
index1.redis.flushdb()

index1.create_index((TextField('txt'),))
index1.add_document('doc1', txt = 'text goes here')

index2 = Client('testAlias2', port=conn.port)
index2.create_index((TextField('txt'),))
index2.add_document('doc2', txt = 'text goes here')


# add the actual alias and check
index1.aliasadd('myalias')
alias_client = Client('myalias', port=conn.port)
res = alias_client.search('*').docs[0]
self.assertEqual('doc1', res.id)

# We should throw an exception when trying to add an alias that already exists
with self.assertRaises(Exception) as context:
index2.aliasadd('myalias')
self.assertEqual('Alias already exists', str(context.exception))

# update the alias and ensure we get doc2
index2.aliasupdate('myalias')
alias_client2 = Client('myalias', port=conn.port)
res = alias_client2.search('*').docs[0]
self.assertEqual('doc2', res.id)

# delete the alias and expect an error if we try to query again
index2.aliasdel('myalias')
with self.assertRaises(Exception) as context:
alias_client2.search('*').docs[0]
self.assertEqual('myalias: no such index', str(context.exception))

def testTags(self):
conn = self.redis()

Expand Down