@@ -1221,3 +1221,102 @@ def test_syndump(client):
1221
1221
"baby" : ["id2" ],
1222
1222
"offspring" : ["id1" ],
1223
1223
}
1224
+
1225
+
1226
+ @pytest .mark .redismod
1227
+ @skip_ifmodversion_lt ("2.2.0" , "search" )
1228
+ def test_create_json_with_alias (client ):
1229
+ """
1230
+ Create definition with IndexType.JSON as index type (ON JSON) with two
1231
+ fields with aliases, and use json client to test it.
1232
+ """
1233
+ definition = IndexDefinition (prefix = ["king:" ], index_type = IndexType .JSON )
1234
+ client .ft ().create_index (
1235
+ (TextField ("$.name" , as_name = "name" ),
1236
+ NumericField ("$.num" , as_name = "num" )),
1237
+ definition = definition
1238
+ )
1239
+
1240
+ client .json ().set ("king:1" , Path .rootPath (), {"name" : "henry" ,
1241
+ "num" : 42 })
1242
+ client .json ().set ("king:2" , Path .rootPath (), {"name" : "james" ,
1243
+ "num" : 3.14 })
1244
+
1245
+ res = client .ft ().search ("@name:henry" )
1246
+ assert res .docs [0 ].id == "king:1"
1247
+ assert res .docs [0 ].json == '{"name":"henry","num":42}'
1248
+ assert res .total == 1
1249
+
1250
+ res = client .ft ().search ("@num:[0 10]" )
1251
+ assert res .docs [0 ].id == "king:2"
1252
+ assert res .docs [0 ].json == '{"name":"james","num":3.14}'
1253
+ assert res .total == 1
1254
+
1255
+ # Tests returns an error if path contain special characters (user should
1256
+ # use an alias)
1257
+ with pytest .raises (Exception ):
1258
+ client .ft ().search ("@$.name:henry" )
1259
+
1260
+
1261
+ @pytest .mark .redismod
1262
+ @skip_ifmodversion_lt ("2.2.0" , "search" )
1263
+ def test_json_with_multipath (client ):
1264
+ """
1265
+ Create definition with IndexType.JSON as index type (ON JSON),
1266
+ and use json client to test it.
1267
+ """
1268
+ definition = IndexDefinition (prefix = ["king:" ], index_type = IndexType .JSON )
1269
+ client .ft ().create_index (
1270
+ (TagField ("$..name" , as_name = "name" )),
1271
+ definition = definition
1272
+ )
1273
+
1274
+ client .json ().set ("king:1" , Path .rootPath (),
1275
+ {"name" : "henry" , "country" : {"name" : "england" }})
1276
+
1277
+ res = client .ft ().search ("@name:{henry}" )
1278
+ assert res .docs [0 ].id == "king:1"
1279
+ assert res .docs [0 ].json == '{"name":"henry","country":{"name":"england"}}'
1280
+ assert res .total == 1
1281
+
1282
+ res = client .ft ().search ("@name:{england}" )
1283
+ assert res .docs [0 ].id == "king:1"
1284
+ assert res .docs [0 ].json == '{"name":"henry","country":{"name":"england"}}'
1285
+ assert res .total == 1
1286
+
1287
+
1288
+ @pytest .mark .redismod
1289
+ @skip_ifmodversion_lt ("2.2.0" , "search" )
1290
+ def test_json_with_jsonpath (client ):
1291
+ definition = IndexDefinition (index_type = IndexType .JSON )
1292
+ client .ft ().create_index (
1293
+ (TextField ('$["prod:name"]' , as_name = "name" ),
1294
+ TextField ('$.prod:name' , as_name = "name_unsupported" )),
1295
+ definition = definition
1296
+ )
1297
+
1298
+ client .json ().set ("doc:1" , Path .rootPath (), {"prod:name" : "RediSearch" })
1299
+
1300
+ # query for a supported field succeeds
1301
+ res = client .ft ().search (Query ("@name:RediSearch" ))
1302
+ assert res .total == 1
1303
+ assert res .docs [0 ].id == "doc:1"
1304
+ assert res .docs [0 ].json == '{"prod:name":"RediSearch"}'
1305
+
1306
+ # query for an unsupported field fails
1307
+ res = client .ft ().search ("@name_unsupported:RediSearch" )
1308
+ assert res .total == 0
1309
+
1310
+ # return of a supported field succeeds
1311
+ res = client .ft ().search (Query ("@name:RediSearch" ).return_field ("name" ))
1312
+ assert res .total == 1
1313
+ assert res .docs [0 ].id == "doc:1"
1314
+ assert res .docs [0 ].name == 'RediSearch'
1315
+
1316
+ # return of an unsupported field fails
1317
+ res = client .ft ().search (Query ("@name:RediSearch" )
1318
+ .return_field ("name_unsupported" ))
1319
+ assert res .total == 1
1320
+ assert res .docs [0 ].id == "doc:1"
1321
+ with pytest .raises (Exception ):
1322
+ res .docs [0 ].name_unsupported
0 commit comments