【问题标题】:How to index JSON arrays in RediSearch?如何在 RediSearch 中索引 JSON 数组?
【发布时间】:2022-01-27 06:34:45
【问题描述】:

我同时使用模块 RedisJSON 和 RediSearch 对 JSON 数据执行搜索查询。 对于每个 JSON 对象,我需要索引数组字段中的所有字符串元素,以便能够通过查询数组中的一个字符串来获取该对象(即通过搜索字符串中包含的作者之一来获取一些书籍数据JSON书中的数组)。 但是,目前似乎是not possible。 有什么可能的解决方法还是我被卡住了?

【问题讨论】:

    标签: database redis redisearch redisjson


    【解决方案1】:

    你需要尝试最新版本的RediSearch+RedisJSON。

    github issue you are referring to 中的示例适用于 RedisJSON 2.0.5 和 RediSearch 2.2.5

    127.0.0.1:6379> ft.create index on json schema $.names[0:].first as first tag
    OK
    
    127.0.0.1:6379> json.set pserson:1 $ '{"names":[{"first": "fname1","last": "lname1"},{"first": "fname2","last": "lname2"},{"first": "fname3", "last": "lname3"}]}'
    OK
    
    127.0.0.1:6379> ft.search index @first:{fname1}
    1) (integer) 1
    2) "pserson:1"
    3) 1) "$"
       2) "{\"names\":[{\"first\":\"fname1\",\"last\":\"lname1\"},{\"first\":\"fname2\",\"last\":\"lname2\"},{\"first\":\"fname3\",\"last\":\"lname3\"}]}"
    
    127.0.0.1:6379> ft.search index @first:{fname2}
    1) (integer) 1
    2) "pserson:1"
    3) 1) "$"
       2) "{\"names\":[{\"first\":\"fname1\",\"last\":\"lname1\"},{\"first\":\"fname2\",\"last\":\"lname2\"},{\"first\":\"fname3\",\"last\":\"lname3\"}]}"
    

    这里是具体的redis-cli info modules

    module:name=ReJSON,ver=20005,api=1,filters=0,usedby=[search],using=[],options=[handle-io-errors]
    module:name=search,ver=20205,api=1,filters=0,usedby=[],using=[ReJSON],options=[handle-io-errors]
    

    【讨论】:

    • 正如问题的最后一条评论所述,对于某些用例,无法将数组字段存储为标签。在我的情况下,不可能使用标签,因为我需要一次对所有这些数组元素执行搜索,所以它们应该保留模式中的 TEXT 字段。
    猜你喜欢
    • 2023-01-13
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2021-08-12
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    相关资源
    最近更新 更多