1+ import json
12import os , sys
3+
4+
25sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), '..' ))
36
47from rmtest import ModuleTestCase
1215import six
1316
1417from redisearch import *
18+ from redisearch .client import IndexType
1519import redisearch .aggregation as aggregations
1620import redisearch .reducers as reducers
21+ import rejson
1722
1823WILL_PLAY_TEXT = os .path .abspath (os .path .dirname (__file__ )) + '/will_play_text.csv.bz2'
1924
@@ -487,6 +492,7 @@ def testAutoComplete(self):
487492 def testNoIndex (self ):
488493 # Creating a client with a given index name
489494 client = self .getCleanClient ('idx' )
495+ client .redis .flushdb ()
490496
491497 client .create_index (
492498 (TextField ('field' ),
@@ -946,7 +952,10 @@ def testAggregations(self):
946952 self .assertEqual ('RediSearch' , res [23 ])
947953 self .assertEqual (2 , len (res [25 ]))
948954
949- def testIndexDefiniontion (self ):
955+ def testIndexDefinition (self ):
956+ """
957+ Create definition and test its args
958+ """
950959 conn = self .redis ()
951960
952961 with conn as r :
@@ -955,19 +964,24 @@ def testIndexDefiniontion(self):
955964 return
956965 client = Client ('test' , port = conn .port )
957966
967+ self .assertRaises (RuntimeError , IndexDefinition , prefix = ['hset:' , 'henry' ], index_type = 'json' )
968+
958969 definition = IndexDefinition (prefix = ['hset:' , 'henry' ],
959970 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 )
961972
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' ],
965976 definition .args )
966977
967978 self .createIndex (client , num_docs = 500 , definition = definition )
968979
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+ """
971985 conn = self .redis ()
972986
973987 with conn as r :
@@ -987,5 +1001,57 @@ def testCreateClientDefiniontion(self):
9871001 info = client .info ()
9881002 self .assertEqual (495 , int (info ['num_docs' ]))
9891003
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+
9901056if __name__ == '__main__' :
9911057 unittest .main ()
0 commit comments