@@ -53,8 +53,8 @@ def createIndex(self, client, num_docs = 100, definition=None):
53
53
54
54
assert isinstance (client , Client )
55
55
try :
56
- client .create_index ((TextField ('play' , weight = 5.0 ),
57
- TextField ('txt' ),
56
+ client .create_index ((TextField ('play' , weight = 5.0 ),
57
+ TextField ('txt' ),
58
58
NumericField ('chapter' )), definition = definition )
59
59
except redis .ResponseError :
60
60
client .dropindex (delete_documents = True )
@@ -161,7 +161,7 @@ def testClient(self):
161
161
self .assertEqual (len (subset ), docs .total )
162
162
ids = [x .id for x in docs .docs ]
163
163
self .assertEqual (set (ids ), set (subset ))
164
-
164
+
165
165
# self.assertRaises(redis.ResponseError, client.search, Query('henry king').return_fields('play', 'nonexist'))
166
166
167
167
# test slop and in order
@@ -272,7 +272,7 @@ def testScores(self):
272
272
#self.assertEqual(0.2, res.docs[1].score)
273
273
274
274
def testReplace (self ):
275
-
275
+
276
276
conn = self .redis ()
277
277
278
278
with conn as r :
@@ -296,7 +296,7 @@ def testReplace(self):
296
296
self .assertEqual (1 , res .total )
297
297
self .assertEqual ('doc1' , res .docs [0 ].id )
298
298
299
- def testStopwords (self ):
299
+ def testStopwords (self ):
300
300
# Creating a client with a given index name
301
301
client = self .getCleanClient ('idx' )
302
302
@@ -324,7 +324,7 @@ def testFilters(self):
324
324
325
325
for i in r .retry_with_rdb_reload ():
326
326
waitForIndex (r , 'idx' )
327
- # Test numerical filter
327
+ # Test numerical filter
328
328
q1 = Query ("foo" ).add_filter (NumericFilter ('num' , 0 , 2 )).no_content ()
329
329
q2 = Query ("foo" ).add_filter (NumericFilter ('num' , 2 , NumericFilter .INF , minExclusive = True )).no_content ()
330
330
res1 , res2 = client .search (q1 ), client .search (q2 )
@@ -338,11 +338,11 @@ def testFilters(self):
338
338
q1 = Query ("foo" ).add_filter (GeoFilter ('loc' , - 0.44 , 51.45 , 10 )).no_content ()
339
339
q2 = Query ("foo" ).add_filter (GeoFilter ('loc' , - 0.44 , 51.45 , 100 )).no_content ()
340
340
res1 , res2 = client .search (q1 ), client .search (q2 )
341
-
341
+
342
342
self .assertEqual (1 , res1 .total )
343
343
self .assertEqual (2 , res2 .total )
344
344
self .assertEqual ('doc1' , res1 .docs [0 ].id )
345
-
345
+
346
346
# Sort results, after RDB reload order may change
347
347
list = [res2 .docs [0 ].id , res2 .docs [1 ].id ]
348
348
list .sort ()
@@ -371,7 +371,7 @@ def testSortby(self):
371
371
# Creating a client with a given index name
372
372
client = Client ('idx' , port = conn .port )
373
373
client .redis .flushdb ()
374
-
374
+
375
375
client .create_index ((TextField ('txt' ), NumericField ('num' , sortable = True )))
376
376
client .add_document ('doc1' , txt = 'foo bar' , num = 1 )
377
377
client .add_document ('doc2' , txt = 'foo baz' , num = 2 )
@@ -381,7 +381,7 @@ def testSortby(self):
381
381
q1 = Query ("foo" ).sort_by ('num' , asc = True ).no_content ()
382
382
q2 = Query ("foo" ).sort_by ('num' , asc = False ).no_content ()
383
383
res1 , res2 = client .search (q1 ), client .search (q2 )
384
-
384
+
385
385
self .assertEqual (3 , res1 .total )
386
386
self .assertEqual ('doc1' , res1 .docs [0 ].id )
387
387
self .assertEqual ('doc2' , res1 .docs [1 ].id )
@@ -417,7 +417,7 @@ def testExample(self):
417
417
# Creating a client with a given index name
418
418
client = Client ('myIndex' , port = conn .port )
419
419
client .redis .flushdb ()
420
-
420
+
421
421
# Creating the index definition and schema
422
422
client .create_index ((TextField ('title' , weight = 5.0 ), TextField ('body' )))
423
423
@@ -552,7 +552,7 @@ def testNoCreate(self):
552
552
# values
553
553
res = client .search ('@f3:f3_val @f2:f2_val @f1:f1_val' )
554
554
self .assertEqual (1 , res .total )
555
-
555
+
556
556
with self .assertRaises (redis .ResponseError ) as error :
557
557
client .add_document ('doc3' , f2 = 'f2_val' , f3 = 'f3_val' , no_create = True )
558
558
@@ -578,7 +578,7 @@ def testSummarize(self):
578
578
doc .txt )
579
579
580
580
q = Query ('king henry' ).paging (0 , 1 ).summarize ().highlight ()
581
-
581
+
582
582
doc = sorted (client .search (q ).docs )[0 ]
583
583
self .assertEqual ('<b>Henry</b> ... ' , doc .play )
584
584
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... ' ,
@@ -600,7 +600,7 @@ def testAlias(self):
600
600
601
601
def1 = IndexDefinition (prefix = ['index1:' ],score_field = 'name' )
602
602
def2 = IndexDefinition (prefix = ['index2:' ],score_field = 'name' )
603
-
603
+
604
604
index1 .create_index ((TextField ('name' ),),definition = def1 )
605
605
index2 .create_index ((TextField ('name' ),),definition = def2 )
606
606
@@ -628,7 +628,7 @@ def testAlias(self):
628
628
with self .assertRaises (Exception ) as context :
629
629
alias_client2 .search ('*' ).docs [0 ]
630
630
self .assertEqual ('spaceballs: no such index' , str (context .exception ))
631
-
631
+
632
632
else :
633
633
634
634
# Creating a client with one index
@@ -808,6 +808,36 @@ def testPhoneticMatcher(self):
808
808
self .assertEqual (2 , len (res .docs ))
809
809
self .assertEqual (['John' , 'Jon' ], sorted ([d .name for d in res .docs ]))
810
810
811
+ def testScorer (self ):
812
+ # Creating a client with a given index name
813
+ client = self .getCleanClient ('idx' )
814
+
815
+ client .create_index ((TextField ('description' ),))
816
+
817
+ client .add_document ('doc1' , description = 'The quick brown fox jumps over the lazy dog' )
818
+ client .add_document ('doc2' , description = 'Quick alice was beginning to get very tired of sitting by her quick sister on the bank, and of having nothing to do.' )
819
+
820
+ # default scorer is TFIDF
821
+ res = client .search (Query ('quick' ).with_scores ())
822
+ self .assertEqual (1.0 , res .docs [0 ].score )
823
+ res = client .search (Query ('quick' ).scorer ('TFIDF' ).with_scores ())
824
+ self .assertEqual (1.0 , res .docs [0 ].score )
825
+
826
+ res = client .search (Query ('quick' ).scorer ('TFIDF.DOCNORM' ).with_scores ())
827
+ self .assertEqual (0.1111111111111111 , res .docs [0 ].score )
828
+
829
+ res = client .search (Query ('quick' ).scorer ('BM25' ).with_scores ())
830
+ self .assertEqual (0.17699114465425977 , res .docs [0 ].score )
831
+
832
+ res = client .search (Query ('quick' ).scorer ('DISMAX' ).with_scores ())
833
+ self .assertEqual (2.0 , res .docs [0 ].score )
834
+
835
+ res = client .search (Query ('quick' ).scorer ('DOCSCORE' ).with_scores ())
836
+ self .assertEqual (1.0 , res .docs [0 ].score )
837
+
838
+ res = client .search (Query ('quick' ).scorer ('HAMMING' ).with_scores ())
839
+ self .assertEqual (0.0 , res .docs [0 ].score )
840
+
811
841
def testGet (self ):
812
842
client = self .getCleanClient ('idx' )
813
843
client .create_index ((TextField ('f1' ), TextField ('f2' )))
0 commit comments