【问题标题】:Associate a document with one index only (Redisearch)仅将文档与一个索引关联(Redisearch)
【发布时间】:2021-06-12 10:34:14
【问题描述】:

我在单个 Redisearch 上使用多个索引,每个索引与代码中的一个类相关联。 例如,XYZ(List<XYZ>)的数据通过索引XYZ保存,而ABC(List<ABC>)的数据通过ABC保存。

问题是,当我使用索引 XYZ 搜索 XYZ 的数据时,ABC 的数据也会出现在搜索中。 这可以像这样轻松地重新创建:

//declare three indexes. Some of them contain duplicate field names but this is to be expected (in real life we would have things like timestamps that spread across indexes)

ft.create sample1 schema sampletext text 
ft.create sample2 schema sampleinteger numeric sortable
ft.create sample3 schema sampletext text sampleinteger numeric

//then we add a text under sample1
ft.add sample1 sample1doc 1.0 fields sampletext "hello this document is under an index called sample1"



//then display all the documents associated with the index sample3
ft.search "sample3" "*"  
//-> prints the document above "hello this document is under..."

//display all the documents associated with the index sample2
ft.search "sample2" "*"  
//-> the same result

这是为什么?有什么好的解决方法吗?

我知道 FT.ADD 现在已弃用,但 C# 库仍在内部调用 FT.ADD,因此我需要让它与 FT.ADD 一起使用,而且我们刚刚添加的文档仅包含“sampletext”,因此它仍然不应该无论如何都会出现在 sample2 下。

【问题讨论】:

    标签: redis stackexchange.redis redisearch


    【解决方案1】:

    RediSearch 2.0 在后台通过HSET 命令加载哈希索引。即使是 FT.ADD 命令,正如您所说,已被弃用,它会检查输入并将其转换为 HSET 命令。 使用FT.CREATE 创建索引时,您可以为文档指定前缀或使用过滤器。

    如果可能,您应该使用前缀,因为它更高效,并且您的命令看起来像 -

    ft.create sample1 prefix 1 txt: schema sampletext text
    ft.create sample2 prefix 1 num: schema sampleinteger numeric sortable
    hset txt:sample1doc sampletext "hello this document is under an index called sample1"
    

    您将收到 -

    ft.search sample1 *
    1) (integer) 1
    2) "txt:sample1doc"
    3) 1) "sampletext"
       2) "hello this document is under an index called sample1"
    ft.search sample2 *
    1) (integer) 0
    

    干杯

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多