Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query params #152

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions redisearch/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Dict, Union
from redis import Redis, ConnectionPool
import itertools
import time
Expand Down Expand Up @@ -501,19 +502,30 @@ def info(self):
it = six.moves.map(to_string, res)
return dict(six.moves.zip(it, it))

def _mk_query_args(self, query):
def get_params_args(self, params: Dict[str, Union[str, int, float]]):
args = []
if len(params) > 0:
args.append("PARAMS")
args.append(len(params)*2)
for key, value in params.items():
args.append(key)
args.append(value)
return args

def _mk_query_args(self, query, query_params):
args = [self.index_name]

if isinstance(query, six.string_types):
# convert the query from a text to a query object
query = Query(query)
if not isinstance(query, Query):
raise ValueError("Bad query type %s" % type(query))

args += query.get_args()
if query_params is not None:
args+= self.get_params_args(query_params)
return args, query

def search(self, query):
def search(self, query, query_params: Dict[str, Union[str, int, float]] = None):
"""
Search the index for a given query, and return a result of documents

Expand All @@ -522,7 +534,7 @@ def search(self, query):
- **query**: the search query. Either a text for simple queries with default parameters, or a Query object for complex queries.
See RediSearch's documentation on query format
"""
args, query = self._mk_query_args(query)
args, query = self._mk_query_args(query, query_params=query_params)
st = time.time()
res = self.redis.execute_command(self.SEARCH_CMD, *args)

Expand All @@ -532,11 +544,11 @@ def search(self, query):
has_payload=query._with_payloads,
with_scores=query._with_scores)

def explain(self, query):
args, query_text = self._mk_query_args(query)
def explain(self, query, query_params: Dict[str, Union[str, int, float]] = None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incompatible variable type: query_params is declared to have type Dict[str, Union[float, int, str]] but is used as type None.
(at-me in a reply with help or ignore)

args, query_text = self._mk_query_args(query, query_params=query_params)
return self.redis.execute_command(self.EXPLAIN_CMD, *args)

def aggregate(self, query):
def aggregate(self, query, query_params: Dict[str, Union[str, int, float]] = None):
"""
Issue an aggregation query

Expand All @@ -556,7 +568,8 @@ def aggregate(self, query):
self.index_name] + query.build_args()
else:
raise ValueError('Bad query', query)

if query_params is not None:
cmd+= self.get_params_args(query_params)
raw = self.redis.execute_command(*cmd)
if has_cursor:
if isinstance(query, Cursor):
Expand Down
2 changes: 1 addition & 1 deletion redisearch/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def get_args(self):

args += self._summarize_fields + self._highlight_fields
args += ["LIMIT", self._offset, self._num]

return args

def paging(self, offset, num):
Expand Down Expand Up @@ -288,7 +289,6 @@ def sort_by(self, field, asc=True):
self._sortby = SortbyField(field, asc)
return self


class Filter(object):

def __init__(self, keyword, field, *args):
Expand Down
63 changes: 63 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,69 @@ def testSearchReturnFields(self):
self.assertEqual('doc:1', total[0].id)
self.assertEqual('telmatosaurus', total[0].txt)

def test_text_params(self):
conn = self.redis()

with conn as r:
# Creating a client with a given index name
client = Client('idx', port=conn.port)
client.redis.flushdb()
client.create_index((TextField('name'),))

client.add_document('doc1', name='Alice')
client.add_document('doc2', name='Bob')
client.add_document('doc3', name='Carol')

params_dict = {"name1":"Alice", "name2":"Bob"}
q = Query("@name:($name1 | $name2 )")
res = client.search(q, query_params=params_dict)
self.assertEqual(2, res.total)
self.assertEqual('doc1', res.docs[0].id)
self.assertEqual('doc2', res.docs[1].id)


def test_numeric_params(self):
conn = self.redis()

with conn as r:
# Creating a client with a given index name
client = Client('idx', port=conn.port)
client.redis.flushdb()
client.create_index((NumericField('numval'),))

client.add_document('doc1', numval=101)
client.add_document('doc2', numval=102)
client.add_document('doc3', numval=103)

params_dict = {"min":101, "max":102}
q = Query('@numval:[$min $max]')
res = client.search(q, query_params=params_dict)
self.assertEqual(2, res.total)
self.assertEqual('doc1', res.docs[0].id)
self.assertEqual('doc2', res.docs[1].id)

def test_geo_params(self):
conn = self.redis()

with conn as r:
# Creating a client with a given index name
client = Client('idx', port=conn.port)
client.redis.flushdb()
client.create_index((GeoField('g'),))

client.add_document('doc1', g='29.69465, 34.95126')
client.add_document('doc2', g='29.69350, 34.94737')
client.add_document('doc3', g='29.68746, 34.94882')

params_dict = {"lat":'34.95126', "lon":'29.69465', "radius":10, "units":"km"}
q = Query('@g:[$lon $lat $radius $units]')
res = client.search(q, query_params=params_dict)
self.assertEqual(3, res.total)
self.assertEqual('doc1', res.docs[0].id)
self.assertEqual('doc2', res.docs[1].id)
self.assertEqual('doc3', res.docs[2].id)



if __name__ == '__main__':
unittest.main()