Skip to content

Commit

Permalink
Revert change of the case of endpoints, implementation of the query f…
Browse files Browse the repository at this point in the history
…ilter
  • Loading branch information
antolinos committed Jan 26, 2023
1 parent 30ebd12 commit 933fcf7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 22 deletions.
27 changes: 17 additions & 10 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def create_api_endpoints(flask_app, api, specs):
# DataGateway API endpoints
if Config.config.datagateway_api is not None:
datagateway_api_spec = next(
(spec for spec in specs if spec.title == "DataGateway API"), None,
(spec for spec in specs if spec.title == "DataGateway API"),
None,
)
try:
backend_type = flask_app.config["TEST_BACKEND"]
Expand Down Expand Up @@ -223,7 +224,8 @@ def create_api_endpoints(flask_app, api, specs):

# Session endpoint
session_endpoint_resource = session_endpoints(
backend, client_pool=icat_client_pool,
backend,
client_pool=icat_client_pool,
)
api.add_resource(
session_endpoint_resource,
Expand All @@ -234,7 +236,8 @@ def create_api_endpoints(flask_app, api, specs):

# Table specific endpoints
instrument_facility_cycle_resource = instrument_facility_cycles_endpoint(
backend, client_pool=icat_client_pool,
backend,
client_pool=icat_client_pool,
)
api.add_resource(
instrument_facility_cycle_resource,
Expand All @@ -244,7 +247,8 @@ def create_api_endpoints(flask_app, api, specs):
datagateway_api_spec.path(resource=instrument_facility_cycle_resource, api=api)

count_instrument_facility_cycle_res = count_instrument_facility_cycles_endpoint(
backend, client_pool=icat_client_pool,
backend,
client_pool=icat_client_pool,
)
api.add_resource(
count_instrument_facility_cycle_res,
Expand All @@ -254,7 +258,8 @@ def create_api_endpoints(flask_app, api, specs):
datagateway_api_spec.path(resource=count_instrument_facility_cycle_res, api=api)

instrument_investigation_resource = instrument_investigation_endpoint(
backend, client_pool=icat_client_pool,
backend,
client_pool=icat_client_pool,
)
api.add_resource(
instrument_investigation_resource,
Expand All @@ -265,7 +270,8 @@ def create_api_endpoints(flask_app, api, specs):
datagateway_api_spec.path(resource=instrument_investigation_resource, api=api)

count_instrument_investigation_res = count_instrument_investigation_endpoint(
backend, client_pool=icat_client_pool,
backend,
client_pool=icat_client_pool,
)
api.add_resource(
count_instrument_investigation_res,
Expand All @@ -283,13 +289,14 @@ def create_api_endpoints(flask_app, api, specs):
# Search API endpoints
if Config.config.search_api is not None:
search_api_spec = next(
(spec for spec in specs if spec.title == "Search API"), None,
(spec for spec in specs if spec.title == "Search API"),
None,
)
search_api_extension = Config.config.search_api.extension
search_api_entity_endpoints = {
"Datasets": "Dataset",
"Documents": "Document",
"Instruments": "Instrument",
"datasets": "Dataset",
"documents": "Document",
"instruments": "Instrument",
}

for endpoint_name, entity_name in search_api_entity_endpoints.items():
Expand Down
7 changes: 7 additions & 0 deletions datagateway_api/src/common/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ class IncludeFilter(QueryFilter):

def __init__(self, included_filters):
self.included_filters = included_filters


class ScoringQueryFilter(QueryFilter):
precedence = 6

def __init__(self, value):
self.value = value
20 changes: 15 additions & 5 deletions datagateway_api/src/datagateway_api/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
IncludeFilter,
LimitFilter,
OrderFilter,
ScoringQueryFilter,
SkipFilter,
WhereFilter,
)
Expand Down Expand Up @@ -56,16 +57,22 @@ def create_filter(self):
elif self.operation == "ilike":
self.field = f"UPPER({self.field})"
where_filter = self.create_condition(
self.field, "like", f"UPPER('%{self.value}%')",
self.field,
"like",
f"UPPER('%{self.value}%')",
)
elif self.operation == "nlike":
where_filter = self.create_condition(
self.field, "not like", f"%{self.value}%",
self.field,
"not like",
f"%{self.value}%",
)
elif self.operation == "nilike":
self.field = f"UPPER({self.field})"
where_filter = self.create_condition(
self.field, "not like", f"UPPER('%{self.value}%')",
self.field,
"not like",
f"UPPER('%{self.value}%')",
)
elif self.operation == "lt":
where_filter = self.create_condition(self.field, "<", self.value)
Expand Down Expand Up @@ -103,7 +110,9 @@ def create_filter(self):
where_filter = self.create_condition(self.field, "not in", self.value)
elif self.operation == "between":
where_filter = self.create_condition(
self.field, "between", f"'{self.value[0]}' and '{self.value[1]}'",
self.field,
"between",
f"'{self.value[0]}' and '{self.value[1]}'",
)
elif self.operation == "regexp":
where_filter = self.create_condition(self.field, "regexp", self.value)
Expand Down Expand Up @@ -221,7 +230,8 @@ def apply_filter(self, query):
PythonICATOrderFilter.join_specs[join_field_str] = "LEFT JOIN"

log.debug(
"Setting query join specs: %s", PythonICATOrderFilter.join_specs,
"Setting query join specs: %s",
PythonICATOrderFilter.join_specs,
)
try:
query.setJoinSpecs(PythonICATOrderFilter.join_specs)
Expand Down
16 changes: 14 additions & 2 deletions datagateway_api/src/resources/search_api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
get_files,
get_files_count,
get_search,
get_search_api_query_filter_list,
get_with_pid,
is_query_filter,
search_api_error_handling,
)

Expand All @@ -30,8 +32,18 @@ class Endpoint(Resource):
@search_api_error_handling
def get(self):
filters = get_filters_from_query_string("search_api", entity_name)
log.debug("Filters: %s", filters)
return get_search(entity_name, filters), 200
# in case there is no query filter then we processed as usual
if not is_query_filter(filters):
return get_search(entity_name, filters), 200
else:
query = get_search_api_query_filter_list(filters)[0].value
entities = get_search(
entity_name,
filters,
"LOWER(o.summary) like '%" + query.lower() + "%'",
)

return entities, 200

get.__doc__ = f"""
---
Expand Down
7 changes: 7 additions & 0 deletions datagateway_api/src/search_api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PythonICATLimitFilter,
PythonICATSkipFilter,
PythonICATWhereFilter,
ScoringQueryFilter
)
from datagateway_api.src.search_api.models import PaNOSCAttribute
from datagateway_api.src.search_api.panosc_mappings import mappings
Expand Down Expand Up @@ -161,6 +162,12 @@ def __init__(self, limit_value):
def apply_filter(self, query):
return super().apply_filter(query.icat_query.query)

class SearchAPIScoringFilter(ScoringQueryFilter):
def __init__(self, query_value):
super().__init__(query_value)

def apply_filter(self, query):
return

class SearchAPIIncludeFilter(PythonICATIncludeFilter):
def __init__(self, included_filters, panosc_entity_name):
Expand Down
33 changes: 28 additions & 5 deletions datagateway_api/src/search_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
SearchAPIIncludeFilter,
SearchAPIWhereFilter,
)
from datagateway_api.src.search_api.filters import SearchAPIScoringFilter
import datagateway_api.src.search_api.models as models
from datagateway_api.src.search_api.query import SearchAPIQuery
from datagateway_api.src.search_api.session_handler import (
Expand Down Expand Up @@ -75,7 +76,7 @@ def create_error_message(e):


@client_manager
def get_search(entity_name, filters):
def get_search(entity_name, filters, str_conditions=None):
"""
Search for data on the given entity, using filters from the request to restrict the
query
Expand All @@ -84,6 +85,8 @@ def get_search(entity_name, filters):
:type entity_name: :class:`str`
:param filters: The list of Search API filters to be applied to the request/query
:type filters: List of specific implementation :class:`QueryFilter`
:param str_conditions: Where clause to be applied to the JPQL query
:type str_conditions: :class:`str`
:return: List of records (in JSON serialisable format) of the given entity for the
query constructed from that and the request's filters
"""
Expand All @@ -96,7 +99,7 @@ def get_search(entity_name, filters):
if isinstance(filter_, SearchAPIIncludeFilter):
entity_relations.extend(filter_.included_filters)

query = SearchAPIQuery(entity_name)
query = SearchAPIQuery(entity_name, str_conditions=str_conditions)

filter_handler = FilterOrderHandler()
filter_handler.add_filters(filters)
Expand Down Expand Up @@ -199,7 +202,9 @@ def get_files(entity_name, pid, filters):

log.info("Getting files of dataset (PID: %s), using request's filters", pid)
log.debug(
"Entity Name: %s, Filters: %s", entity_name, filters,
"Entity Name: %s, Filters: %s",
entity_name,
filters,
)

filters.append(SearchAPIWhereFilter("dataset.pid", pid, "eq"))
Expand All @@ -221,11 +226,29 @@ def get_files_count(entity_name, filters, pid):
"""

log.info(
"Getting number of files for dataset (PID: %s), using request's filters", pid,
"Getting number of files for dataset (PID: %s), using request's filters",
pid,
)
log.debug(
"Entity Name: %s, Filters: %s", entity_name, filters,
"Entity Name: %s, Filters: %s",
entity_name,
filters,
)

filters.append(SearchAPIWhereFilter("dataset.pid", pid, "eq"))
return get_count(entity_name, filters)


def get_search_api_query_filter_list(filters):
"""
Returns the list of SearchAPIQueryFilter that are in the filters array
"""
return list(filter(lambda x: isinstance(x, SearchAPIScoringFilter), filters))


@client_manager
def is_query_filter(filters):
"""
Checks if there is a SearchAPIQueryFilter in the list of filters
"""
return len(get_search_api_query_filter_list(filters)) == 1

0 comments on commit 933fcf7

Please sign in to comment.