diff options
author | Munyoki Kilyungi | 2023-10-14 11:49:44 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-10-27 13:45:32 +0300 |
commit | a5e447a6faf4ddd4788fabcdb8f1d5c676e61439 (patch) | |
tree | a455af47ba2995f47599d76d41380473f8d3af9c | |
parent | 50349f7e067bd894d4aa4865ded5589ee1fcd9e9 (diff) | |
download | genenetwork3-a5e447a6faf4ddd4788fabcdb8f1d5c676e61439.tar.gz |
Implement "GET /metadata/datasets/search/:term".
* gn3/api/metadata.py (search_datasets): New search endpoint with
pagination.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r-- | gn3/api/metadata.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index 877ff7c..fd3166c 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -6,6 +6,7 @@ from http.client import RemoteDisconnected from urllib.error import URLError from flask import Blueprint from flask import jsonify +from flask import request from flask import current_app from pyld import jsonld @@ -120,6 +121,81 @@ CONSTRUCT { # The virtuoso server is misconfigured or it isn't running at all except (RemoteDisconnected, URLError): return jsonify({}) + + +@metadata.route("/datasets/search/<term>", methods=["GET"]) +def search_datasets(term): + """Search datasets""" + try: + args = request.args + page = args.get("page", 0) + page_size = args.get("limit", 10) + sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT")) + sparql.setQuery(Template(""" +$prefix + +CONSTRUCT { + ex:result rdf:type ex:resultType ; + ex:totalCount ?totalCount ; + ex:currentPage $offset ; + ex:items [ + rdfs:label ?label ; + dct:title ?title ; + ex:belongsToInbredSet ?inbredSetName ; + xkos:classifiedUnder ?datasetType + ] +} WHERE { +{ + SELECT ?dataset ?label ?inbredSetName ?datasetType ?title WHERE { + ?dataset rdf:type dcat:Dataset ; + rdfs:label ?label ; + xkos:classifiedUnder ?inbredSet . + ?inbredSet ^skos:member gnc:Set ; + rdfs:label ?inbredSetName . + ?label bif:contains "'$term'" . + OPTIONAL { + ?dataset dct:title ?title . + } . + OPTIONAL { + ?classification ^xkos:classifiedUnder ?dataset ; + ^skos:member gnc:DatasetType ; + ?typePredicate ?typeName ; + skos:prefLabel ?datasetType . + } + } ORDER BY ?dataset LIMIT $limit OFFSET $offset +} + +{ + SELECT (COUNT(*)/$limit+1 AS ?totalCount) WHERE { + ?dataset rdf:type dcat:Dataset ; + rdfs:label ?label . + ?label bif:contains "'$term'" . + } +} + +} +""").substitute(prefix=RDF_PREFIXES, term=term, limit=page_size, offset=page)) + return jsonld.frame( + json.loads( + sparql.queryAndConvert().serialize(format="json-ld")), + { + "@context": PREFIXES | { + "data": "@graph", + "type": "@type", + "id": "@id", + "inbredSet": "ex:belongsToInbredSet", + "classifiedUnder": "xkos:classifiedUnder", + "dataset": "rdfs:label", + "title": "dct:title", + "currentPage": "ex:currentPage", + "result": "ex:result", + "results": "ex:items", + "resultItem": "ex:resultType", + "pages": "ex:totalCount" + }, + "type": "resultItem", + } + ) # The virtuoso server is misconfigured or it isn't running at all except (RemoteDisconnected, URLError): return jsonify({}) |