@@ -1052,6 +1052,67 @@ def testCreateClientDefinitionJson(self):
1052
1052
self .assertEqual (res .docs [0 ].json , '{"name":"henry"}' )
1053
1053
self .assertEqual (res .total , 1 )
1054
1054
1055
+ def testFieldsAsName (self ):
1056
+ conn = self .redis ()
1057
+
1058
+ with conn as r :
1059
+ r .flushdb ()
1060
+ if not check_version (r , 20200 ):
1061
+ return
1062
+
1063
+ # create index
1064
+ SCHEMA = (
1065
+ TextField ("$.name" , sortable = True , as_name = 'name' ),
1066
+ NumericField ("$.age" , as_name = 'just_a_number' ),
1067
+ )
1068
+ definition = IndexDefinition (index_type = IndexType .JSON )
1069
+ json_client = Client ('idxJson' )
1070
+ json_client .create_index (SCHEMA , definition = definition )
1071
+
1072
+ # insert json data
1073
+ rj = rejson .Client (host = 'localhost' , port = conn .port , decode_responses = True )
1074
+ res = rj .jsonset ('doc:1' , rejson .Path .rootPath (), {'name' : 'Jon' , 'age' : 25 })
1075
+ self .assertTrue (res )
1076
+
1077
+ total = json_client .search (Query ('Jon' ).return_fields ('name' , 'just_a_number' )).docs
1078
+ self .assertEqual (1 , len (total ))
1079
+ self .assertEqual ('doc:1' , total [0 ].id )
1080
+ self .assertEqual ('Jon' , total [0 ].name )
1081
+ self .assertEqual ('25' , total [0 ].just_a_number )
1082
+
1083
+ def testSearchReturnFields (self ):
1084
+ conn = self .redis ()
1085
+
1086
+ with conn as r :
1087
+ r .flushdb ()
1088
+ if not check_version (r , 20200 ):
1089
+ return
1090
+
1091
+ # insert json data
1092
+ rj = rejson .Client (host = 'localhost' , port = conn .port , decode_responses = True )
1093
+ res = rj .jsonset ('doc:1' , rejson .Path .rootPath (),
1094
+ {"t" : "riceratops" , "t2" : "telmatosaurus" , "n" : 9072 , "flt" : 97.2 })
1095
+ self .assertTrue (res )
1096
+
1097
+ # create index json
1098
+ definition = IndexDefinition (index_type = IndexType .JSON )
1099
+ SCHEMA = (
1100
+ TextField ("$.t" ),
1101
+ NumericField ("$.flt" ),
1102
+ )
1103
+ json_client = Client ('idxJson' )
1104
+ json_client .create_index (SCHEMA , definition = definition )
1105
+
1106
+ total = json_client .search (Query ('*' ).return_field ("$.t" , as_field = "txt" )).docs
1107
+ self .assertEqual (1 , len (total ))
1108
+ self .assertEqual ('doc:1' , total [0 ].id )
1109
+ self .assertEqual ('riceratops' , total [0 ].txt )
1110
+
1111
+ total = json_client .search (Query ('*' ).return_field ("$.t2" , as_field = "txt" )).docs
1112
+ self .assertEqual (1 , len (total ))
1113
+ self .assertEqual ('doc:1' , total [0 ].id )
1114
+ self .assertEqual ('telmatosaurus' , total [0 ].txt )
1115
+
1055
1116
1056
1117
if __name__ == '__main__' :
1057
1118
unittest .main ()
0 commit comments