1
+ import json
1
2
import os , sys
3
+
4
+
2
5
sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), '..' ))
3
6
4
7
from rmtest import ModuleTestCase
12
15
import six
13
16
14
17
from redisearch import *
18
+ from redisearch .client import IndexType
15
19
import redisearch .aggregation as aggregations
16
20
import redisearch .reducers as reducers
21
+ import rejson
17
22
18
23
WILL_PLAY_TEXT = os .path .abspath (os .path .dirname (__file__ )) + '/will_play_text.csv.bz2'
19
24
@@ -487,6 +492,7 @@ def testAutoComplete(self):
487
492
def testNoIndex (self ):
488
493
# Creating a client with a given index name
489
494
client = self .getCleanClient ('idx' )
495
+ client .redis .flushdb ()
490
496
491
497
client .create_index (
492
498
(TextField ('field' ),
@@ -946,7 +952,10 @@ def testAggregations(self):
946
952
self .assertEqual ('RediSearch' , res [23 ])
947
953
self .assertEqual (2 , len (res [25 ]))
948
954
949
- def testIndexDefiniontion (self ):
955
+ def testIndexDefinition (self ):
956
+ """
957
+ Create definition and test its args
958
+ """
950
959
conn = self .redis ()
951
960
952
961
with conn as r :
@@ -955,19 +964,24 @@ def testIndexDefiniontion(self):
955
964
return
956
965
client = Client ('test' , port = conn .port )
957
966
967
+ self .assertRaises (RuntimeError , IndexDefinition , prefix = ['hset:' , 'henry' ], index_type = 'json' )
968
+
958
969
definition = IndexDefinition (prefix = ['hset:' , 'henry' ],
959
970
filter = '@f1==32' , language = 'English' , language_field = 'play' ,
960
- score_field = 'chapter' , score = 0.5 , payload_field = 'txt' )
971
+ score_field = 'chapter' , score = 0.5 , payload_field = 'txt' , index_type = IndexType . JSON )
961
972
962
- self .assertEqual (['ON' ,'HASH ' , 'PREFIX' ,2 , 'hset:' ,'henry' ,
963
- 'FILTER' ,'@f1==32' ,'LANGUAGE_FIELD' ,'play' ,'LANGUAGE' ,'English' ,
964
- 'SCORE_FIELD' ,'chapter' ,'SCORE' ,0.5 ,'PAYLOAD_FIELD' ,'txt' ],
973
+ self .assertEqual (['ON' , 'JSON ' , 'PREFIX' , 2 , 'hset:' , 'henry' ,
974
+ 'FILTER' , '@f1==32' , 'LANGUAGE_FIELD' , 'play' , 'LANGUAGE' , 'English' ,
975
+ 'SCORE_FIELD' , 'chapter' , 'SCORE' , 0.5 , 'PAYLOAD_FIELD' , 'txt' ],
965
976
definition .args )
966
977
967
978
self .createIndex (client , num_docs = 500 , definition = definition )
968
979
969
-
970
- def testCreateClientDefiniontion (self ):
980
+ def testCreateClientDefinition (self ):
981
+ """
982
+ Create definition with no index type provided,
983
+ and use hset to test the client definition (the default is HASH).
984
+ """
971
985
conn = self .redis ()
972
986
973
987
with conn as r :
@@ -987,5 +1001,57 @@ def testCreateClientDefiniontion(self):
987
1001
info = client .info ()
988
1002
self .assertEqual (495 , int (info ['num_docs' ]))
989
1003
1004
+ def testCreateClientDefinitionHash (self ):
1005
+ """
1006
+ Create definition with IndexType.HASH as index type (ON HASH),
1007
+ and use hset to test the client definition.
1008
+ """
1009
+ conn = self .redis ()
1010
+
1011
+ with conn as r :
1012
+ r .flushdb ()
1013
+ if not check_version (r , 20000 ):
1014
+ return
1015
+ client = Client ('test' , port = conn .port )
1016
+
1017
+ definition = IndexDefinition (prefix = ['hset:' , 'henry' ], index_type = IndexType .HASH )
1018
+ self .createIndex (client , num_docs = 500 , definition = definition )
1019
+
1020
+ info = client .info ()
1021
+ self .assertEqual (494 , int (info ['num_docs' ]))
1022
+
1023
+ r .hset ('hset:1' , 'f1' , 'v1' );
1024
+
1025
+ info = client .info ()
1026
+ self .assertEqual (495 , int (info ['num_docs' ]))
1027
+
1028
+ def testCreateClientDefinitionJson (self ):
1029
+ """
1030
+ Create definition with IndexType.JSON as index type (ON JSON),
1031
+ and use json client to test it.
1032
+ """
1033
+ conn = self .redis ()
1034
+
1035
+ with conn as r :
1036
+ r .flushdb ()
1037
+ if not check_version (r , 20200 ):
1038
+ return
1039
+
1040
+ client = Client ('json1' , port = conn .port )
1041
+
1042
+ definition = IndexDefinition (prefix = ['king:' ], index_type = IndexType .JSON )
1043
+ client .create_index ((TextField ('$.name' ),), definition = definition )
1044
+
1045
+ rj = rejson .Client (host = 'localhost' , port = conn .port , decode_responses = True )
1046
+ rj .jsonset ('king:1' , rejson .Path .rootPath (), {'name' : 'henry' })
1047
+ rj .jsonset ('king:2' , rejson .Path .rootPath (), {'name' : 'james' })
1048
+
1049
+ res = client .search ('henry' )
1050
+ self .assertEqual (res .docs [0 ].id , 'king:1' )
1051
+ self .assertIsNone (res .docs [0 ].payload )
1052
+ self .assertEqual (res .docs [0 ].json , '{"name":"henry"}' )
1053
+ self .assertEqual (res .total , 1 )
1054
+
1055
+
990
1056
if __name__ == '__main__' :
991
1057
unittest .main ()
0 commit comments