aboutsummaryrefslogtreecommitdiff
path: root/gn3/api
diff options
context:
space:
mode:
authorArun Isaac2023-01-15 16:54:17 +0000
committerArun Isaac2023-01-18 01:15:47 +0000
commite4f572c7e2b4a553ec5231a9d8440c0b39bee5e3 (patch)
treeb19c5518f4700f56d80680bb67d1fd0cd253a7bd /gn3/api
parent98dc0c5b1a67a7c7b97a1fa02211e9f99360edce (diff)
downloadgenenetwork3-e4f572c7e2b4a553ec5231a9d8440c0b39bee5e3.tar.gz
Split off search query parsing into separate function.
* gn3/api/search.py (parse_query): New function. (search_results): Use parse_query.
Diffstat (limited to 'gn3/api')
-rw-r--r--gn3/api/search.py37
1 files changed, 22 insertions, 15 deletions
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: