-
Notifications
You must be signed in to change notification settings - Fork 63
query params #152
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
base: master
Are you sure you want to change the base?
query params #152
Changes from 4 commits
76b3bc4
0dc3534
14586ea
f3bbad6
a2b2796
a398487
1c938c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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) | ||
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 | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incompatible variable type: query_params is declared to have type |
||
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 | ||
|
||
|
@@ -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): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to have something like:
WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
There was a problem hiding this comment.
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 argumentquery_params
isNone
?(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 forAggregateRequest
)