From 63ec8efc421c5b94800623be5474dd687345ed34 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Thu, 17 Apr 2025 16:25:40 +0100 Subject: [PATCH 1/2] DOC-5107 added hash examples for index/query intro page --- doctests/home_json.py | 55 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/doctests/home_json.py b/doctests/home_json.py index 922c83d2fe..794f844034 100644 --- a/doctests/home_json.py +++ b/doctests/home_json.py @@ -10,7 +10,7 @@ import redis.commands.search.aggregation as aggregations import redis.commands.search.reducers as reducers from redis.commands.search.field import TextField, NumericField, TagField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import Query import redis.exceptions # STEP_END @@ -25,7 +25,12 @@ except redis.exceptions.ResponseError: pass -r.delete("user:1", "user:2", "user:3") +try: + r.ft("hash-idx:users").dropindex(True) +except redis.exceptions.ResponseError: + pass + +r.delete("user:1", "user:2", "user:3", "huser:1", "huser:2", "huser:3") # REMOVE_END # STEP_START create_data user1 = { @@ -134,4 +139,50 @@ ) # REMOVE_END +# STEP_START make_hash_index +hashSchema = ( + TextField("name"), + TagField("city"), + NumericField("age") +) + +hashIndexCreated = r.ft("hash-idx:users").create_index( + hashSchema, + definition=IndexDefinition( + prefix=["huser:"], index_type=IndexType.HASH + ) +) +# STEP_END +# REMOVE_START +assert hashIndexCreated +# REMOVE_END + +# STEP_START add_hash_data +huser1Set = r.hset("huser:1", mapping=user1) +huser2Set = r.hset("huser:2", mapping=user2) +huser3Set = r.hset("huser:3", mapping=user3) +# STEP_END +# REMOVE_START +assert huser1Set +assert huser2Set +assert huser3Set +# REMOVE_END + +# STEP_START query1_hash +findPaulHashResult = r.ft("hash-idx:users").search( + Query("Paul @age:[30 40]") +) + +print(findPaulHashResult) +# >>> Result{1 total, docs: [Document {'id': 'huser:3', +# >>> 'payload': None, 'name': 'Paul Zamir', ... +# STEP_END +# REMOVE_START +assert str(findPaulHashResult) == ( + "Result{1 total, docs: [Document " + + "{'id': 'huser:3', 'payload': None, 'name': 'Paul Zamir', " + + "'email': 'paul.zamir@example.com', 'age': '35', 'city': 'Tel Aviv'}]}" +) +# REMOVE_END + r.close() From 4ed09cb503201ca96691f2cf8a50aeea2436851a Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 22 Apr 2025 13:15:57 +0100 Subject: [PATCH 2/2] DOC-5107 restored old index_definition import --- doctests/query_agg.py | 2 +- doctests/query_combined.py | 2 +- doctests/query_em.py | 2 +- doctests/query_ft.py | 2 +- doctests/query_geo.py | 2 +- doctests/query_range.py | 2 +- doctests/search_quickstart.py | 2 +- doctests/search_vss.py | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doctests/query_agg.py b/doctests/query_agg.py index 4fa8f14b84..4d81ddbcda 100644 --- a/doctests/query_agg.py +++ b/doctests/query_agg.py @@ -6,7 +6,7 @@ from redis.commands.search import Search from redis.commands.search.aggregation import AggregateRequest from redis.commands.search.field import NumericField, TagField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType import redis.commands.search.reducers as reducers r = redis.Redis(decode_responses=True) diff --git a/doctests/query_combined.py b/doctests/query_combined.py index a17f19417c..e6dd5a2cb5 100644 --- a/doctests/query_combined.py +++ b/doctests/query_combined.py @@ -6,7 +6,7 @@ import warnings from redis.commands.json.path import Path from redis.commands.search.field import NumericField, TagField, TextField, VectorField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import Query from sentence_transformers import SentenceTransformer diff --git a/doctests/query_em.py b/doctests/query_em.py index a00ff11150..91cc5ae940 100644 --- a/doctests/query_em.py +++ b/doctests/query_em.py @@ -4,7 +4,7 @@ import redis from redis.commands.json.path import Path from redis.commands.search.field import TextField, NumericField, TagField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import NumericFilter, Query r = redis.Redis(decode_responses=True) diff --git a/doctests/query_ft.py b/doctests/query_ft.py index 182a5b2bd3..6272cdab25 100644 --- a/doctests/query_ft.py +++ b/doctests/query_ft.py @@ -5,7 +5,7 @@ import redis from redis.commands.json.path import Path from redis.commands.search.field import TextField, NumericField, TagField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import NumericFilter, Query r = redis.Redis(decode_responses=True) diff --git a/doctests/query_geo.py b/doctests/query_geo.py index dcb7db6ee7..ed8c9a5f99 100644 --- a/doctests/query_geo.py +++ b/doctests/query_geo.py @@ -5,7 +5,7 @@ import redis from redis.commands.json.path import Path from redis.commands.search.field import GeoField, GeoShapeField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import Query r = redis.Redis(decode_responses=True) diff --git a/doctests/query_range.py b/doctests/query_range.py index 4ef957acfb..674afc492a 100644 --- a/doctests/query_range.py +++ b/doctests/query_range.py @@ -5,7 +5,7 @@ import redis from redis.commands.json.path import Path from redis.commands.search.field import TextField, NumericField, TagField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import NumericFilter, Query r = redis.Redis(decode_responses=True) diff --git a/doctests/search_quickstart.py b/doctests/search_quickstart.py index e190393b16..cde4caa84a 100644 --- a/doctests/search_quickstart.py +++ b/doctests/search_quickstart.py @@ -10,7 +10,7 @@ import redis.commands.search.reducers as reducers from redis.commands.json.path import Path from redis.commands.search.field import NumericField, TagField, TextField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import Query # HIDE_END diff --git a/doctests/search_vss.py b/doctests/search_vss.py index 8b4884727a..a1132971db 100644 --- a/doctests/search_vss.py +++ b/doctests/search_vss.py @@ -20,7 +20,7 @@ TextField, VectorField, ) -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.index_definition import IndexDefinition, IndexType from redis.commands.search.query import Query from sentence_transformers import SentenceTransformer