fix(llm): nest the theme $or under $and with the field filters — langchain-postgres rejects an operator beside field keys at the top level (500 on /search?theme=)
Some checks failed
CI / lint (push) Successful in 42s
CI / notebooks-smoke (push) Successful in 1m52s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
Deploy / llm (push) Has been skipped
Deploy / mc (push) Has been skipped
CI / test (push) Failing after 2m32s
Infra CI / zotero (push) Successful in 22s
Infra CI / notebooks (push) Successful in 1m4s
Deploy / report (push) Has been cancelled
Infra CI / docs (push) Has started running
Infra CI / llm (push) Has been cancelled
Infra CI / mc (push) Has been cancelled
Infra CI / api (push) Has been cancelled

This commit is contained in:
kert
2026-09-24 20:02:15 -04:00
parent edcf15f340
commit 254a7f10d2
2 changed files with 20 additions and 8 deletions

View File

@@ -78,7 +78,13 @@ def _store_filter(filters: dict[str, str]) -> dict[str, Any]:
}
theme = str(filters.get("theme") or "")
if theme:
out["$or"] = [{f"theme_{i}": theme} for i in range(1, 4)]
# langchain-postgres accepts either a mapping of field keys or a
# single operator at the top level, never both — so field
# conditions and the theme $or nest under one $and.
theme_or = {"$or": [{f"theme_{i}": theme} for i in range(1, 4)]}
if out:
return {"$and": [*({k: v} for k, v in out.items()), theme_or]}
return theme_or
return out

View File

@@ -393,10 +393,16 @@ class TestThemeFilter:
assert _theme_ok({}, "")
def test_theme_is_pushed_down_as_an_or_of_rank_keys(self):
out = _store_filter({"theme": "telehealth", "doctype": "manual"})
assert out["doctype"] == "manual"
assert out["$or"] == [
{"theme_1": "telehealth"},
{"theme_2": "telehealth"},
{"theme_3": "telehealth"},
]
theme_or = {
"$or": [
{"theme_1": "telehealth"},
{"theme_2": "telehealth"},
{"theme_3": "telehealth"},
]
}
# field keys and an operator never share the top level (the store
# rejects that): fields and the theme $or nest under one $and
assert _store_filter({"theme": "telehealth", "doctype": "manual"}) == {
"$and": [{"doctype": "manual"}, theme_or]
}
assert _store_filter({"theme": "telehealth"}) == theme_or