Summary
This post will provide some snippets from Redis Query Workshop available on GitHub. That workshop covers parallel examples in CLI, Python, Nodejs, Java, and C#. This post will focus on Python examples.
Basic JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
client.json().set('ex2:5', '$', {"str1": "val1", "str2": "val2", "arr1":[1,2,3,4], "obj1": {"num1": 1,"arr2":["val1","val2", "val3"]}}) | |
result = client.json().get('ex2:5', Path('$.obj1.arr2')) | |
print(result) | |
result = client.json().get('ex2:5', Path('$.arr1[1]' )) | |
print(result) | |
result = client.json().get('ex2:5', Path('$.obj1.arr2[0:2]')) | |
print(result) | |
result = client.json().get('ex2:5', Path('$.arr1[-2:]')) | |
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[['val1', 'val2', 'val3']] | |
[2] | |
['val1', 'val2'] | |
[3, 4] |
Basic Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
idx_def = IndexDefinition(index_type=IndexType.JSON, prefix=['product:']) | |
schema = [ | |
NumericField('$.id', as_name='id'), | |
TagField('$.gender', as_name='gender'), | |
TagField('$.season.*', as_name='season'), | |
TextField('$.description', as_name='description'), | |
NumericField('$.price', as_name='price'), | |
TextField('$.city', as_name='city'), | |
GeoField('$.coords', as_name='coords') | |
] | |
result = client.ft('idx1').create_index(schema, definition=idx_def) | |
client.json().set('product:15970', '$', {"id": 15970, "gender": "Men", "season":["Fall", "Winter"], "description": "Turtle Check Men Navy Blue Shirt", "price": 34.95, "city": "Boston", "coords": "-71.057083, 42.361145"}) | |
client.json().set('product:59263', '$', {"id": 59263, "gender": "Women", "season":["Fall", "Winter", "Spring", "Summer"],"description": "Titan Women Silver Watch", "price": 129.99, "city": "Dallas", "coords": "-96.808891, 32.779167"}) | |
client.json().set('product:46885', '$', {"id": 46885, "gender": "Boys", "season":["Fall"], "description": "Ben 10 Boys Navy Blue Slippers", "price": 45.99, "city": "Denver", "coords": "-104.991531, 39.742043"}) | |
query = Query('@description:("Blue Shirt")') | |
result = client.ft('idx1').search(query) | |
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Result{1 total, docs: [Document {'id': 'product:15970', 'payload': None, 'json': '{"id":15970,"gender":"Men","season":["Fall","Winter"],"description":"Turtle Check Men Navy Blue Shirt","price":34.95,"city":"Boston","coords":"-71.057083, 42.361145"}'}]} |
Advanced JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
client.json().set('warehouse:1', '$', { | |
"city": "Boston", | |
"location": "42.361145, -71.057083", | |
"inventory":[{ | |
"id": 15970, | |
"gender": "Men", | |
"season":["Fall", "Winter"], | |
"description": "Turtle Check Men Navy Blue Shirt", | |
"price": 34.95 | |
},{ | |
"id": 59263, | |
"gender": "Women", | |
"season": ["Fall", "Winter", "Spring", "Summer"], | |
"description": "Titan Women Silver Watch", | |
"price": 129.99 | |
},{ | |
"id": 46885, | |
"gender": "Boys", | |
"season": ["Fall"], | |
"description": | |
"Ben 10 Boys Navy Blue Slippers", | |
"price": 45.99 | |
}]}) | |
result = client.json().get('warehouse:1', Path('$.inventory[?(@.description=="Turtle Check Men Navy Blue Shirt")]')) | |
print(json.dumps(result, indent=4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": 15970, | |
"gender": "Men", | |
"season": [ | |
"Fall", | |
"Winter" | |
], | |
"description": "Turtle Check Men Navy Blue Shirt", | |
"price": 34.95 | |
} | |
] |
Advanced Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schema = [VectorField('$.vector', 'FLAT', { "TYPE": 'FLOAT32', "DIM": 4, "DISTANCE_METRIC": 'L2'}, as_name='vector')] | |
idx_def: IndexDefinition = IndexDefinition(index_type=IndexType.JSON, prefix=['vec:']) | |
result = client.ft('vss_idx').create_index(schema, definition=idx_def) | |
client.json().set('vec:1', '$', {'vector': [1,1,1,1]}) | |
client.json().set('vec:2', '$', {'vector': [2,2,2,2]}) | |
client.json().set('vec:3', '$', {'vector': [3,3,3,3]}) | |
client.json().set('vec:4', '$', {'vector': [4,4,4,4]}) | |
vec = [2,2,3,3] | |
query_vector = np.array(vec, dtype=np.float32).tobytes() | |
q_str = '*=>[KNN 3 @vector $query_vec]' | |
q = Query(q_str)\ | |
.sort_by('__vector_score')\ | |
.dialect(2) | |
params_dict = {"query_vec": query_vector} | |
results = client.ft('vss_idx').search(q, query_params=params_dict) | |
print(results) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Result{3 total, docs: [Document {'id': 'vec:2', 'payload': None, '__vector_score': '2', 'json': '{"vector":[2,2,2,2]}'}, Document {'id': 'vec:3', 'payload': None, '__vector_score': '2', 'json': '{"vector":[3,3,3,3]}'}, Document {'id': 'vec:1', 'payload': None, '__vector_score': '10', 'json': '{"vector":[1,1,1,1]}'}]} |
Source
Copyright ©1993-2024 Joey E Whelan, All rights reserved.