From e4f572c7e2b4a553ec5231a9d8440c0b39bee5e3 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Sun, 15 Jan 2023 16:54:17 +0000 Subject: Split off search query parsing into separate function. * gn3/api/search.py (parse_query): New function. (search_results): Use parse_query. --- gn3/api/search.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'gn3') diff --git a/gn3/api/search.py b/gn3/api/search.py index f4d0aeb..3d8cbd0 100644 --- a/gn3/api/search.py +++ b/gn3/api/search.py @@ -11,6 +11,27 @@ from gn3.db_utils import xapian_database search = Blueprint("search", __name__) + +def parse_query(query: str): + """Parse search query using GeneNetwork specific field processors.""" + queryparser = xapian.QueryParser() + queryparser.set_stemmer(xapian.Stem("en")) + queryparser.set_stemming_strategy(queryparser.STEM_SOME) + queryparser.add_boolean_prefix("author", "A") + queryparser.add_boolean_prefix("species", "XS") + queryparser.add_boolean_prefix("group", "XG") + queryparser.add_boolean_prefix("tissue", "XI") + queryparser.add_boolean_prefix("dataset", "XDS") + queryparser.add_boolean_prefix("symbol", "XY") + queryparser.add_boolean_prefix("chr", "XC") + queryparser.add_boolean_prefix("peakchr", "XPC") + queryparser.add_prefix("description", "XD") + range_prefixes = ["mean", "peak", "mb", "peakmb", "additive", "year"] + for i, prefix in enumerate(range_prefixes): + queryparser.add_rangeprocessor(xapian.NumberRangeProcessor(i, prefix + ":")) + return queryparser.parse_query(query) + + @search.route("/") def search_results(): """Search Xapian index and return a list of results.""" @@ -25,21 +46,7 @@ def search_results(): if results_per_page > maximum_results_per_page: abort(400, description="Requested too many search results") - queryparser = xapian.QueryParser() - queryparser.set_stemmer(xapian.Stem("en")) - queryparser.set_stemming_strategy(queryparser.STEM_SOME) - queryparser.add_boolean_prefix("author", "A") - queryparser.add_boolean_prefix("species", "XS") - queryparser.add_boolean_prefix("group", "XG") - queryparser.add_boolean_prefix("tissue", "XI") - queryparser.add_boolean_prefix("dataset", "XDS") - queryparser.add_boolean_prefix("symbol", "XY") - queryparser.add_boolean_prefix("chr", "XC") - queryparser.add_boolean_prefix("peakchr", "XPC") - queryparser.add_prefix("description", "XD") - for i, prefix in enumerate(["mean:", "peak:", "mb:", "peakmb:", "additive:", "year:"]): - queryparser.add_rangeprocessor(xapian.NumberRangeProcessor(i, prefix)) - query = queryparser.parse_query(querystring) + query = parse_query(querystring) traits = [] # pylint: disable=invalid-name with xapian_database(current_app.config["XAPIAN_DB_PATH"]) as db: -- cgit v1.2.3