Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit a2c141c

Browse files
authored
Merge pull request #210 from cloudant/add-q-parameter-for-cloudant-search
Add 'q' query parameter for Cloudant Search
2 parents 3995370 + 251b424 commit a2c141c

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

src/cloudant/database.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,8 +1291,9 @@ def get_search_result(self, ddoc_id, index_name, **query_params):
12911291
:param int limit: Optional number to limit the maximum count of the
12921292
returned documents. In case of a grouped search, this parameter
12931293
limits the number of documents per group.
1294-
:param query: A Lucene query in the form of ``name:value``.
1294+
:param query/q: A Lucene query in the form of ``name:value``.
12951295
If name is omitted, the special value ``default`` is used.
1296+
The ``query`` parameter can be abbreviated as ``q``.
12961297
:param ranges: Optional JSON facet syntax that reuses the standard
12971298
Lucene syntax to return counts of results which fit into each
12981299
specified category. Inclusive range queries are denoted by brackets.
@@ -1325,10 +1326,13 @@ def get_search_result(self, ddoc_id, index_name, **query_params):
13251326
13261327
:returns: Search query result data in JSON format
13271328
"""
1328-
if not query_params.get('query'):
1329+
param_q = query_params.get('q')
1330+
param_query = query_params.get('query')
1331+
# Either q or query parameter is required
1332+
if bool(param_q) == bool(param_query):
13291333
raise CloudantArgumentError(
1330-
'No query parameter found. Please add a query parameter '
1331-
'containing Lucene syntax and retry.')
1334+
'A single query/q parameter is required. '
1335+
'Found: {0}'.format(query_params))
13321336

13331337
# Validate query arguments and values
13341338
for key, val in iteritems_(query_params):

tests/unit/database_tests.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,20 @@ def test_get_search_result_with_invalid_argument(self):
12071207
err = cm.exception
12081208
self.assertEqual(str(err), 'Invalid argument: foo')
12091209

1210+
def test_get_search_result_with_both_q_and_query(self):
1211+
"""
1212+
Test get_search_result by passing in both a q and query parameter
1213+
"""
1214+
with self.assertRaises(CloudantArgumentError) as cm:
1215+
self.db.get_search_result('searchddoc001', 'searchindex001',
1216+
query='julia*', q='julia*')
1217+
err = cm.exception
1218+
self.assertEqual(
1219+
str(err),
1220+
'A single query/q parameter is required. '
1221+
'Found: {\'q\': \'julia*\', \'query\': \'julia*\'}'
1222+
)
1223+
12101224
def test_get_search_result_with_invalid_value_types(self):
12111225
"""
12121226
Test get_search_result by passing in invalid value types for
@@ -1252,8 +1266,8 @@ def test_get_search_result_without_query(self):
12521266
err = cm.exception
12531267
self.assertEqual(
12541268
str(err),
1255-
'No query parameter found. Please add a query parameter '
1256-
'containing Lucene syntax and retry.'
1269+
'A single query/q parameter is required. '
1270+
'Found: {\'limit\': 10, \'include_docs\': True}'
12571271
)
12581272

12591273
def test_get_search_result_with_invalid_query_type(self):
@@ -1271,7 +1285,7 @@ def test_get_search_result_with_invalid_query_type(self):
12711285

12721286
def test_get_search_result_executes_search_query(self):
12731287
"""
1274-
Test get_search_result executes a search query
1288+
Test get_search_result executes a search with query parameter.
12751289
"""
12761290
self.create_search_index()
12771291
self.populate_db_with_documents(100)
@@ -1313,6 +1327,28 @@ def test_get_search_result_executes_search_query(self):
13131327
'id': 'julia004', 'order': ['julia004', 1]}], 'total_rows': 100}
13141328
)
13151329

1330+
def test_get_search_result_executes_search_q(self):
1331+
"""
1332+
Test get_search_result executes a search query with q parameter.
1333+
"""
1334+
self.create_search_index()
1335+
self.populate_db_with_documents(100)
1336+
resp = self.db.get_search_result(
1337+
'searchddoc001',
1338+
'searchindex001',
1339+
query='julia*',
1340+
sort='_id<string>',
1341+
limit=1
1342+
)
1343+
self.assertEqual(len(resp['rows']), 1)
1344+
self.assertTrue(resp['bookmark'])
1345+
resp.pop('bookmark')
1346+
self.assertEqual(
1347+
resp,
1348+
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia000',
1349+
'order': ['julia000', 0]}], 'total_rows': 100}
1350+
)
1351+
13161352
def test_get_search_result_executes_search_query_with_group_option(self):
13171353
"""
13181354
Test get_search_result executes a search query with grouping parameters.

0 commit comments

Comments
 (0)