简体   繁体   中英

Associate a document with one index only (Redisearch)

I use multiple indexes on a single Redisearch, and each index is associated with a class in the code. For example, the data of XYZ ( List<XYZ> ) is saved via an index XYZ, whereas the data of ABC ( List<ABC> )via ABC.

The problem is, when I search for data of XYZ using the index XYZ, data of ABC also shows up in the search. This can be easily recreated like this:

//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

Why is this? Is there any good workaround for this?

I know FT.ADD is deprecated now but the C# library still internally calls FT.ADD so I need to get it working with FT.ADD, plus the document we just added only contains "sampletext" so it still shouldn't appear under sample2 anyway.

RediSearch 2.0 indexes hashes in the background as they are loaded via an HSET command. Even the FT.ADD command, which, as you stated, was deprecated, checks the input and translates it into an HSET command. When creating an index using FT.CREATE , you can specify either a prefix for your document or use a filter.

If possible, you should use a prefix as it is more performant, and your command will look something like -

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"

You will receive -

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

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM