Skip to content

Commit b50f023

Browse files
committed
feat: Add double metaphone phonetic matcher
1 parent 9156402 commit b50f023

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

redisearch/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Field(object):
2121
SORTABLE = 'SORTABLE'
2222
NOINDEX = 'NOINDEX'
2323
SEPARATOR = 'SEPARATOR'
24+
PHONETIC = 'PHONETIC'
2425

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

3940
def __init__(self, name, weight=1.0, sortable=False, no_stem=False,
40-
no_index=False):
41+
no_index=False, phonetic_matcher=None):
4142
args = [Field.TEXT, Field.WEIGHT, weight]
4243
if no_stem:
4344
args.append(self.NOSTEM)
4445
if sortable:
4546
args.append(Field.SORTABLE)
4647
if no_index:
4748
args.append(self.NOINDEX)
49+
if phonetic_matcher and phonetic_matcher in ['dm:en', 'dm:fr', 'dm:pt', 'dm:es']:
50+
args.append(self.PHONETIC)
51+
args.append(phonetic_matcher)
4852

4953
if no_index and not sortable:
5054
raise ValueError('Non-Sortable non-Indexable fields are ignored')

test/test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,35 @@ def testDictOps(self):
568568
# Remove rest of the items before reload
569569
client.dict_del('custom_dict', *res)
570570

571+
def testPhoneticMatcher(self):
572+
conn = self.redis()
573+
574+
with conn as r:
575+
# Creating a client with a given index name
576+
client = Client('myIndex', port=conn.port)
577+
client.redis.flushdb()
578+
579+
client.create_index((TextField('name'),))
580+
581+
client.add_document('doc1', name='Jon')
582+
client.add_document('doc2', name='John')
583+
584+
res = client.search(Query("Jon"))
585+
self.assertEqual(1, len(res.docs))
586+
self.assertEqual('Jon', res.docs[0].name)
587+
588+
# Drop and create index with phonetic matcher
589+
client.redis.flushdb()
590+
591+
client.create_index((TextField('name', phonetic_matcher='dm:en'),))
592+
593+
client.add_document('doc1', name='Jon')
594+
client.add_document('doc2', name='John')
595+
596+
res = client.search(Query("Jon"))
597+
self.assertEqual(2, len(res.docs))
598+
self.assertEqual(['John', 'Jon'], sorted([d.name for d in res.docs]))
599+
571600

572601
if __name__ == '__main__':
573602

0 commit comments

Comments
 (0)