Skip to content

Instantly share code, notes, and snippets.

@GavinGan23
Last active July 19, 2024 08:49
Show Gist options
  • Save GavinGan23/c7128f7a1ff927e2d5038892308cf2db to your computer and use it in GitHub Desktop.
Save GavinGan23/c7128f7a1ff927e2d5038892308cf2db to your computer and use it in GitHub Desktop.
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())
@GavinGan23
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment