From 3075f0ab8853fd97c68d3f0b8086dfa5c863c7f2 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 21 Jun 2018 18:23:09 -0700 Subject: copy some of paper-search over --- python/config.py | 2 ++ python/fatcat/routes.py | 28 ++++++++++++++++- python/fatcat/search.py | 49 +++++++++++++++++++++++++++++ python/fatcat/templates/base.html | 10 +++--- python/fatcat/templates/release_search.html | 33 +++++++++++++++++++ python/tests/routes.py | 8 ++++- 6 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 python/fatcat/search.py create mode 100644 python/fatcat/templates/release_search.html (limited to 'python') diff --git a/python/config.py b/python/config.py index a7ec4b50..21c1e148 100644 --- a/python/config.py +++ b/python/config.py @@ -6,6 +6,8 @@ class Config(object): SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URI') or \ 'sqlite:///' + os.path.join(basedir, 'fatcat_dev.sqlite') SQLALCHEMY_TRACK_MODIFICATIONS = False + ELASTIC_BACKEND = "http://search.qa.fatcat.wiki:8088" + ELASTIC_INDEX = "crossref-works" # "Event more verbose" debug options. SECRET_KEY is bogus. #SQLALCHEMY_ECHO = True diff --git a/python/fatcat/routes.py b/python/fatcat/routes.py index f4c7c513..8fe74a2d 100644 --- a/python/fatcat/routes.py +++ b/python/fatcat/routes.py @@ -5,11 +5,11 @@ from flask import Flask, render_template, send_from_directory, request, \ url_for, abort, g, redirect, jsonify, session from fatcat import app, api from fatcat_client.rest import ApiException +from fatcat.search import do_search ### Views ################################################################### - @app.route('/container/', methods=['GET']) def container_view(ident): try: @@ -156,6 +156,32 @@ def editor_changelog(username): return render_template('editor_changelog.html', editor=editor, changelog_entries=changelog_entries) +### Search ################################################################## + +@app.route('/release/search', methods=['GET', 'POST']) +def search(): + + limit = 20 + query = request.args.get('q') + + # Convert raw DOIs to DOI queries + if query is not None: + oldquery = query.split() + for word in oldquery: + if word.startswith("10.") and word.count("/") >= 1: + query = query.replace(word, 'doi:"{}"'.format(word)) + + # Convert "author:" query to "authors:" + if query is not None: + query = query.replace("author:", "authors:") + + if 'q' in request.args.keys(): + # always do files for HTML + found = do_search(query, limit=limit) + return render_template('release_search.html', found=found) + else: + return render_template('release_search.html') + ### Static Routes ########################################################### diff --git a/python/fatcat/search.py b/python/fatcat/search.py new file mode 100644 index 00000000..7ed7a99c --- /dev/null +++ b/python/fatcat/search.py @@ -0,0 +1,49 @@ + +import requests +from flask import abort +from fatcat import app + + +def do_search(q, limit=20): + + print("Search hit: " + q) + if limit > 100: + # Sanity check + limit = 100 + + search_request = { + "query": { + "query_string": { + "query": q, + "analyzer": "textIcuSearch", + "default_operator": "AND", + "analyze_wildcard": True, + "lenient": True, + "auto_generate_phrase_queries": True, + "default_field": "_all", + }, + }, + "size": int(limit), + } + + resp = requests.get("%s/%s/_search" % + (app.config['ELASTIC_BACKEND'], app.config['ELASTIC_INDEX']), + json=search_request) + + if resp.status_code != 200: + print("elasticsearch non-200 status code: " + str(resp.status_code)) + print(resp.content) + abort(resp.status_code) + + content = resp.json() + results = [h['_source'] for h in content['hits']['hits']] + for h in results: + # Ensure 'authors' is a list, not a single string + if type(h['authors']) is not list: + h['authors'] = [h['authors'], ] + + found = content['hits']['total'] + return {"query": { "q": q }, + "count_returned": len(results), + "count_found": found, + "results": results } diff --git a/python/fatcat/templates/base.html b/python/fatcat/templates/base.html index ae487a18..ac6fef43 100644 --- a/python/fatcat/templates/base.html +++ b/python/fatcat/templates/base.html @@ -26,10 +26,12 @@ Guide