RediSearch是否能够在索引JSON数组时返回找到的数组项的属性?

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

Can RediSearch return the property of found array item when indexing JSON arrays?

问题

无法直接返回找到的对象属性,但可以返回找到的索引。在此示例中,您可以使用以下命令来返回找到的索引:

FT.SEARCH nameSearch '@userEmail:{boris@some.domain}' RETURN 1 $.people.*.email @INDEX


这将返回索引,您可以根据它获取对象属性。在这种情况下,索引将是0,因为JSON数组中第一个对象的email匹配了搜索条件。然后,您可以使用这个索引来获取相应对象的属性值。

希望这可以帮助您解决问题。
英文:

I have groups of entities saved in a Redis database. Each group is its own JSON-type key (let's say for a simple example it's a list of users, userlist:1, userlist:2), and each value has an array of objects, like so:

"name": "accounting_team",
"people": [
    {
        "name": "Ahmed",
        "email: "ahmed@some.domain"
    },
    {
        "name": "Boris",
        "email": "boris@some.domain"
    }
]

I need to create an index that would search a property of the objects in the array, and return:

  1. the database key (in this example, the userlist:N key name)
  2. a specific property of the found object (let's say in this example, the name associated with the email)

Indexing using FT.CREATE works as expected: FT.CREATE nameSearch ON JSON PREFIX 1 userlist: SCHEMA $.people.*.email AS userEmail TAG indexes all the emails, so searching with FT.SEARCH nameSearch '@userEmail:{ahmed\@some\.domain}' or (boris\@some\.domain}) returns the needed key userlist:1, and its full contents.

However, I cannot find a way to return a property of the found object. In this example, searching FT.SEARCH nameSearch '@userEmail:{boris\@some\.domain}' RETURN 1 $.people.*.email returns Ahmed instead of Boris. I guess this is because the * operator in the JSONPath doesn't specify an index, but rather "any value", so it gives first value it encouters.

Is there a way to RETURN directly a property (in this exmaple, the specific name value of object where the email value was found)? Or at least, is it possible to RETURN the found index in the array?

答案1

得分: 0

JSONPath过滤表达式?()原来能够提供所需的结果。它们可以用于在放置在[]内时遍历所有数组值,@符号用于表示"正在迭代的当前索引"。路径可以在返回语句中指定。

因此,像这样的搜索 FT.SEARCH nameSearch '@userEmail:{boris\@some\.domain}' RETURN 1 "$.people[?(@.userEmail=='boris\@some\.domain')].name" 将返回 "Boris"

需要注意的几点:

  • 路径需要用引号括起来
  • 如果比较的值是字符串,它需要用引号括起来(与路径中的引号不同,所以如果路径用""括起来,邮箱需要用''括起来)
英文:

JSONPath filter expressions ?() turned out to give the needed result. They can be used to iterate through all array values when placed inside [], and the @ symbol is used for "current index that's being iterated". The path can be specified in the return statement.

So a search like FT.SEARCH nameSearch '@userEmail:{boris\@some\.domain}' RETURN 1 "$.people[?(@.userEmail=='boris\@some\.domain')].name" will return "Boris".

Several things to note:

  • The path needs to be in quotes
  • The compared value needs to be in quotes if it's a string (different ones from the path, so if path is enclosed in "", enclose email in '')

huangapple
  • 本文由 发表于 2023年7月20日 18:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729119.html
匿名

发表评论

匿名网友

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

确定