Skip to content

feat: Add double metaphone phonetic matcher #55

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 2 commits into from
Feb 17, 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
6 changes: 5 additions & 1 deletion redisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Field(object):
SORTABLE = 'SORTABLE'
NOINDEX = 'NOINDEX'
SEPARATOR = 'SEPARATOR'
PHONETIC = 'PHONETIC'

def __init__(self, name, *args):
self.name = name
Expand All @@ -37,14 +38,17 @@ class TextField(Field):
NOSTEM = 'NOSTEM'

def __init__(self, name, weight=1.0, sortable=False, no_stem=False,
no_index=False):
no_index=False, phonetic_matcher=None):
args = [Field.TEXT, Field.WEIGHT, weight]
if no_stem:
args.append(self.NOSTEM)
if sortable:
args.append(Field.SORTABLE)
if no_index:
args.append(self.NOINDEX)
if phonetic_matcher and phonetic_matcher in ['dm:en', 'dm:fr', 'dm:pt', 'dm:es']:
args.append(self.PHONETIC)
args.append(phonetic_matcher)

if no_index and not sortable:
raise ValueError('Non-Sortable non-Indexable fields are ignored')
Expand Down
29 changes: 29 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,35 @@ def testDictOps(self):
# Remove rest of the items before reload
client.dict_del('custom_dict', *res)

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

with conn as r:
# Creating a client with a given index name
client = Client('myIndex', port=conn.port)
client.redis.flushdb()

client.create_index((TextField('name'),))

client.add_document('doc1', name='Jon')
client.add_document('doc2', name='John')

res = client.search(Query("Jon"))
self.assertEqual(1, len(res.docs))
self.assertEqual('Jon', res.docs[0].name)

# Drop and create index with phonetic matcher
client.redis.flushdb()

client.create_index((TextField('name', phonetic_matcher='dm:en'),))

client.add_document('doc1', name='Jon')
client.add_document('doc2', name='John')

res = client.search(Query("Jon"))
self.assertEqual(2, len(res.docs))
self.assertEqual(['John', 'Jon'], sorted([d.name for d in res.docs]))

def testGet(self):
client = self.getCleanClient('idx')
client.create_index((TextField('f1'), TextField('f2')))
Expand Down