Range query in Redisearch with Python client

1.4k views Asked by At

I'm trying to query a range of values in Redisearch with the python client but it's not reading the space in between the values correctly. Any thoughts on how to fix?

conn = redis.Redis(host='localhost', port=6379, db=0)

q = 'FT.SEARCH idx "@date:[20200101 20200301]" LIMIT 0 100'
conn.execute_command(q)

Throws the error:

--------------------------------------------------------------------------
ResponseError                             Traceback (most recent call last)
<ipython-input-31-5321b184194e> in <module>
      2 q = f'''FT.SEARCH idx "@date:[20200101 20200301]" LIMIT 0 1000000'''
      3 
----> 4 res = conn.execute_command(q)
      5 print(res)

C:\Miniconda3\envs\ssnc\lib\site-packages\redis\client.py in execute_command(self, *args, **options)
    899         try:
    900             conn.send_command(*args)
--> 901             return self.parse_response(conn, command_name, **options)
    902         except (ConnectionError, TimeoutError) as e:
    903             conn.disconnect()

C:\Miniconda3\envs\ssnc\lib\site-packages\redis\client.py in parse_response(self, connection, command_name, **options)
    913         "Parses a response from the Redis server"
    914         try:
--> 915             response = connection.read_response()
    916         except ResponseError:
    917             if EMPTY_RESPONSE in options:

C:\Miniconda3\envs\ssnc\lib\site-packages\redis\connection.py in read_response(self)
    754 
    755         if isinstance(response, ResponseError):
--> 756             raise response
    757         return response
    758 

ResponseError: Unknown argument `20200301]"` at position 1 for <main>
1

There are 1 answers

0
Andrew Brookins On BEST ANSWER

Try passing the command separately from the arguments. Here's an example:

conn.execute_command('ft.search', 'books-idx', '@average_rating:[0 1]')

We also have a dedicated Python library for RediSearch built on top of redis-py: https://github.com/RediSearch/redisearch-py