|  | 
|  | 1 | +# EXAMPLE: query_range | 
|  | 2 | +# HIDE_START | 
|  | 3 | +import json | 
|  | 4 | +import sys | 
|  | 5 | +import redis | 
|  | 6 | +from redis.commands.json.path import Path | 
|  | 7 | +from redis.commands.search.field import TextField, NumericField, TagField | 
|  | 8 | +from redis.commands.search.indexDefinition import IndexDefinition, IndexType | 
|  | 9 | +from redis.commands.search.query import NumericFilter, Query | 
|  | 10 | + | 
|  | 11 | +r = redis.Redis(decode_responses=True) | 
|  | 12 | + | 
|  | 13 | +# create index | 
|  | 14 | +schema = ( | 
|  | 15 | +    TextField("$.description", as_name="description"), | 
|  | 16 | +    NumericField("$.price", as_name="price"), | 
|  | 17 | +    TagField("$.condition", as_name="condition"), | 
|  | 18 | +) | 
|  | 19 | + | 
|  | 20 | +index = r.ft("idx:bicycle") | 
|  | 21 | +index.create_index( | 
|  | 22 | +    schema, | 
|  | 23 | +    definition=IndexDefinition(prefix=["bicycle:"], index_type=IndexType.JSON), | 
|  | 24 | +) | 
|  | 25 | + | 
|  | 26 | +# load data | 
|  | 27 | +with open("data/query_em.json") as f: | 
|  | 28 | +    bicycles = json.load(f) | 
|  | 29 | + | 
|  | 30 | +pipeline = r.pipeline(transaction=False) | 
|  | 31 | +for bid, bicycle in enumerate(bicycles): | 
|  | 32 | +    pipeline.json().set(f'bicycle:{bid}', Path.root_path(), bicycle) | 
|  | 33 | +pipeline.execute() | 
|  | 34 | +# HIDE_END | 
|  | 35 | + | 
|  | 36 | +# STEP_START range1 | 
|  | 37 | +res = index.search(Query("@price:[500 1000]")) | 
|  | 38 | +print(res.total) | 
|  | 39 | +# >>> 3 | 
|  | 40 | +# REMOVE_START | 
|  | 41 | +assert res.total == 3 | 
|  | 42 | +# REMOVE_END | 
|  | 43 | +# STEP_END | 
|  | 44 | + | 
|  | 45 | +# STEP_START range2 | 
|  | 46 | +query = Query("*").add_filter(NumericFilter("price", 500, 1000)) | 
|  | 47 | +res = index.search(query) | 
|  | 48 | +print(res.total) | 
|  | 49 | +# >>> 3 | 
|  | 50 | +# REMOVE_START | 
|  | 51 | +assert res.total == 3 | 
|  | 52 | +# REMOVE_END | 
|  | 53 | +# STEP_END | 
|  | 54 | + | 
|  | 55 | +# STEP_START range3 | 
|  | 56 | +query = Query("*").add_filter(NumericFilter("price", "(1000", "+inf")) | 
|  | 57 | +res = index.search(query) | 
|  | 58 | +print(res.total) | 
|  | 59 | +# >>> 5 | 
|  | 60 | +# REMOVE_START | 
|  | 61 | +assert res.total == 5 | 
|  | 62 | +# REMOVE_END | 
|  | 63 | +# STEP_END | 
|  | 64 | + | 
|  | 65 | +# STEP_START range4 | 
|  | 66 | +query = Query('@price:[-inf 2000]').sort_by('price').paging(0, 5) | 
|  | 67 | +res = index.search(query) | 
|  | 68 | +print(res.total) | 
|  | 69 | +print(res) | 
|  | 70 | +# >>> Result{7 total, docs: [Document {'id': 'bicycle:0', ... }, Document {'id': 'bicycle:7', ... }, Document {'id': 'bicycle:5', ... }, ...] | 
|  | 71 | +# REMOVE_START | 
|  | 72 | +assert res.total == 7 | 
|  | 73 | +# REMOVE_END | 
|  | 74 | +# STEP_END | 
|  | 75 | + | 
|  | 76 | +# REMOVE_START | 
|  | 77 | +# destroy index and data | 
|  | 78 | +r.ft("idx:bicycle").dropindex(delete_documents=True) | 
|  | 79 | +# REMOVE_END | 
0 commit comments