-
-
Save GavinGan23/c7128f7a1ff927e2d5038892308cf2db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import heapq | |
import requests | |
import flask | |
import search | |
result = [] | |
@search.app.route('/') | |
def show_index(): | |
"""Display / route.""" | |
# Connect to database | |
connection = search.model.get_db() | |
weight = flask.request.args.get("w", 0.5) | |
query_curr = flask.request.args.get("q") | |
if not query_curr: | |
return flask.render_template("index.html", **{"search_r": 0}) | |
url_list = search.app.config['SEARCH_INDEX_SEGMENT_API_URLS'] | |
thread_send1 = threading.Thread(target=make_request, | |
args=(query_curr, weight, url_list[0])) | |
thread_send1.start() | |
thread_send2 = threading.Thread(target=make_request, | |
args=(query_curr, weight, url_list[1])) | |
thread_send2.start() | |
thread_send3 = threading.Thread(target=make_request, | |
args=(query_curr, weight, url_list[2])) | |
thread_send3.start() | |
thread_send3.join() | |
thread_send2.join() | |
thread_send1.join() | |
output = [] | |
web = [] | |
for item in result: | |
output.append(item['hits']) | |
merged_results = heapq.merge(*output, | |
key=lambda x: x['score'], reverse=True) | |
# print(list(merged_results)[:10]) | |
for item in list(merged_results)[:10]: | |
cur = connection.execute( | |
"SELECT url, title, summary FROM Documents WHERE docid = %s", (item['docid'],) | |
) | |
hit = cur.fetchone() | |
web.append(hit) | |
search_r = 1 | |
if len(web) == 0: | |
search_r = 0 | |
print(web) | |
# Add database info to context | |
context = {"webs": web, | |
"search_r": search_r, "weight": weight, "query": query_curr} | |
return flask.render_template("index.html", **context) | |
def make_request(query, weight, url): | |
"""Display / route.""" | |
parameter = {"q": query, "w": weight} | |
response = requests.get(url, params=parameter, timeout=10) | |
result.append(response.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code snippets is only accessible to individuals who got the link. I can't share the entire source code for the concern of violating Engineering Honor code. The Code snippets above is provided to show my work.