StackExchange / NRediSearch

Other
31 stars 9 forks source link

RediSearch FT.CREATE SCHEMA: A sortable text field with WEIGHT causes as an error #7

Open abremora opened 4 years ago

abremora commented 4 years ago

Note: Analysis after code review. Currently no UnitTest available.

Setup:

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

See Field class here and the TextField class here.

abremora commented 4 years ago

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