Skip to content

Commit a23dd5c

Browse files
committed
test fixes
1 parent 754b90b commit a23dd5c

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

pycsw/ogc/api/records.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,9 @@ def items(self, headers_, json_post_data, args, collection='metadata:main'):
647647
collections = ','.join(f'"{x}"' for x in v.split(','))
648648
else:
649649
collections = ','.join(f'"{x}"' for x in v)
650-
query_args.append(f"parentidentifier IN ({collections})")
650+
query_args.append(f"collection IN ({collections})")
651651
elif k == 'anytext':
652-
query_args.append(build_anytext(k, v))
652+
query_args.append(build_anytext(k, v, self.repository))
653653
elif k == 'bbox':
654654
query_args.append(f'BBOX(geometry, {v})')
655655
elif k == 'keywords':
@@ -660,20 +660,24 @@ def items(self, headers_, json_post_data, args, collection='metadata:main'):
660660
else:
661661
begin, end = v.split('/')
662662
if begin != '..':
663-
query_args.append(f'time_begin >= "{begin}"')
663+
query_args.append(f'"properties.datetime" >= "{begin}"')
664+
#query_args.append(f'time_begin >= "{begin}"')
664665
if end != '..':
665666
query_args.append(f'time_end <= "{end}"')
666667
elif k == 'q':
667668
if v not in [None, '']:
668-
query_args.append(build_anytext('anytext', v))
669+
query_args.append(build_anytext('anytext', v, self.repository))
669670
else:
670671
query_args.append(f'{k} = "{v}"')
671672

672673
facets_requested = str2bool(args.get('facets', False))
673674

674675
if collection != 'metadata:main':
675676
LOGGER.debug('Adding virtual collection filter')
676-
query_args.append(f'parentidentifier = "{collection}"')
677+
query_args.append(f'collection = "{collection}"')
678+
679+
if self.repository.dbtype == 'Elasticsearch':
680+
query_args = [qa.replace('"', "'") for qa in query_args]
677681

678682
LOGGER.debug('Evaluating CQL and other specified filtering parameters')
679683
if cql_query is not None and query_args:
@@ -771,10 +775,12 @@ def items(self, headers_, json_post_data, args, collection='metadata:main'):
771775
offset = int(args.get('offset', 0))
772776

773777
if query_parser is not None and cql_query != {}:
778+
LOGGER.debug('Parsing CQL into AST')
774779
LOGGER.debug('Parsing CQL into AST')
775780
LOGGER.debug(json_post_data)
776781
LOGGER.debug(cql_query)
777782
try:
783+
print("CQL QUERY", cql_query)
778784
ast = query_parser(cql_query)
779785
LOGGER.debug(f'Abstract syntax tree: {ast}')
780786
except Exception as err:
@@ -1451,13 +1457,13 @@ def record2json(record, url, collection, mode='ogcapi-records'):
14511457

14521458
return record_dict
14531459

1454-
1455-
def build_anytext(name, value):
1460+
def build_anytext(name, value, repository):
14561461
"""
14571462
deconstructs free-text search into CQL predicate(s)
14581463
14591464
:param name: property name
1460-
:param name: property value
1465+
:param value: property value
1466+
:param repository: repository object
14611467
14621468
:returns: string of CQL predicate(s)
14631469
"""
@@ -1470,17 +1476,30 @@ def build_anytext(name, value):
14701476

14711477
if len(tokens) == 1 and ' ' not in value: # single term
14721478
LOGGER.debug('Single term with no spaces')
1473-
return f"{name} ILIKE '%{value}%'"
1479+
predicates = [f"{name} ILIKE '%{value}%'"]
14741480

1475-
for token in tokens:
1476-
if ' ' in token:
1477-
tokens2 = token.split()
1478-
predicates2 = []
1479-
for token2 in tokens2:
1480-
predicates2.append(f"{name} ILIKE '%{token2}%'")
1481+
else:
1482+
for token in tokens:
1483+
if ' ' in token:
1484+
tokens2 = token.split()
1485+
predicates2 = []
1486+
for token2 in tokens2:
1487+
predicates2.append(f"{name} ILIKE '%{token2}%'")
14811488

1482-
predicates.append('(' + ' AND '.join(predicates2) + ')')
1483-
else:
1484-
predicates.append(f"{name} ILIKE '%{token}%'")
1489+
predicates.append('(' + ' AND '.join(predicates2) + ')')
1490+
else:
1491+
predicates.append(f"{name} ILIKE '%{token}%'")
1492+
1493+
if name == 'anytext' and repository.dbtype == 'Elasticsearch':
1494+
predicates2 = []
1495+
for p in predicates:
1496+
p2 = p
1497+
predicates2.append(p2.replace(name, '"properties.title"'))
1498+
predicates2.append(p2.replace(name, '"properties.description"'))
1499+
1500+
predicates = predicates2
14851501

1486-
return f"({' OR '.join(predicates)})"
1502+
if len(predicates) == 1:
1503+
return predicates[0]
1504+
else:
1505+
return f"({' OR '.join(predicates)})"

pycsw/plugins/repository/elastic.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
class Record(Document):
4545
identifier = Text()
4646
parentidentifier = Text()
47+
# anytext = Text()
48+
# datetime = Text()
4749

4850

4951
class ElasticsearchRepository:
@@ -69,17 +71,20 @@ def __init__(self, repo_object: dict, context):
6971

7072
self.query_mappings = {
7173
# OGC API - Records mappings
74+
'geometry': 'geometry',
7275
'identifier': 'identifier',
7376
'type': 'type',
7477
'typename': 'typename',
7578
'parentidentifier': 'parentidentifier',
79+
'collection': 'parentidentifier',
7680
'collections': 'parentidentifier',
7781
'updated': 'insert_date',
7882
'title': 'title',
83+
'abstract': 'abstract',
7984
'description': 'abstract',
8085
'keywords': 'keywords',
8186
'edition': 'edition',
82-
'anytext': 'anytext',
87+
'anytext': 'title',
8388
'bbox': 'wkt_geometry',
8489
'date': 'date',
8590
'datetime': 'date',
@@ -184,7 +189,7 @@ def query_collections(self, filters=None, limit=10) -> list:
184189
raise RuntimeError(msg)
185190

186191
for hit in response['hits']['hits']:
187-
if hit['_source']['type'] in ['Collection', 'stac:Collection']:
192+
if hit['_source']['type'] == 'Collection':
188193
dataset = type('dataset', (object,), hit['_source'])
189194
dataset.identifier = dataset.id
190195
dataset.title = hit['_source'].get('title')
@@ -288,8 +293,9 @@ def query(self, constraint=None, sortby=None, typenames=None,
288293

289294
if constraint.get('ast') is not None:
290295
LOGGER.debug('Applying filter')
296+
291297
es_query = es_query.query(
292-
to_filter(constraint['ast'], {'ows:BoundingBox': 'geometry'},
298+
to_filter(constraint['ast'], self.query_mappings,
293299
version=ES_VERSION)).extra(track_total_hits=True)
294300

295301
es_response = es_query.execute()

0 commit comments

Comments
 (0)