An open source, non-profit web search engine
Update results as you type
Daoud Clarkeaf29b4c039c5
diff --git a/app.py b/app.pyindex 909030f..9bb801f 100644--- a/app.py+++ b/app.py@@ -1,3 +1,5 @@+import logging+import uvicornimport create_app@@ -8,6 +10,8 @@ from paths import INDEX_PATHtiny_index = TinyIndex(Document, INDEX_PATH, NUM_PAGES, PAGE_SIZE)app = create_app.create(tiny_index)+logging.basicConfig()+if __name__ == "__main__":uvicorn.run("app:app", host="127.0.0.1", port=8000, log_level="info")diff --git a/create_app.py b/create_app.pyindex 1aafff8..590b62a 100644--- a/create_app.py+++ b/create_app.py@@ -1,3 +1,4 @@+from logging import getLoggerfrom typing import Listimport Levenshtein@@ -8,22 +9,17 @@ from starlette.staticfiles import StaticFilesfrom index import TinyIndex, Document+logger = getLogger(__name__)++def create(tiny_index: TinyIndex):app = FastAPI()@app.get("/search")def search(s: str):results = get_results(s)- doc = ""- for result in results:- doc += f'<p><a href="{result.url}">{result.title}</a></p>\n'- return HTMLResponse(doc)-- # if '—' in s:- # url = s.split('—')[1].strip()- # else:- # url = f'https://www.google.com/search?q={s}'- # return RedirectResponse(url)+ logger.info("Return results: %r", results)+ return resultsdef order_results(query, results: List[Document]):ordered_results = sorted(results, key=lambda result: Levenshtein.distance(query, result.title))diff --git a/static/index.html b/static/index.htmlindex 2bd6812..84d2ee6 100644--- a/static/index.html+++ b/static/index.html@@ -5,12 +5,17 @@type="application/opensearchdescription+xml"title="Stoatally Different"href="http://localhost:8000/plugin.xml">+<script src="/index.js"></script></head><body>Stoatally different.<form action="/search">- <input type="search" name="s" />+ <input type="search" id="search" name="s" /></form>++<div id="results">++</div></body></html>diff --git a/static/index.js b/static/index.jsnew file mode 100644index 0000000..6d0426b--- /dev/null+++ b/static/index.js@@ -0,0 +1,42 @@+++window.onload = (event) => {+ const searchInput = document.getElementById('search');++ searchInput.addEventListener('keyup', (e) => {+ console.log(searchInput.value);++ const encodedValue = encodeURIComponent(searchInput.value);+ fetch('/search?s=' + encodedValue).then(response => {+ clearResults();+ console.log(response);+ response.json().then(content => {+ console.log(content);+ content.forEach(element => {+ addResult(element.title, element.url);+ })+ });+ });+ });+};+++function clearResults() {+ const results = document.getElementById('results');+ results.innerHTML = '';+}+++function addResult(title, url) {+ const par = document.createElement("p");++ const link = document.createElement("a");+ const linkText = document.createTextNode(title);+ link.appendChild(linkText);+ link.href = url;++ par.appendChild(link);++ const results = document.getElementById('results');+ results.appendChild(par);+}\ No newline at end of filediff --git a/static/search.html b/static/search.htmlnew file mode 100644index 0000000..42dc8b8--- /dev/null+++ b/static/search.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<html lang="en">+<head>+ <meta charset="UTF-8">+ <title>Search results</title>+</head>+<body>++<div id="remote">+ <form action="/search">+ <input class="typeahead" type="search" placeholder="Search" name="s">+ <!--<input type="search" name="s" />-->+ </form>+</div>+++</body>+</html>\ No newline at end of file