Skip to content

Commit

Permalink
Implementation of Query filter
Browse files Browse the repository at this point in the history
This allows to search by a keyword. Example:
```
/search-api/documents?filter={"query":"diffraction"}
```

This should returns the document that are related to diffraction
  • Loading branch information
antolinos committed Jan 26, 2023
1 parent 933fcf7 commit 1fee761
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
21 changes: 7 additions & 14 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ 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 @@ -224,8 +223,7 @@ 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 @@ -236,8 +234,7 @@ 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 @@ -247,8 +244,7 @@ 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 @@ -258,8 +254,7 @@ 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 @@ -270,8 +265,7 @@ 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 @@ -289,8 +283,7 @@ 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 = {
Expand Down
20 changes: 5 additions & 15 deletions datagateway_api/src/datagateway_api/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
IncludeFilter,
LimitFilter,
OrderFilter,
ScoringQueryFilter,
SkipFilter,
WhereFilter,
)
Expand Down Expand Up @@ -57,22 +56,16 @@ 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 @@ -110,9 +103,7 @@ 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 @@ -230,8 +221,7 @@ 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
4 changes: 3 additions & 1 deletion datagateway_api/src/search_api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
PythonICATLimitFilter,
PythonICATSkipFilter,
PythonICATWhereFilter,
ScoringQueryFilter
ScoringQueryFilter,
)
from datagateway_api.src.search_api.models import PaNOSCAttribute
from datagateway_api.src.search_api.panosc_mappings import mappings
Expand Down Expand Up @@ -162,13 +162,15 @@ 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):
self.included_filters = included_filters
Expand Down
11 changes: 3 additions & 8 deletions datagateway_api/src/search_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,7 @@ 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 @@ -226,13 +224,10 @@ 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"))
Expand Down
4 changes: 4 additions & 0 deletions datagateway_api/src/search_api/query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datagateway_api.src.search_api.filters import (
SearchAPIIncludeFilter,
SearchAPILimitFilter,
SearchAPIScoringFilter,
SearchAPISkipFilter,
SearchAPIWhereFilter,
)
Expand Down Expand Up @@ -62,6 +63,9 @@ def get_query_filter(request_filter, entity_name=None, related_entity_name=None)
elif filter_name == "skip":
log.info("skip JSON object found")
query_filters.append(SearchAPISkipFilter(filter_input))
elif filter_name == "query":
log.info("query JSON object found")
query_filters.append(SearchAPIScoringFilter(filter_input))
else:
raise FilterError(
"No valid filter name given within filter query param:"
Expand Down

0 comments on commit 1fee761

Please sign in to comment.