Skip to content

Commit 1885501

Browse files
authored
fix support for RediSearch 2.0 (#74)
* fix support for RediSearch 2.0 * add flushdb since drop index don't delete the data * replace drop_index with getCleanClient
1 parent 21beb62 commit 1885501

File tree

3 files changed

+18
-74
lines changed

3 files changed

+18
-74
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
build:
99
docker:
1010
- image: circleci/python:2.7.15
11-
- image: redislabs/redisearch:latest
11+
- image: redislabs/redisearch:edge
1212

1313
working_directory: ~/repo
1414

redisearch/client.py

+1-21
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ class Client(object):
109109
ALTER_CMD = 'FT.ALTER'
110110
SEARCH_CMD = 'FT.SEARCH'
111111
ADD_CMD = 'FT.ADD'
112-
ADDHASH_CMD = "FT.ADDHASH"
113112
DROP_CMD = 'FT.DROP'
114113
EXPLAIN_CMD = 'FT.EXPLAIN'
115114
DEL_CMD = 'FT.DEL'
@@ -210,7 +209,7 @@ def create_index(self, fields, no_term_offsets=False,
210209
- **stopwords**: If not None, we create the index with this custom stopword list. The list can be empty
211210
"""
212211

213-
args = [self.CREATE_CMD, self.index_name]
212+
args = [self.CREATE_CMD, self.index_name, 'ON', 'HASH']
214213
if no_term_offsets:
215214
args.append(self.NOOFFSETS)
216215
if no_field_flags:
@@ -276,25 +275,6 @@ def _add_document(self, doc_id, conn=None, nosave=False, score=1.0, payload=None
276275
args += list(itertools.chain(*fields.items()))
277276
return conn.execute_command(*args)
278277

279-
def _add_document_hash(
280-
self, doc_id, conn=None, score=1.0, language=None, replace=False,
281-
):
282-
"""
283-
Internal add_document_hash used for both batch and single doc indexing
284-
"""
285-
if conn is None:
286-
conn = self.redis
287-
288-
args = [self.ADDHASH_CMD, self.index_name, doc_id, score]
289-
290-
if replace:
291-
args.append("REPLACE")
292-
293-
if language:
294-
args += ["LANGUAGE", language]
295-
296-
return conn.execute_command(*args)
297-
298278
def add_document(self, doc_id, nosave=False, score=1.0, payload=None,
299279
replace=False, partial=False, language=None, no_create=False, **fields):
300280
"""

test/test.py

+16-52
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class RedisSearchTestCase(ModuleTestCase('../module.so')):
2323
def createIndex(self, client, num_docs = 100):
2424

2525
assert isinstance(client, Client)
26-
#conn.flushdb()
27-
#client = Client('test', port=conn.port)
2826
try:
2927
client.create_index((TextField('play', weight=5.0),
3028
TextField('txt'),
@@ -172,36 +170,9 @@ def getCleanClient(self, name):
172170
except:
173171
pass
174172

173+
client.redis.flushdb()
175174
return client
176175

177-
def testAddHash(self):
178-
conn = self.redis()
179-
180-
with conn as r:
181-
# Creating a client with a given index name
182-
client = Client('idx', port=conn.port)
183-
184-
client.redis.flushdb()
185-
# Creating the index definition and schema
186-
client.create_index((TextField('title',
187-
weight=5.0), TextField('body')))
188-
189-
client.redis.hset(
190-
'doc1',
191-
mapping={
192-
'title': 'RediSearch',
193-
'body': 'Redisearch impements a search engine on top of redis'
194-
})
195-
# Indexing the hash
196-
client.add_document_hash('doc1')
197-
198-
# Searching with complext parameters:
199-
q = Query("search engine").verbatim().no_content().paging(0, 5)
200-
201-
res = client.search(q)
202-
203-
self.assertEqual('doc1', res.docs[0].id)
204-
205176
def testPayloads(self):
206177

207178
conn = self.redis()
@@ -276,24 +247,19 @@ def testReplace(self):
276247

277248

278249
def testStopwords(self):
279-
conn = self.redis()
280250

281-
with conn as r:
282-
# Creating a client with a given index name
283-
client = Client('idx', port=conn.port)
284-
try:
285-
client.drop_index()
286-
except:
287-
pass
288-
client.create_index((TextField('txt'),), stopwords = ['foo', 'bar', 'baz'])
289-
client.add_document('doc1', txt = 'foo bar')
290-
client.add_document('doc2', txt = 'hello world')
291-
292-
q1 = Query("foo bar").no_content()
293-
q2 = Query("foo bar hello world").no_content()
294-
res1, res2 = client.search(q1), client.search(q2)
295-
self.assertEqual(0, res1.total)
296-
self.assertEqual(1, res2.total)
251+
# Creating a client with a given index name
252+
client = self.getCleanClient('idx')
253+
254+
client.create_index((TextField('txt'),), stopwords = ['foo', 'bar', 'baz'])
255+
client.add_document('doc1', txt = 'foo bar')
256+
client.add_document('doc2', txt = 'hello world')
257+
258+
q1 = Query("foo bar").no_content()
259+
q2 = Query("foo bar hello world").no_content()
260+
res1, res2 = client.search(q1), client.search(q2)
261+
self.assertEqual(0, res1.total)
262+
self.assertEqual(1, res2.total)
297263

298264
def testFilters(self):
299265

@@ -458,11 +424,9 @@ def testAutoComplete(self):
458424
self.assertTrue(sug.payload.startswith('pl'))
459425

460426
def testNoIndex(self):
461-
client = Client('idx', port=self.server.port)
462-
try:
463-
client.drop_index()
464-
except:
465-
pass
427+
428+
# Creating a client with a given index name
429+
client = self.getCleanClient('idx')
466430

467431
client.create_index(
468432
(TextField('f1', no_index=True, sortable=True), TextField('f2')))

0 commit comments

Comments
 (0)