0

我在单个 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 下反正。

4

1 回答 1

2

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

干杯

于 2021-03-15T07:49:06.967 回答