Commit f2284710 authored by felix.herrmann's avatar felix.herrmann
Browse files

fix(search): make filters function again by rolling back to old add_tag_filter() function

parent fa7482d6
......@@ -52,22 +52,36 @@ SEARCH_FIELDS_DATASET = [
# }
def add_tag_filter(search, filter_name, filter_list, filter_field):
# Commented out, as filters are not applied using this code.
# Switching back to older version below.
# def add_tag_filter(search, filter_name, filter_list, filter_field):
# """
# Add specific filters dynamically to search object using the alternative elasticsearch-dsl syntax
# see: https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#dotted-fields
# """
# if filter_list:
# query_list = list()
# # construct list of Q match clauses for filtering elements
# for elem in filter_list:
# if elem != "":
# query_list.append(Q("match", **{"{}.name".format(filter_name): elem}))
# # add the list to the search object
# return search.filter("terms", **{filter_name + "." + filter_field: query_list})
# else:
# # return original search object if filter_list is None
# return search
def add_tag_filter(search, filter_name, filter_list):
"""
Add specific filters dynamically to search object using the alternative elasticsearch-dsl syntax
Add specific filters dynamically using the alternative elasticsearch-dsl syntax
see: https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html#dotted-fields
"""
if filter_list:
query_list = list()
# construct list of Q match clauses for filtering elements
for elem in filter_list:
if elem != "":
query_list.append(Q("match", **{"{}.name".format(filter_name): elem}))
# add the list to the search object
return search.filter("terms", **{filter_name + "." + filter_field: query_list})
else:
# return original search object if filter_list is None
return search
search = search.query("match", **{"{}.name".format(filter_name): elem})
return search
def index_search(index, term, **kwargs):
......@@ -134,23 +148,23 @@ def index_search(index, term, **kwargs):
search = document.search().query(filter_query)
# add filters for both dataset_index and user_index to search
search = add_tag_filter(search, "countries", countries, "name")
search = add_tag_filter(search, "countries", countries)
# add filters for dataset_index
if index == "dataset_index":
search = add_tag_filter(search, "keywords", keywords, "name")
search = add_tag_filter(search, "languages_of_data", languages, "name")
search = add_tag_filter(search, "disciplines", disciplines, "name")
search = add_tag_filter(search, "keywords", keywords)
search = add_tag_filter(search, "languages_of_data", languages)
search = add_tag_filter(search, "disciplines", disciplines)
search = add_tag_filter(
search, "methods_of_data_collection", methods_of_data_collection, "name"
search, "methods_of_data_collection", methods_of_data_collection
)
search = add_tag_filter(
search, "methods_of_data_analysis", methods_of_data_analysis, "name"
search, "methods_of_data_analysis", methods_of_data_analysis
)
# add filters for user_index
if index == "user_index":
search = add_tag_filter(search, "interests", keywords, "name")
search = add_tag_filter(search, "interests", keywords)
# return all results, not just 10 (elastic default)
total = search.count()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment