I am trying to understand why index_test
works but index_test_wildcard
does not work. The index created in the latter does not match to the records created within the function despite respecting the wildcard syntax. Any help would be appreciated. Thanks.
from redis.commands.json.path import Path
from redis.commands.search.field import TagField, TextField, NumericField
def index_test(r: Redis):
r.json().set('doc:data:1', Path.root_path(), {
"title": "Example Title",
"views": 150,
"tags": ["tutorial", "redis"]
})
# Define the schema and index definition
schema = [
TextField('$.title', as_name='title', weight=5.0),
NumericField('$.views', as_name='views'),
TagField('$.tags', as_name='tags', separator=',')
]
definition = IndexDefinition(prefix=['doc:data:'], index_type=IndexType.JSON)
# Create the index
r.ft("index_test").create_index(schema, definition=definition)
def index_test_wildcard(r: Redis):
r.json().set('doc:1:data:1', Path.root_path(), {
"title": "Example Title",
"views": 150,
"tags": ["tutorial", "redis"]
})
# Define the schema and index definition
schema = [
TextField('$.title', as_name='title', weight=5.0),
NumericField('$.views', as_name='views'),
TagField('$.tags', as_name='tags', separator=',')
]
definition = IndexDefinition(prefix=['doc:*:data:'], index_type=IndexType.JSON)
# Create the index
r.ft("index_test_wildcard").create_index(schema, definition=definition)
redis_client = initialise_redis()
index_test(redis_client)
index_test_wildcard(redis_client)
Here are the results:
127.0.0.1:6379> FT.SEARCH index_test *
1) (integer) 1
2) "doc:data:1"
3) 1) "$"
2) "{\"title\":\"Example Title\",\"views\":150,\"tags\":[\"tutorial\",\"redis\"]}"
127.0.0.1:6379> FT.SEARCH index_test_wildcard *
1) (integer) 0
Prefixes can't be wildcarded, they are, well, prefixes and not a glob-style wildcard pattern or anything like that.
So, in your example, what this is doing is looking for JSON documents that start with doc:*:data:
. It's actually looking for the asterisk as a character in the string. And since the document you added starts with doc:1:data:1
, that won't match.
The same would apply if you put an asterisk at the end of the prefix. The asterisk would be part of the prefix and wouldn't match.