How to model a boolean at index creation time?
Last updated 22, Mar 2024
Question
How to model a boolean at index creation time?
Answer
In terms of efficiency, it is better to map a boolean as TAG
. Internally, boolean values are currently indexed as numeric 1
and 0
. Using the JSON format, this can be set as follows:
FT.CREATE test on JSON prefix 1 test: SCHEMA $.bool as bool TAG
JSON.SET test:1 $ '{"bool":true}'
FT.SEARCH test * RETURN 1 bool
1) (integer) 1
2) "test:1"
3) 1) "bool"
2) "1"
Use DIALECT 3
to return the output as JSON and print the stored attribute as inserted:
FT.SEARCH test * RETURN 1 bool DIALECT 3
1) (integer) 1
2) "test:1"
3) 1) "bool"
2) "[true]"
References
Gain more insights about the Redis numeric index.