An open source, non-profit web search engine
Improve examples and fix some minor issues
Daoud Clarke85a7d0a6cd9f
diff --git a/mwmbl/tinysearchengine/rank.py b/mwmbl/tinysearchengine/rank.pyindex ce10599..d35aa1c 100644--- a/mwmbl/tinysearchengine/rank.py+++ b/mwmbl/tinysearchengine/rank.py@@ -355,7 +355,6 @@ def get_wiki_results(s: str, max_wiki_results: int) -> list[Document]:response = session.get(WIKI_SEARCH_API_URL.format(query=escaped_query), headers=headers)try:wiki_response = response.json()- print("Wiki response", json.dumps(wiki_response, indent=2))except json.JSONDecodeError:logger.exception(f"Failed to decode JSON response from Wikipedia API for query {s}, status: {response.status_code}, content: {response.content}")return []@@ -413,3 +412,9 @@ class DomainLimitingRanker:filtered_results.append(result)seen_domains.add(domain)return filtered_results++ def complete(self, q: str):+ return self.ranker.complete(q)++ def get_raw_results(self, query: str):+ return self.ranker.get_raw_results(query)diff --git a/mwmbl/tinysearchengine/search.py b/mwmbl/tinysearchengine/search.pyindex 1086c0f..7629282 100644--- a/mwmbl/tinysearchengine/search.py+++ b/mwmbl/tinysearchengine/search.py@@ -112,6 +112,58 @@ class RawSearchResponse(Schema):}+# The complete endpoint returns a two-element list: [query_string, list[str]].+# Ninja cannot express this as a typed Schema, so we describe it via openapi_extra.+_COMPLETE_RESPONSE_SCHEMA = {+ "type": "array",+ "minItems": 2,+ "maxItems": 2,+ "prefixItems": [+ {+ "type": "string",+ "description": "The original partial query string, echoed back.",+ "example": "pyth",+ },+ {+ "type": "array",+ "description": (+ "List of suggestion strings. Each entry is one of:\n"+ "- A completed query term, e.g. `\"python tutorial\"`.\n"+ "- A direct-navigation URL prefixed with `\"go: \"`, "+ "e.g. `\"go: docs.python.org/3/tutorial\"`.\n"+ "- A Google fallback prefixed with `\"search: google.com \"` "+ "when no index results exist."+ ),+ "items": {"type": "string"},+ "example": [+ "go: docs.python.org/3/tutorial",+ "python",+ "python tutorial",+ "python documentation",+ ],+ },+ ],+ "examples": [+ [+ "pyth",+ [+ "go: docs.python.org/3/tutorial",+ "python",+ "python tutorial",+ "python documentation",+ ],+ ],+ [+ "xyzzy lang",+ [+ "search: google.com xyzzy lang",+ "search: google.com xyzzy language",+ ],+ ],+ ],+}++# ---------------------------------------------------------------------------# Route registration# ---------------------------------------------------------------------------@@ -143,49 +195,9 @@ def _register_routes(r: Router | NinjaAPI, ranker: HeuristicRanker):"name": "s","in": "query","required": True,- "description": "The search query string.","schema": {"type": "string", "example": "python tutorial"},}- ],- "responses": {- "200": {- "description": "A list of formatted search results.",- "content": {- "application/json": {- "example": [- {- "url": "https://docs.python.org/3/tutorial/",- "title": [- {"value": "The ", "is_bold": False},- {"value": "Python", "is_bold": True},- {"value": " Tutorial — Python 3 documentation", "is_bold": False},- ],- "extract": [- {"value": "Python", "is_bold": True},- {"value": " is an easy to learn, powerful programming language.", "is_bold": False},- ],- "source": "mwmbl",- },- {- "url": "https://wiki.python.org/moin/BeginnersGuide/Programmers",- "title": [- {"value": "Python", "is_bold": True},- {"value": " for Programmers — ", "is_bold": False},- {"value": "Python", "is_bold": True},- {"value": " Wiki", "is_bold": False},- ],- "extract": [- {"value": "A list of ", "is_bold": False},- {"value": "Python", "is_bold": True},- {"value": " resources for experienced programmers.", "is_bold": False},- ],- "source": "mwmbl",- },- ]- }- },- }- },+ ]},)def search(request, s: str):@@ -214,17 +226,17 @@ def _register_routes(r: Router | NinjaAPI, ranker: HeuristicRanker):"name": "q","in": "query","required": True,- "description": "The partial query string to complete.","schema": {"type": "string", "example": "pyth"},}],"responses": {"200": {"description": (- "A two-element list: the original query string and a list of suggestions."+ "A two-element list: the echoed query string and a list of suggestions."),"content": {"application/json": {+ "schema": _COMPLETE_RESPONSE_SCHEMA,"examples": {"with_index_results": {"summary": "Suggestions when index results exist",@@ -248,11 +260,11 @@ def _register_routes(r: Router | NinjaAPI, ranker: HeuristicRanker):],],},- }+ },}},}- },+ }},)def complete(request, q: str):@@ -284,40 +296,9 @@ def _register_routes(r: Router | NinjaAPI, ranker: HeuristicRanker):"name": "s","in": "query","required": True,- "description": "The search query string.","schema": {"type": "string", "example": "python tutorial"},}- ],- "responses": {- "200": {- "description": "The raw query and unranked index results.",- "content": {- "application/json": {- "example": {- "query": "python tutorial",- "results": [- {- "title": "The Python Tutorial",- "url": "https://docs.python.org/3/tutorial/",- "extract": "Python is an easy to learn, powerful programming language.",- "score": 0.92,- "term": "python tutorial",- "state": None,- },- {- "title": "Python for Beginners",- "url": "https://www.python.org/about/gettingstarted/",- "extract": "Are you new to programming? Python is great for beginners.",- "score": 0.74,- "term": "python",- "state": None,- },- ],- }- }- },- }- },+ ]},)def raw(request, s: str):diff --git a/test/test_evaluation_api.py b/test/test_evaluation_api.pyindex cf57654..0dd5f12 100644--- a/test/test_evaluation_api.py+++ b/test/test_evaluation_api.py@@ -85,6 +85,7 @@ def invalid_wasm_bytes():return b'invalid wasm content'[email protected]("Evaluation API is disabled for now")@pytest.mark.django_dbdef test_submit_valid_wasm_file(client, verified_user, access_token, valid_wasm_bytes):"""Test submitting a valid WASM file"""@@ -113,6 +114,7 @@ def test_submit_valid_wasm_file(client, verified_user, access_token, valid_wasm_assert job.wasm_file == valid_wasm_bytes[email protected]("Evaluation API is disabled for now")@pytest.mark.django_dbdef test_submit_invalid_wasm_file(client, access_token, invalid_wasm_bytes):"""Test submitting an invalid WASM file"""@@ -134,6 +136,7 @@ def test_submit_invalid_wasm_file(client, access_token, invalid_wasm_bytes):assert 'WASM validation failed' in response_data['message'][email protected]("Evaluation API is disabled for now")@pytest.mark.django_dbdef test_submit_non_wasm_file(client, access_token):"""Test submitting a file without .wasm extension"""@@ -155,6 +158,7 @@ def test_submit_non_wasm_file(client, access_token):assert 'File must have .wasm extension' in response_data['message'][email protected]("Evaluation API is disabled for now")@pytest.mark.django_dbdef test_submit_large_file(client, access_token):"""Test submitting a file that exceeds size limit"""@@ -178,6 +182,7 @@ def test_submit_large_file(client, access_token):assert 'File too large' in response_data['message'][email protected]("Evaluation API is disabled for now")@pytest.mark.django_dbdef test_submit_without_authentication(client, valid_wasm_bytes):"""Test submitting without JWT token"""@@ -194,6 +199,7 @@ def test_submit_without_authentication(client, valid_wasm_bytes):assert response.status_code == 401[email protected]("Evaluation API is disabled for now")@pytest.mark.django_dbdef test_submit_with_unverified_email(client, unverified_access_token, valid_wasm_bytes):"""Test submitting with unverified email address"""@@ -215,6 +221,7 @@ def test_submit_with_unverified_email(client, unverified_access_token, valid_wasassert 'Email address is not verified' in response_data['message'][email protected]("Evaluation API is disabled for now")@pytest.mark.django_dbdef test_submit_without_file(client, access_token):"""Test submitting without a file"""@@ -227,6 +234,7 @@ def test_submit_without_file(client, access_token):assert response.status_code == 422 # Validation error[email protected]("Evaluation API is disabled for now")@pytest.mark.django_dbdef test_multiple_submissions_same_user(client, verified_user, access_token, valid_wasm_bytes):"""Test that a user can submit multiple WASM files"""