mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-08 22:22:17 +02:00
fix(web.discover): cap results to top_k
This commit is contained in:
parent
43893fc1f8
commit
87df0bb629
2 changed files with 23 additions and 1 deletions
|
|
@ -27,7 +27,9 @@ def build_discover_executor(
|
||||||
"(set a SearXNG host or a Linkup/Baidu key)."
|
"(set a SearXNG host or a Linkup/Baidu key)."
|
||||||
)
|
)
|
||||||
hits = await provider.search(payload.query, payload.top_k)
|
hits = await provider.search(payload.query, payload.top_k)
|
||||||
return DiscoverOutput(hits=hits)
|
# Enforce the verb's documented cap here, once, for every provider:
|
||||||
|
# some backends (e.g. SearXNG) treat `top_k` as a hint and over-return.
|
||||||
|
return DiscoverOutput(hits=hits[: payload.top_k])
|
||||||
|
|
||||||
return execute
|
return execute
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -77,3 +77,23 @@ async def test_self_disables_when_no_provider_is_configured():
|
||||||
|
|
||||||
with pytest.raises(NoDiscoverProviderError):
|
with pytest.raises(NoDiscoverProviderError):
|
||||||
await execute(DiscoverInput(query="q"))
|
await execute(DiscoverInput(query="q"))
|
||||||
|
|
||||||
|
|
||||||
|
async def test_caps_hits_to_top_k_when_provider_over_returns():
|
||||||
|
# SearXNG treats `limit` as a hint and can return more rows than asked; the
|
||||||
|
# verb must honor its own documented `top_k` cap regardless of the provider.
|
||||||
|
over = _FakeProvider(
|
||||||
|
"searxng",
|
||||||
|
available=True,
|
||||||
|
hits=[_hit(f"https://{i}.com", "searxng") for i in range(5)],
|
||||||
|
)
|
||||||
|
execute = build_discover_executor(providers=[over])
|
||||||
|
|
||||||
|
out = await execute(DiscoverInput(query="q", top_k=3))
|
||||||
|
|
||||||
|
assert len(out.hits) == 3
|
||||||
|
assert [h.url for h in out.hits] == [
|
||||||
|
"https://0.com",
|
||||||
|
"https://1.com",
|
||||||
|
"https://2.com",
|
||||||
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue