Skip to content

Commit 83c6563

Browse files
authored
Merge pull request #1015 from hubmapconsortium/Derek-Furst/new-reindex-optimization
changed get_dataset_documents to use an included fields model instead of excluded like the rest
2 parents 5dfedd6 + 701bb00 commit 83c6563

2 files changed

Lines changed: 25 additions & 26 deletions

File tree

src/app.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,25 +1349,30 @@ def alphabetize_dict_recursive(obj):
13491349
def get_dataset_documents(uuid):
13501350
validate_token_if_auth_header_exists(request)
13511351
token = get_internal_token()
1352-
excluded_fields = None
1352+
include_fields = None
13531353
if bool(request.args):
1354-
excluded = request.args.get('exclude')
1355-
if excluded:
1356-
excluded_fields = [
1354+
included = request.args.get('include')
1355+
if included:
1356+
include_fields = [
13571357
f.strip().strip("'").strip('"')
1358-
for f in excluded.split(',')
1358+
for f in included.split(',')
13591359
if f.strip()
13601360
]
1361+
# Validation step to ensure fields are real property names
1362+
valid_fields = set(schema_manager.get_persistent_fields())
1363+
invalid = [f for f in include_fields if f not in valid_fields]
1364+
if invalid:
1365+
return bad_request_error(f"Invalid include fields: {invalid}")
1366+
else:
1367+
return bad_request_error("Missing required parameter: 'include'. Must include a list of properties to be returned.")
1368+
else:
1369+
return bad_request_error("Missing required parameter: 'include'. Must include a list of properties to be returned.")
13611370

1362-
# This is a validation step. Because we're allowing excluded fields to be passed from search-api,
1363-
# we want to minimally at least make sure these are real property names before using them for
1364-
# querying neo4j.
1365-
valid_fields = set(schema_manager.get_persistent_fields())
1366-
invalid = [f for f in excluded_fields if f not in valid_fields]
1367-
if invalid:
1368-
return bad_request_error(f"Invalid excluded fields: {invalid}")
1369-
1370-
entity_record = app_neo4j_queries.get_dataset_documents_raw(neo4j_driver_instance, uuid, excluded_fields=excluded_fields)
1371+
entity_record = app_neo4j_queries.get_dataset_documents_raw(
1372+
neo4j_driver_instance,
1373+
uuid,
1374+
included_fields=include_fields
1375+
)
13711376
if entity_record is None:
13721377
return not_found_error(f"Entity {uuid} not found")
13731378

src/app_neo4j_queries.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,7 @@ def get_source_samples(neo4j_driver, uuid):
256256
are found, or None if the input UUID does not correspond to a supported
257257
entity type.
258258
"""
259-
def get_dataset_documents_raw(neo4j_driver, uuid, excluded_fields=None):
260-
if excluded_fields is None:
261-
excluded_fields = []
262-
259+
def get_dataset_documents_raw(neo4j_driver, uuid, included_fields):
263260
with neo4j_driver.session() as session:
264261
entity_record = session.run("""
265262
MATCH (e:Entity {uuid: $uuid})
@@ -279,22 +276,19 @@ def get_dataset_documents_raw(neo4j_driver, uuid, excluded_fields=None):
279276
root_label = 'Upload'
280277
else:
281278
return None
282-
283-
projection = "d { .* }"
284279

285-
if excluded_fields:
286-
null_projection = ", ".join(f"{field}: NULL" for field in excluded_fields)
287-
projection = f"d {{ .*, {null_projection} }}"
288280

289281
record = session.run("""
290282
MATCH (root:%s {uuid: $uuid})<-[:%s]-(d:Dataset)
291-
RETURN apoc.map.fromPairs(COLLECT([d.uuid, %s])) AS result
292-
""" % (root_label, relationship, projection), uuid=uuid).single()
283+
WITH apoc.coll.toSet(COLLECT(d)) AS datasets
284+
RETURN [d IN datasets | d { %s }] AS result
285+
""" % (root_label, relationship, ', '.join(f'.{f}' for f in included_fields)),
286+
uuid=uuid).single()
293287

294288
if not record or not record["result"]:
295289
return {}
296290

297-
return {uuid: dict(props) for uuid, props in record["result"].items()}
291+
return {d['uuid']: dict(d) for d in record["result"]}
298292

299293

300294

0 commit comments

Comments
 (0)