英文:
how to decode Redis ft search query?
问题
以下是代码的翻译部分:
我是新手学习 Redis,尽力学习一切可能的知识,以后想将其用于我的网站缓存。但是,我目前遇到了一个小问题,我不知道如何解码来自 ft 函数的返回查询。
以下是代码:
import redis
import json
from redis.commands.json.path import Path
from redis.commands.search.query import Query, NumericFilter
r = redis.Redis(host="192.168.196.75", port=6379)
user1 = {
"name": "Paul John",
"email": "paul.john@example.com",
"age": 42,
"city": "London",
"id": 1,
"username": "Paul Walker",
}
loc = r.ft("Idx").search(query="@username:Paul")
print(loc)
输出结果是 Result{1 total, docs: [Document {'id': 'user:1', 'payload': None, 'json': '{"name":"Paul John","email":"paul.john@example.com","age":42,"city":"London","id":1,"username":"Paul Walker"}'}]}
正如您所看到的,一切都运行正常,但我无法提取包含信息的字典,我可以尝试进行多个步骤,比如拆分和切片,但我认为有一个函数可以做到这一点,但我找不到它。如果有人知道是什么函数,那将非常有帮助。感谢阅读我的问题。
英文:
So I am new to redis and I am trying to learn everything I possible can and later want to add this for caching in my website.
But, I am currently encountering a very small problem, I have no idea of any function how to decode the return query from the ft function.
The code is below:
import redis
import json
from redis.commands.json.path import Path
from redis.commands.search.query import Query,NumericFilter
r=redis.Redis(host="192.168.196.75",port=6379)
user1 = {
"name": "Paul John",
"email": "paul.john@example.com",
"age": 42,
"city": "London",
"id":1,
"username":"Paul Walker",
}
# r.json().set("user:1",Path.root_path(),user1)
loc=r.ft("Idx").search(query="@username:Paul")
print(loc)
The output I am getting is Result{1 total, docs: [Document {'id': 'user:1', 'payload': None, 'json': '{"name":"Paul John","email":"paul.john@example.com","age":42,"city":"London","id":1,"username":"Paul Walker"}'}]}
As you can see everything is working fine, But I am not able to derive the dictioniary that has the information, I can try to make many steps like splitting and slicing but I think there is a function for this which I am not able to find.
It would be very helpful If anyone knows what that is. Thank you for reading my question
答案1
得分: 1
只获取文档,您可以使用.docs
,例如:
loc=r.ft("Idx").search(query="@username:Paul").docs
以检索所有结果,或者使用.docs[0]
检索第一个元素。
英文:
for retrieving just the documents you can use the .docs
such as:
loc=r.ft("Idx").search(query="@username:Paul").docs
retrieving all results or .docs[0]
for the first element and on
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论