Skip to content

Commit 45cac60

Browse files
authored
Proper handling of query parameter q with multiple search terms for TinyDB provider. (geopython#2053)
1 parent c2510bb commit 45cac60

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

pygeoapi/provider/tinydb_.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,45 @@ def _add_extra_fields(self, json_data: dict) -> dict:
401401

402402
return json_data
403403

404+
def _prepare_q_param_with_spaces(self, s: str) -> str:
405+
"""
406+
Prepare a search statement for the search term `s`.
407+
The term `s` might have spaces.
408+
409+
Examples (f is shorthand for Q.properties["_metadata-anytext"]):
410+
+---------------+--------------------+
411+
| search term | TinyDB search |
412+
+---------------+--------------------+
413+
| 'aa' | f.search('aa') |
414+
| 'aa bb' | f.search('aa +bb') |
415+
| ' aa bb ' | f.search('aa +bb') |
416+
+---------------+--------------------+
417+
"""
418+
return 'Q.properties["_metadata-anytext"].search("' \
419+
+ ' +'.join(s.split()) \
420+
+ '", flags=re.IGNORECASE)'
421+
404422
def _add_search_query(self, query: list, search_term: str = None) -> str:
405-
if search_term is not None:
423+
"""
424+
Create a search query according to the OGC API - Records specification.
425+
426+
https://docs.ogc.org/is/20-004r1/20-004r1.html (Listing 14)
427+
428+
Examples (f is shorthand for Q.properties["_metadata-anytext"]):
429+
+-------------+-----------------------------------+
430+
| search term | TinyDB search |
431+
+-------------+-----------------------------------+
432+
| 'aa' | f.search('aa') |
433+
| 'aa,bb' | f.search('aa')|f.search('bb') |
434+
| 'aa,bb cc' | f.search('aa')|f.search('bb +cc') |
435+
+-------------+-----------------------------------+
436+
"""
437+
if search_term is not None and len(search_term) > 0:
406438
LOGGER.debug('catalogue q= query')
407-
for t in search_term.split():
408-
query.append(f"(Q.properties['_metadata-anytext'].search('{t}', flags=re.IGNORECASE))") # noqa
439+
terms = [s for s in search_term.split(',') if len(s) > 0]
440+
query.append('|'.join(
441+
[self._prepare_q_param_with_spaces(t) for t in terms]
442+
))
409443

410444
return query
411445

tests/test_tinydb_catalogue_provider.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ def test_query(config):
133133
assert results['numberMatched'] == 6
134134
assert results['numberReturned'] == 6
135135

136-
results = p.query(q='crops barley')
137-
assert len(results['features']) == 2
138-
assert results['numberMatched'] == 2
139-
assert results['numberReturned'] == 2
136+
results = p.query(q='Frost free')
137+
assert len(results['features']) == 1
138+
assert results['numberMatched'] == 1
139+
assert results['numberReturned'] == 1
140140

141141
results = p.query(limit=1)
142142
assert len(results['features']) == 1

0 commit comments

Comments
 (0)