微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

仅将文档与一个索引关联重新搜索

如何解决仅将文档与一个索引关联重新搜索

我在单个 Redissearch 上使用了多个索引,每个索引都与代码中的一个类相关联。 例如,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 下。

解决方法

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

干杯

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。