Skip to content

Commit

Permalink
Add in mark relevant for search
Browse files Browse the repository at this point in the history
  • Loading branch information
VikParuchuri committed Aug 15, 2022
1 parent d291647 commit 528f6bf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
41 changes: 35 additions & 6 deletions search/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Flask, request
from search import Search
from flask import Flask, request, jsonify
from search import search
from filter import Filter
from storage import DBStorage
import html

app = Flask(__name__)
Expand All @@ -17,7 +18,27 @@
color: gray;
margin-bottom: 30px;
}
.rel-button {
cursor: pointer;
color: blue;
}
</style>
<script>
const relevant = function(query, link){
fetch("/relevant", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"query": query,
"link": link
})
});
}
</script>
"""

search_template = styles + """
Expand All @@ -28,7 +49,7 @@
"""

result_template = """
<p class="site">{rank}: {link}</p>
<p class="site">{rank}: {link} <span class="rel-button" onclick='relevant("{query}", "{link}");'>Relevant</span></p>
<a href="{link}">{title}</a>
<p class="snippet">{snippet}</p>
"""
Expand All @@ -37,8 +58,7 @@ def show_search_form():
return search_template

def run_search(query):
se = Search()
results = se.search(query)
results = search(query)
fi = Filter(results)
filtered = fi.filter()
rendered = search_template
Expand All @@ -53,4 +73,13 @@ def search_form():
query = request.form["query"]
return run_search(query)
else:
return show_search_form()
return show_search_form()

@app.route("/relevant", methods=["POST"])
def mark_relevant():
data = request.get_json()
query = data["query"]
link = data["link"]
storage = DBStorage()
storage.update_relevance(query, link, 10)
return jsonify(success=True)
39 changes: 18 additions & 21 deletions search/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,23 @@ def scrape_page(links):
html.append("")
return html

class Search():
def search(query):
columns = ["query", "rank", "link", "title", "snippet", "html", "created"]
storage = DBStorage()

def __init__(self):
self.storage = DBStorage()
self.columns = ["query", "rank", "link", "title", "snippet", "html", "created"]
stored_results = storage.query_results(query)
if stored_results.shape[0] > 0:
stored_results["created"] = pd.to_datetime(stored_results["created"])
return stored_results[columns]

def search(self, query):
stored_results = self.storage.query_results(query)
if stored_results.shape[0] > 0:
stored_results["created"] = pd.to_datetime(stored_results["created"])
return stored_results[self.columns]

print("No results in database. Using the API.")
results = search_api(query)
html = scrape_page(results["link"])
results["html"] = html
results = results[results["html"].str.len() > 0].copy()
results["query"] = query
results["created"] = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
results = results[self.columns]
results.apply(lambda x: self.storage.insert_row(x), axis=1)
print(f"Inserted {results.shape[0]} records.")
return results
print("No results in database. Using the API.")
results = search_api(query)
html = scrape_page(results["link"])
results["html"] = html
results = results[results["html"].str.len() > 0].copy()
results["query"] = query
results["created"] = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
results = results[columns]
results.apply(lambda x: storage.insert_row(x), axis=1)
print(f"Inserted {results.shape[0]} records.")
return results
6 changes: 6 additions & 0 deletions search/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ def insert_row(self, values):
self.con.commit()
except sqlite3.IntegrityError:
pass
cur.close()

def update_relevance(self, query, link, relevance):
cur = self.con.cursor()
cur.execute('UPDATE results SET relevance=? WHERE query=? AND link=?', [relevance, query, link])
self.con.commit()
cur.close()

0 comments on commit 528f6bf

Please sign in to comment.