|
| 1 | +# EXAMPLE: geoindex |
| 2 | +import redis |
| 3 | +from redis.commands.json.path import Path |
| 4 | +from redis.commands.search.field import TextField, GeoField, GeoShapeField |
| 5 | +from redis.commands.search.indexDefinition import IndexDefinition, IndexType |
| 6 | +from redis.commands.search.query import Query |
| 7 | + |
| 8 | +r = redis.Redis() |
| 9 | +# REMOVE_START |
| 10 | +try: |
| 11 | + r.ft("productidx").dropindex(True) |
| 12 | +except redis.exceptions.ResponseError: |
| 13 | + pass |
| 14 | + |
| 15 | +try: |
| 16 | + r.ft("geomidx").dropindex(True) |
| 17 | +except redis.exceptions.ResponseError: |
| 18 | + pass |
| 19 | + |
| 20 | +r.delete("product:46885", "product:46886", "shape:1", "shape:2", "shape:3", "shape:4") |
| 21 | +# REMOVE_END |
| 22 | + |
| 23 | +# STEP_START create_geo_idx |
| 24 | +geo_schema = ( |
| 25 | + GeoField("$.location", as_name="location") |
| 26 | +) |
| 27 | + |
| 28 | +geo_index_create_result = r.ft("productidx").create_index( |
| 29 | + geo_schema, |
| 30 | + definition=IndexDefinition( |
| 31 | + prefix=["product:"], index_type=IndexType.JSON |
| 32 | + ) |
| 33 | +) |
| 34 | +print(geo_index_create_result) # >>> True |
| 35 | +# STEP_END |
| 36 | +# REMOVE_START |
| 37 | +assert geo_index_create_result |
| 38 | +# REMOVE_END |
| 39 | + |
| 40 | +# STEP_START add_geo_json |
| 41 | +prd46885 = { |
| 42 | + "description": "Navy Blue Slippers", |
| 43 | + "price": 45.99, |
| 44 | + "city": "Denver", |
| 45 | + "location": "-104.991531, 39.742043" |
| 46 | +} |
| 47 | + |
| 48 | +json_add_result_1 = r.json().set("product:46885", Path.root_path(), prd46885) |
| 49 | +print(json_add_result_1) # >>> True |
| 50 | + |
| 51 | +prd46886 = { |
| 52 | + "description": "Bright Green Socks", |
| 53 | + "price": 25.50, |
| 54 | + "city": "Fort Collins", |
| 55 | + "location": "-105.0618814,40.5150098" |
| 56 | +} |
| 57 | + |
| 58 | +json_add_result_2 = r.json().set("product:46886", Path.root_path(), prd46886) |
| 59 | +print(json_add_result_2) # >>> True |
| 60 | +# STEP_END |
| 61 | +# REMOVE_START |
| 62 | +assert json_add_result_1 |
| 63 | +assert json_add_result_2 |
| 64 | +# REMOVE_END |
| 65 | + |
| 66 | +# STEP_START geo_query |
| 67 | +geo_result = r.ft("productidx").search( |
| 68 | + "@location:[-104.800644 38.846127 100 mi]" |
| 69 | +) |
| 70 | +print(geo_result) |
| 71 | +# >>> Result{1 total, docs: [Document {'id': 'product:46885'... |
| 72 | +# STEP_END |
| 73 | +# REMOVE_START |
| 74 | +assert len(geo_result.docs) == 1 |
| 75 | +assert geo_result.docs[0]["id"] == "product:46885" |
| 76 | +# REMOVE_END |
| 77 | + |
| 78 | +# STEP_START create_gshape_idx |
| 79 | +geom_schema = ( |
| 80 | + TextField("$.name", as_name="name"), |
| 81 | + GeoShapeField( |
| 82 | + "$.geom", as_name="geom", coord_system=GeoShapeField.FLAT |
| 83 | + ) |
| 84 | +) |
| 85 | + |
| 86 | +geom_index_create_result = r.ft("geomidx").create_index( |
| 87 | + geom_schema, |
| 88 | + definition=IndexDefinition( |
| 89 | + prefix=["shape:"], index_type=IndexType.JSON |
| 90 | + ) |
| 91 | +) |
| 92 | +print(geom_index_create_result) # True |
| 93 | +# STEP_END |
| 94 | +# REMOVE_START |
| 95 | +assert geom_index_create_result |
| 96 | +# REMOVE_END |
| 97 | + |
| 98 | +# STEP_START add_gshape_json |
| 99 | +shape1 = { |
| 100 | + "name": "Green Square", |
| 101 | + "geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))" |
| 102 | +} |
| 103 | + |
| 104 | +gm_json_res_1 = r.json().set("shape:1", Path.root_path(), shape1) |
| 105 | +print(gm_json_res_1) # >>> True |
| 106 | + |
| 107 | +shape2 = { |
| 108 | + "name": "Red Rectangle", |
| 109 | + "geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))" |
| 110 | +} |
| 111 | + |
| 112 | +gm_json_res_2 = r.json().set("shape:2", Path.root_path(), shape2) |
| 113 | +print(gm_json_res_2) # >>> True |
| 114 | + |
| 115 | +shape3 = { |
| 116 | + "name": "Blue Triangle", |
| 117 | + "geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))" |
| 118 | +} |
| 119 | + |
| 120 | +gm_json_res_3 = r.json().set("shape:3", Path.root_path(), shape3) |
| 121 | +print(gm_json_res_3) # >>> True |
| 122 | + |
| 123 | +shape4 = { |
| 124 | + "name": "Purple Point", |
| 125 | + "geom": "POINT (2 2)" |
| 126 | +} |
| 127 | + |
| 128 | +gm_json_res_4 = r.json().set("shape:4", Path.root_path(), shape4) |
| 129 | +print(gm_json_res_4) # >>> True |
| 130 | +# STEP_END |
| 131 | +# REMOVE_START |
| 132 | +assert gm_json_res_1 |
| 133 | +assert gm_json_res_2 |
| 134 | +assert gm_json_res_3 |
| 135 | +assert gm_json_res_4 |
| 136 | +# REMOVE_END |
| 137 | + |
| 138 | +# STEP_START gshape_query |
| 139 | +geom_result = r.ft("geomidx").search( |
| 140 | + Query( |
| 141 | + "(-@name:(Green Square) @geom:[WITHIN $qshape])" |
| 142 | + ).dialect(4).paging(0, 1), |
| 143 | + query_params={ |
| 144 | + "qshape": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))" |
| 145 | + } |
| 146 | +) |
| 147 | +print(geom_result) |
| 148 | +# >>> Result{1 total, docs: [Document {'id': 'shape:4'... |
| 149 | +# STEP_END |
| 150 | +# REMOVE_START |
| 151 | +assert len(geom_result.docs) == 1 |
| 152 | +assert geom_result.docs[0]["id"] == "shape:4" |
| 153 | +# REMOVE_END |
0 commit comments