Redisearch 全文索引不适用于 Python 客户端

Sex*_*ast 6 python redis redis-py redisearch

我正在尝试按照Redis 文档链接创建一个可实时搜索的名人小型数据库(使用 Python 客户端)。

我尝试了类似的代码,但最后一行(按“s”查询)应该返回两个文档,而是返回一个空白集。有人可以帮我找出我所犯的错误吗?

import redis
from redis.commands.json.path import Path
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.query import NumericFilter, Query

d1 = {"key": "shahrukh khan", "pl": '{"d": "mvtv", "id": "1234-a", "img": "foo.jpg", "t: "act", "tme": "1965-"}', "org": "1", "p": 100}
d2 = {"key": "salman khan", "pl": '{"d": "mvtv", "id": "1236-a", "img": "fool.jpg", "t: "act", "tme": "1965-"}', "org": "1", "p": 100}
d3 = {"key": "aamir khan", "pl": '{"d": "mvtv", "id": "1237-a", "img": "fooler.jpg", "t: "act", "tme": "1965-"}', "org": "1", "p": 100}


schema = ( 
    TextField("$.key", as_name="key"),  
    NumericField("$.p", as_name="p"),  
) 

r = redis.Redis(host='localhost', port=6379)
rs = r.ft("idx:au") 
rs.create_index(     
    schema,     
    definition=IndexDefinition(     
        prefix=["au:"], index_type=IndexType.JSON   
    )    
)

r.json().set("au:mvtv-1234-a", Path.root_path(), d1)  
r.json().set("au:mvtv-1236-a", Path.root_path(), d2)  
r.json().set("au:mvtv-1237-a", Path.root_path(), d3)  

rs.search(Query("s"))
Run Code Online (Sandbox Code Playgroud)

L. *_*yer 2

当从redis-py客户端执行查询时,它会将命令传输FT.SEARCH到redis服务器。MONITOR例如,您可以使用 redis-client 中的命令来观察它。

根据文档,当为研究提供单个单词时,匹配是完整的。这就是为什么查询结果是空集的原因。如果要按前缀搜索,则需要使用表达式prefix*

然而,文档说:

前缀的长度至少需要两个字符。

因此,您不能通过仅以 s 开头的单词进行搜索。你可以做什么:

rs.search(Query("sa*"))
#Result{1 total, docs: [Document {'id': 'au:mvtv-1236-a', 'payload': None, 'json': '{"key":"salman khan","pl":"{\\"d\\": \\"mvtv\\", \\"id\\": \\"1236-a\\", \\"img\\": \\"fool.jpg\\", \\"t: \\"act\\", \\"tme\\": \\"1965-\\"}","org":"1","p":100}'}]}
Run Code Online (Sandbox Code Playgroud)

旁注

如果您想将搜索范围限制在特定字段,语法为:

Query("@field_name: word") # Query("@key: sa*")
Run Code Online (Sandbox Code Playgroud)

其中@field_name是架构字段的名称。否则,搜索将查找所有TextField属性。