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 4 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
19 changes: 11 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,20 @@ def info(self):
it = six.moves.map(to_string, res)
return dict(six.moves.zip(it, it))

def _mk_query_args(self, query):
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+= Query.get_params_args(query_params)
Copy link

Choose a reason for hiding this comment

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

Do you want to use the member query._params if argument query_params is None?
(or even to merge both if both are not None, giving precedence to the argument over the member?)
Or do you want to get rid of the member? (it is only available for Query, not for AggregateRequest)

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 +524,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 +534,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 +558,8 @@ def aggregate(self, query):
self.index_name] + query.build_args()
else:
raise ValueError('Bad query', query)

if query_params is not None:
cmd+= Query.get_params_args(query_params)
raw = self.redis.execute_command(*cmd)
if has_cursor:
if isinstance(query, Cursor):
Expand Down
35 changes: 35 additions & 0 deletions redisearch/query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Dict, Union
import six

class Query(object):
Expand Down Expand Up @@ -34,6 +35,7 @@ def __init__(self, query_string):
self._summarize_fields = []
self._highlight_fields = []
self._language = None
self._params = {}

def query_string(self):
"""
Expand Down Expand Up @@ -147,6 +149,17 @@ def scorer(self, scorer):
self._scorer = scorer
return self

@staticmethod
def get_params_args(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 get_args(self):
"""
Format the redis arguments for this query and return them
Expand Down Expand Up @@ -209,6 +222,9 @@ def get_args(self):

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

args+= Query.get_params_args(self._params)

return args

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

def set_params_dict(self, params: Dict[str, Union[str, int, float]]):
""""
Add a parameters dictionary. Overwrites an existing parameters dictionary.

- **params** - Dict[str, Union[str, int, float]] dictionary
"""
self._params = params
return self

def set_param(self, param_name:str, value:Union[str, int, float]):
"""
Adds a parameter to the parameters dictionary.

- **param_name** - parmaeter name
- **value** - parameter value
"""
self._params[param_name] = value
return self


class Filter(object):

Expand Down
109 changes: 109 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,115 @@ 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 )").set_params_dict(params=params_dict)
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer to have something like:

params = {"name1":"Alice", "name2":"Bob"}
q = Query("@name:($name1 | $name2 )")
res1 = client.search(params=params)
res2 = client.search(params=other_params)

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we have it, continue reading the test ;)

res = client.search(q)
self.assertEqual(2, res.total)
self.assertEqual('doc1', res.docs[0].id)
self.assertEqual('doc2', res.docs[1].id)

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

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]').set_params_dict(params=params_dict)
res = client.search(q)
self.assertEqual(2, res.total)

self.assertEqual('doc1', res.docs[0].id)
self.assertEqual('doc2', res.docs[1].id)

q = Query('@numval:[$min $max]').set_param("min", 101).set_param("max", 102)
res = client.search(q)
self.assertEqual(2, res.total)

self.assertEqual('doc1', res.docs[0].id)
self.assertEqual('doc2', res.docs[1].id)

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]').set_params_dict(params=params_dict)
res = client.search(q)
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)


q = Query('@g:[$lon $lat $radius $units]').set_param("lat", '34.95126').set_param("lon", '29.69465',).set_param("radius", 10).set_param("units", "km")
res = client.search(q)
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)

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()