-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_app.py
54 lines (47 loc) · 1.7 KB
/
web_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from elasticsearch import Elasticsearch
from flask import Flask, redirect, url_for, render_template, request
INDEXNAME = 'cv19index'
app = Flask(__name__)
es = Elasticsearch([{'host':'localhost','port':9200}])
#--- Homepage ---#
@app.route("/")
@app.route("/", methods=["POST", "GET"])
def home():
if request.method == "POST":
q = request.form["query"]
return results(q)
else:
return render_template("home.html")
@app.route("/results")
def results(q):
query_dict = {
"query": {
"bool": {
"should": [
{ "match": { "title" : q } },
{ "match": { "abstract" : q } }
]
}
}
}
n_docs = 100
response = es.search(index = INDEXNAME, body = query_dict, size = n_docs)
documents = []
n_chars = 225
for hit in response["hits"]["hits"]:
temp = dict()
temp["id"] = (hit["_id"])
temp["title"] = str(hit["_source"]["title"])
temp["abstract"] = str(hit["_source"]["abstract"])
temp["authors"] = str(hit["_source"]["authors"])
temp["link"] = str(hit["_source"]["url"])
if len(temp["abstract"]) > n_chars:
temp["abs_first"] = temp["abstract"][0:n_chars]
temp["abs_last"] = temp["abstract"][n_chars:]
else:
temp["abs_first"] = temp["abstract"]
temp["abs_last"] = ""
documents.append(temp)
return render_template("results.html", documents = documents, q = q)
if __name__ == "__main__":
app.run(debug = True)