Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different resources or #90

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions omero_search_engine/api/v1/resources/query_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def __init__(self, filter, adjust_res=True):
"""
self.resource = filter.get("resource")
self.name = filter.get("name")
if self.name and self.name.lower() == "name":
self.name = "name"
self.value = filter.get("value")
self.operator = filter.get("operator")
if filter.get("set_query_type") and filter.get("query_type"):
Expand Down Expand Up @@ -243,10 +245,13 @@ def get_image_non_image_query(self):
for or_it in self.or_query_group:
checked_list = []
main_or_attribute_ = {}
ss = []
image_or_queries.append(ss)
for resource, or_query in or_it.resources_query.items():
checked_list.append(resource)
if resource == "image":
image_or_queries.append(or_query)
ss += or_query
# image_or_queries.append(or_query)
else:
# non image or filters should be inside the or main
# attributes filters
Expand All @@ -257,29 +262,15 @@ def get_image_non_image_query(self):
res = self.run_query(query, resource)
new_cond = get_ids(res, resource)
if new_cond:
if not main_or_attribute_.get(resource):
main_or_attribute_[resource] = new_cond
else:
main_or_attribute_[resource] = (
main_or_attribute_[resource] + new_cond
)

# main_or_attribute.append(new_cond)
# self.additional_image_conds.append(new_cond)
for cond in new_cond:
cond.resource = "image"
ss.append(cond)
else:
# check if all the conditions have been checked
if len(main_or_attribute_.keys()) == 0 and len(
checked_list
) == len(or_it.resources_query):
return {"Error": "Your query returns no results"}
for res, items_ in main_or_attribute_.items():
if res not in main_or_attribute:
main_or_attribute[res] = items_
else:
main_or_attribute[res] = combine_conds(
main_or_attribute[res], items_, res
)

if len(self.or_query_group) > 0 and len(image_or_queries) == 0:
no_conds = 0
for res, item in main_or_attribute.items():
Expand Down Expand Up @@ -667,7 +658,7 @@ def determine_search_results_(query_, return_columns=False, return_containers=Fa
"This release does not support search by description."
)
if q_item.query_type == "main_attribute" and (
filter["name"] == "name" # or filter["name"] == "description"
filter["name"].lower() == "name" # or filter["name"] == "description"
):
if isinstance(q_item.value, list):
new_or_filter = []
Expand Down Expand Up @@ -705,7 +696,8 @@ def determine_search_results_(query_, return_columns=False, return_containers=Fa
"This release does not support search by description."
)
if q_item.query_type == "main_attribute" and (
filter["name"] == "name" # or filter["name"] == "description"
filter["name"].lower()
== "name" # or filter["name"] == "description"
):
if isinstance(q_item.value, list):
for val in q_item.value:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ tags:
- Mixed Complex query

parameters:
- name: return_columns
description: return additional columns to help display the results in a table
in: query
type: boolean
required: false
- name: data
in: body
required: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ tags:
- Return containers for a Mixed Complex query

parameters:
- name: return_columns
description: return additional columns to help display the results in a table
in: query
type: boolean
required: false
- name: data
in: body
required: true
Expand Down
216 changes: 121 additions & 95 deletions omero_search_engine/api/v1/resources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ def get_resource_annotation_table(resource_table):

query_template = Template("""{"query": {"bool": {$query}}}""")


# This template is added to the query to return the count of an attribute
count_attr_template = Template(
"""{"key_count": {"terms": {"field": "$field","size": 10000}}}
Expand Down Expand Up @@ -553,142 +552,163 @@ def elasticsearch_query_builder(
for or_filter in or_filters_:
should_values = []
shoud_not_value = []
# should_names = []
main_should_values = []
main_shoud_not_value = []
try:
key = or_filter["name"].strip()
value = or_filter["value"].strip()
value = str(or_filter["value"]).strip()
operator = or_filter["operator"].strip()
except Exception:
return build_error_message(

except Exception as e:
e_message = build_error_message(
"Each Filter needs to have,\
name, value and operator keywords."
name, value and operator keywords. Error message: %s"
% str(e)
)
search_omero_app.logger.info(e_message)

return e_message
# searching using recourse id, e.g. project id
if key and (key.endswith("_id") or key == "id"):
main_clause = main_attribute_query_template_id.substitute( # noqa
attribute=key.strip(),
value=value.strip(),
)

if key not in added_keys:
added_keys.append(key)
if or_filter["operator"].strip() == "equals":
main_should_values.append(main_clause)
elif or_filter["operator"].strip() == "not_equals":
main_shoud_not_value.append(main_clause)
else:
if key not in added_keys:
added_keys.append(key)

if operator == "equals":
if case_sensitive:
should_values.append(
case_sensitive_must_value_condition_template.substitute( # noqa
value=value
)
)
should_values.append(
case_sensitive_must_name_condition_template.substitute(
name=key
)
)
else:
should_values.append(
case_insensitive_must_value_condition_template.substitute( # noqa
value=value
)
)
should_values.append(
case_insensitive_must_name_condition_template.substitute(
name=key
)
)
elif operator == "contains":
value = "*{value}*".format(value=value)
if case_sensitive:
should_values.append(
case_sensitive_wildcard_value_condition_template.substitute( # noqa
wild_card_value=value
if operator == "equals":
if case_sensitive:
should_values.append(
case_sensitive_must_value_condition_template.substitute( # noqa
value=value
)
)
)
should_values.append(
case_sensitive_must_name_condition_template.substitute(
name=key
should_values.append(
case_sensitive_must_name_condition_template.substitute(
name=key
)
)
)
else:
should_values.append(
case_insensitive_wildcard_value_condition_template.substitute( # noqa
wild_card_value=value
else:
should_values.append(
case_insensitive_must_value_condition_template.substitute( # noqa
value=value
)
)
)
should_values.append(
case_insensitive_must_name_condition_template.substitute(
name=key
should_values.append(
case_insensitive_must_name_condition_template.substitute( # noqa
name=key
)
)
)
elif operator in ["not_equals", "not_contains"]:
if operator == "not_contains":
elif operator == "contains":
value = "*{value}*".format(value=value)
if case_sensitive:
shoud_not_value.append(
should_values.append(
case_sensitive_wildcard_value_condition_template.substitute( # noqa
wild_card_value=value
)
)
shoud_not_value.append(
should_values.append(
case_sensitive_must_name_condition_template.substitute(
name=key
)
)
else:
shoud_not_value.append(
should_values.append(
case_insensitive_wildcard_value_condition_template.substitute( # noqa
wild_card_value=value
)
)
shoud_not_value.append(
should_values.append(
case_insensitive_must_name_condition_template.substitute( # noqa
name=key
)
)
else:
if case_sensitive:
shoud_not_value.append(
case_sensitive_must_value_condition_template.substitute( # noqa
value=value
elif operator in ["not_equals", "not_contains"]:
if operator == "not_contains":
value = "*{value}*".format(value=value)
if case_sensitive:
shoud_not_value.append(
case_sensitive_wildcard_value_condition_template.substitute( # noqa
wild_card_value=value
)
)
)
shoud_not_value.append(
case_sensitive_must_name_condition_template.substitute(
name=key
shoud_not_value.append(
case_sensitive_must_name_condition_template.substitute( # noqa
name=key
)
)
else:
shoud_not_value.append(
case_insensitive_wildcard_value_condition_template.substitute( # noqa
wild_card_value=value
)
)
shoud_not_value.append(
case_insensitive_must_name_condition_template.substitute( # noqa
name=key
)
)
)
else:
shoud_not_value.append(
case_insensitive_must_value_condition_template.substitute( # noqa
value=value
if case_sensitive:
shoud_not_value.append(
case_sensitive_must_value_condition_template.substitute( # noqa
value=value
)
)
shoud_not_value.append(
case_sensitive_must_name_condition_template.substitute( # noqa
name=key
)
)
else:
shoud_not_value.append(
case_insensitive_must_value_condition_template.substitute( # noqa
value=value
)
)
shoud_not_value.append(
case_insensitive_must_name_condition_template.substitute( # noqa
name=key
)
)
elif operator in ["lt", "lte", "gt", "gte"]:
if case_sensitive:
should_values.append(
case_sensitive_range_value_condition_template.substitute( # noqa
operator=operator, value=value
)
)
shoud_not_value.append(
case_insensitive_must_name_condition_template.substitute( # noqa
should_values.append(
case_sensitive_must_name_condition_template.substitute(
name=key
)
)
elif operator in ["lt", "lte", "gt", "gte"]:
if case_sensitive:
else:
should_values.append(
case_sensitive_range_value_condition_template.substitute( # noqa
case_insensitive_range_value_condition_template.substitute( # noqa
operator=operator, value=value
)
)
should_values.append(
case_sensitive_must_name_condition_template.substitute(
case_insensitive_must_name_condition_template.substitute(
name=key
)
)
else:
should_values.append(
case_insensitive_range_value_condition_template.substitute( # noqa
operator=operator, value=value
)
)
should_values.append(
case_insensitive_must_name_condition_template.substitute(
name=key
)
)
# must_value_condition
ss = ",".join(should_values)
ff = nested_keyvalue_pair_query_template.substitute(nested=ss)
should_part_list_or.append(ff)
# must_value_condition
if len(main_should_values) > 0:
ss_ = ",".join(main_should_values)
should_part_list_or.append(ss_)
if len(should_values) > 0:
ss = ",".join(should_values)
ff = nested_keyvalue_pair_query_template.substitute(nested=ss)
should_part_list_or.append(ff)
if len(shoud_not_value) > 0:
ss = ",".join(shoud_not_value)
ff = nested_query_template_must_not.substitute(must_not_value=ss)
Expand Down Expand Up @@ -1147,17 +1167,23 @@ def adjust_query_for_container(query):
if or_filters:
for filter in or_filters:
if isinstance(filter, list):
del_list = []
new_or = []
for filter_ in filter:
if filter_.get("resource") == "container":
new_or_filters.append(get_filter_list(filter_))
to_delete_or_filter.append(filter_)
new_or += get_filter_list(filter_)
del_list.append(filter_)
for ff in del_list:
filter.remove(ff)
filter += new_or
else:
if filter.get("resource") == "container":
new_or_filters.append(get_filter_list(filter))
to_delete_or_filter.append(filter)
filter = get_filter_list(filter)
# to_delete_or_filter.append(filter)
else:
or_filters = []
query_details["or_filters"] = or_filters

for filter in to_delete_or_filter:
if filter in or_filters:
or_filters.remove(filter)
Expand Down
Loading