heymenn commited on
Commit
c320b8e
·
1 Parent(s): 924632b

fix ops error

Browse files
Files changed (1) hide show
  1. ops.py +26 -5
ops.py CHANGED
@@ -212,14 +212,35 @@ def _fulltext_paragraphs(data: dict, section: str) -> Optional[str]:
212
 
213
  # ------------------------------- Public API --------------------------------
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  async def ops_search(client: AsyncClient, query: str, n_results: int) -> list[dict]:
216
  """Keyword search against OPS published-data, normalized to SERP dicts."""
217
  n_results = max(1, min(n_results, 100)) # OPS caps a page at 100.
218
- data = await _ops_get(
219
- client,
220
- "/published-data/search/biblio",
221
- params={"q": query, "Range": f"1-{n_results}"},
222
- )
 
 
 
 
 
 
223
  search = data.get("ops:world-patent-data", {}).get("ops:biblio-search", {})
224
  result = search.get("ops:search-result", {})
225
  results: list[dict] = []
 
212
 
213
  # ------------------------------- Public API --------------------------------
214
 
215
+ def _to_cql(query: str) -> str:
216
+ """Turn a free-text query into a CQL expression OPS accepts.
217
+
218
+ OPS only auto-wraps a single bare word; multi-word queries like
219
+ 'agentic ai' are invalid CQL and 404. Queries already containing a
220
+ relational operator ('=') are assumed to be hand-written CQL and passed
221
+ through unchanged.
222
+ """
223
+ q = query.strip()
224
+ if not q or "=" in q:
225
+ return q
226
+ escaped = q.replace('"', " ").strip()
227
+ return f'txt all "{escaped}"'
228
+
229
+
230
  async def ops_search(client: AsyncClient, query: str, n_results: int) -> list[dict]:
231
  """Keyword search against OPS published-data, normalized to SERP dicts."""
232
  n_results = max(1, min(n_results, 100)) # OPS caps a page at 100.
233
+ try:
234
+ data = await _ops_get(
235
+ client,
236
+ "/published-data/search/biblio",
237
+ params={"q": _to_cql(query), "Range": f"1-{n_results}"},
238
+ )
239
+ except HTTPStatusError as e:
240
+ # OPS returns 404 (SERVER.EntityNotFound) when a search has no hits.
241
+ if e.response.status_code == 404:
242
+ return []
243
+ raise
244
  search = data.get("ops:world-patent-data", {}).get("ops:biblio-search", {})
245
  result = search.get("ops:search-result", {})
246
  results: list[dict] = []