An open source, non-profit web search engine
Stream intermediate results after each source completes in super search
Daoud Clarke39e221f55a8d
diff --git a/mwmbl/tinysearchengine/super_search.py b/mwmbl/tinysearchengine/super_search.pyindex 53d2b04..be0d3d9 100644--- a/mwmbl/tinysearchengine/super_search.py+++ b/mwmbl/tinysearchengine/super_search.py@@ -294,6 +294,8 @@ async def _run_pipeline(query: str, emit, all_docs: list[Document]) -> None:secondary.append(asyncio.create_task(_follow_links(doc, name, query, emit, all_docs)))+ if all_docs:+ await _emit_final_results(query, all_docs, emit)if secondary:await asyncio.gather(*secondary, return_exceptions=True)@@ -383,7 +385,9 @@ def init_router() -> None:"or JWT bearer token). Quota is 10 requests per user per month.\n\n""Returns `text/event-stream`. Event types: `source_started`, ""`source_returned`, `source_failed`, `result_promoted`, "- "`page_fetched`, `link_followed`, `error`, `done`."+ "`page_fetched`, `link_followed`, `results` (emitted progressively "+ "after each source returns, and once more as the authoritative final "+ "ranking — clients should replace their display on each), `error`, `done`."),openapi_extra={"parameters": [{diff --git a/test/test_super_search.py b/test/test_super_search.pyindex f9706cc..2f88c88 100644--- a/test/test_super_search.py+++ b/test/test_super_search.py@@ -220,11 +220,12 @@ def test_final_results_event_emitted(client, api_key, monkeypatch):assert "results" in event_typesresults_events = [d for t, d in events if t == "results"]- assert len(results_events) == 1- results = results_events[0]["results"]- assert len(results) >= 1- assert results[0]["url"] == "https://py.example/"- # 'results' must appear before 'done'+ assert len(results_events) >= 1+ # The last results event is the authoritative final ranking.+ last_results = results_events[-1]["results"]+ assert len(last_results) >= 1+ assert last_results[0]["url"] == "https://py.example/"+ # At least one 'results' must appear before 'done'assert event_types.index("results") < event_types.index("done")