Enterprise Redis JSON.GET 用于 JSONARRAY 包含的语法

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

Enterprise Redis JSON.GET Syntax for JSONARRAY contains

问题

如何在 JSON.GET warehouse:1 中查询包含 "Winter" 的 "season" 内容?

英文:

I'am looking what functionalities that Redis Enterprise can offer, and came up with following questions.
Content in JSON using JSON.Set

JSON.SET warehouse:1 $ '{
    "city": "Boston",
    "location": "42.361145, -71.057083",
    "inventory": [
        {   
            "id": 15970,
            "gender": "Men",
            "season":["Fall", "Winter"],
            "description": "Turtle Check Men Navy Blue Shirt",
            "price": 34.95
        },
        {
            "id": 59263,
            "gender": "Women",
            "season": ["Fall", "Winter", "Spring", "Summer"],
            "description": "Titan Women Silver Watch",
            "price": 129.99
        },
        {
            "id": 46885,
            "gender": "Boys",
            "season": ["Fall"],
            "description": "Ben 10 Boys Navy Blue Slippers",
            "price": 45.99
        }
    ]
}'

How to write JSON.GET warehouse:1 for "season" contains "Winter"

答案1

得分: 0

你正在寻找的是JSONPath中的“包含”操作。目前,我认为这是不可能的。这不是Redis Enterprise或Redis Stack的限制,而是JSONPath本身的限制。

我进行了一些搜索,发现一些关于JSONPath提出的用于执行此操作的语法片段。然而,目前还没有实现任何内容。

也许有一种巧妙的方法可以绕过这个问题,但我想不出来。我建议您查看Redis的JSONPath文档,特别关注过滤示例,以及尝试使用在线评估器来尝试JSONPath的各种功能。

英文:

What you are looking for is a "contains" operation for JSONPath. At the moment, I don't believe this is possible. This isn't a limitation with Redis Enterprise or Redis Stack, this is a limitation of JSONPath itself.

I did some googling and found some bits and bobs with proposed syntax for JSONPath to do this. However, nothing implemented yet.

There might be a clever hack to get around this but I couldn't think of one. I would suggest that you check out the JSONPath docs for Redis, looking specifically at the filtering examples, and that you play around with what you can do with JSONPath with an online evaluator.

答案2

得分: 0

  • 如果您知道要查看哪个JSON文档,可以使用JSONPath过滤器,正如Guy Royse建议的那样,例如,要获取具有Winter季节的物品的id:

    JSON.GET warehouse:1 '$.inventory[?(@.season[?(@=="Winter")])].id';
    
  • 如果您不知道要查看哪个JSON文档,可以使用RediSearch,正如Simon Prickett建议的那样,例如:

    FT.CREATE idx:inventory ON JSON SCHEMA '$.inventory[*].season[*]' AS season TAG
    

    然后搜索文档:

    FT.SEARCH idx:inventory @season:{Winter}
    

    或者还可以检索相关的id:

    FT.SEARCH idx:inventory @season:{Winter} RETURN 1 '$.inventory[?(@.season[?(@=="Winter")])].id';
    
英文:
  • If you know which JSON document to look into, JSONPath filters can be used, as suggested by Guy Royse,
    For example, to get the id of items with Winter in season:

    JSON.GET warehouse:1 '$.inventory[?(@.season[?(@=="Winter")])].id'
    
  • If you do not know which JSON document to look into, RediSearch can be used, as suggested by Simon Prickett,
    For example:

    FT.CREATE idx:inventory ON JSON SCHEMA '$.inventory[*].season[*]' AS season TAG
    

    And then search for documents

    FT.SEARCH idx:inventory @season:{Winter}
    

    Or also retrieve the relevant id

    FT.SEARCH idx:inventory @season:{Winter} RETURN 1 '$.inventory[?(@.season[?(@=="Winter")])].id'
    

huangapple
  • 本文由 发表于 2023年6月16日 06:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485978.html
匿名

发表评论

匿名网友

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

确定