Search code examples
redisredisearchredisjson

Redisearch NOT operator negates full query


I'm using Redisearch and RedisJSON and I have this object type

{
    "Id": "string",
    "Locked": "string"
}

I have this index

FT.CREATE idx ON JSON PREFIX 1 "queue:" SCHEMA $.Id AS Id TAG $.Locked AS Locked TAG

I'm trying to find objects where Id is NOT equal to hello-world and Locked IS "false". I'm attempting to do that with this command

FT.SEARCH idx "-@Id:{hello\\-world} @Locked:{false}"

But the results are completly wrong, in fact it returns every object regardless of if their Id is NOT equal to the one specified in the query and if Locked is false. However I've found that by swapping Id and Locked the results are correct which got me thinking that the NOT operator applied to Id is negating the whole query.

Am I doing something wrong or is this intended behavior? The workaround is fine but what if I need to search with two negative queries?


Solution

  • This is a known issue with DIALECT 1, which is currently the default one.

    Try using DIALECT 2 and above, either with the command itself:

    FT.SEARCH idx "-@Id:{hello\\-world} @Locked:{false}" DIALECT 2
    

    Or by setting the default dialect (when loading the module, or after with FT.CONFIG):

    FT.CONFIG SET DEFAULT_DIALECT 2
    

    See the documentation for more information