如何解码Redis ft搜索查询?

huangapple go评论55阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月31日 02:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定