NRediSearch
NRediSearch copied to clipboard
RediSearch FT.CREATE SCHEMA: A sortable text field with WEIGHT causes as an error
Note: Analysis after code review. Currently no UnitTest available.
Setup:
- NRediSearch Version=2.1.30
- .NET Core 3.1
The syntax of the command for creating a new index is not correct.
Syntax (see documentation):
FT.CREATE {index}
[MAXTEXTFIELDS] [TEMPORARY {seconds}] [NOOFFSETS] [NOHL] [NOFIELDS] [NOFREQS]
[STOPWORDS {num} {stopword} ...]
SCHEMA {field} [TEXT [NOSTEM] [WEIGHT {weight}] [PHONETIC {matcher}] | NUMERIC | GEO | TAG [SEPARATOR {sep}] ] [SORTABLE][NOINDEX] ...
An example looks like:
FT.CREATE myindex SCHEMA lastname TEXT WEIGHT 5.0 SORTABLE
The code looks like:
var schema = new Schema();
schema.AddSortableTextField("lastname", 5.0);
var options = new Client.ConfiguredIndexOptions();
await _client.CreateIndexAsync(schema, options);
But this code causes an StackExchange.Redis.RedisServerException with the message:
ERROR: Invalid field type for field
WEIGHT
Reason:
TextField.SerializeRedisArgs() runs the base class first. So the arguments for sortables fields are in the wrong order. The output is:
FT.CREATE myindex SCHEMA lastname TEXT SORTABLE WEIGHT 5.0
I'm working on a fix. I already have a failing test. But the current implementation is a little bit weird. I think a refactoring is needed.
See: https://gist.github.com/abremora/283c733c21e952aa898ea26fb7f854a0