From 38825876b64cdd08495f515a5d503f3da89385c2 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 8 Apr 2020 14:20:07 -0700 Subject: initial implementation of filters --- fatcat_covid19/search.py | 30 +++++++++++- fatcat_covid19/templates/fulltext_search.html | 70 ++++++++++++++++++++++++--- fatcat_covid19/webface.py | 8 +-- 3 files changed, 98 insertions(+), 10 deletions(-) diff --git a/fatcat_covid19/search.py b/fatcat_covid19/search.py index 82d5c60..7d4596d 100644 --- a/fatcat_covid19/search.py +++ b/fatcat_covid19/search.py @@ -71,13 +71,41 @@ def generic_search_execute(search, limit=25, offset=0, deep_page_limit=2000): "query_time_ms": int(resp.took), } -def do_fulltext_search(q, limit=25, offset=0): +def do_fulltext_search(q, limit=25, offset=0, filter_time=None, filter_type=None): # Convert raw DOIs to DOI queries if len(q.split()) == 1 and q.startswith("10.") and q.count("/") >= 1: q = 'doi:"{}"'.format(q) search = Search(using=app.es_client, index=app.config['ELASTICSEARCH_FULLTEXT_INDEX']) + + # type filters + if filter_type == "papers": + search = search.filter("terms", release_type=[ "article-journal", "paper-conference", ]) + elif filter_type == "reports": + search = search.filter("terms", release_type=[ "report", "standard", ]) + elif filter_type == "datasets": + search = search.filter("terms", release_type=[ "dataset", "software", ]) + elif filter_type == "everything" or filter_type == None: + pass + else: + abort(400) + + # time filters + if filter_time == "past_week": + week_ago_date = str(datetime.date.today() - datetime.timedelta(days=7)) + search = search.filter("range", release_date=dict(gte=week_ago_date)) + elif filter_time == "this_year": + search = search.filter("term", release_year=datetime.date.today().year) + elif filter_time == "since_2000": + search = search.filter("range", release_year=dict(gte=2000)) + elif filter_time == "before_1925": + search = search.filter("range", release_year=dict(lte=1924)) + elif filter_time == "all_time" or filter_time == None: + pass + else: + abort(400) + search = search.query( 'query_string', query=q, diff --git a/fatcat_covid19/templates/fulltext_search.html b/fatcat_covid19/templates/fulltext_search.html index 1d54f78..affb7b5 100644 --- a/fatcat_covid19/templates/fulltext_search.html +++ b/fatcat_covid19/templates/fulltext_search.html @@ -21,13 +21,63 @@ - {# -
- - + +
+ Include:   + +{# + +#} + + + + from + -
Can also lookup by identifier or search for containers (eg, journals). - #}
@@ -89,3 +139,11 @@ {% endif %} {% endblock %} + +{% block postscript %} + +{% endblock %} diff --git a/fatcat_covid19/webface.py b/fatcat_covid19/webface.py index 662a2f0..aa604df 100644 --- a/fatcat_covid19/webface.py +++ b/fatcat_covid19/webface.py @@ -83,15 +83,17 @@ def page_home(): def fulltext_search(): query = request.args.get('q') + filter_type = request.args.get('filter_type') or None + filter_time = request.args.get('filter_time') or None offset = request.args.get('offset', '0') offset = max(0, int(offset)) if offset.isnumeric() else 0 if 'q' in request.args.keys(): - found = do_fulltext_search(query, offset=offset) - return render_template('fulltext_search.html', found=found, query=query) + found = do_fulltext_search(query, offset=offset, filter_type=filter_type, filter_time=filter_time) + return render_template('fulltext_search.html', found=found, query=query, filter_type=filter_type, filter_time=filter_time) else: - return render_template('fulltext_search.html', query=query) + return render_template('fulltext_search.html', query=query, filter_type=filter_type, filter_time=filter_time) @bp.route('/about', methods=['GET']) def page_about(): -- cgit v1.2.3