Skip to content

Commit cb80f50

Browse files
committed
add support for json query
1 parent 563127d commit cb80f50

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

openlibrary/fastapi/search.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import json
34
from typing import Annotated
45

56
from fastapi import APIRouter, Depends, Query, Request
@@ -33,28 +34,31 @@ async def search_json(
3334
spellcheck_count: int | None = Query(
3435
3, description="The number of spellcheck suggestions."
3536
),
37+
query: str | None = Query(None, description="JSON query to search for"),
3638
):
3739
"""
3840
Performs a search for documents based on the provided query.
3941
"""
42+
if query:
43+
final_query = json.loads(query)
44+
else:
45+
final_query = {"q": q, "page": page, "limit": limit}
4046

4147
# Call the underlying search logic
4248
_fields = WorkSearchScheme.default_fetched_fields
4349
if fields:
4450
_fields = fields.split(',') # type: ignore
4551

46-
query = {"q": q, "page": page, "limit": limit}
47-
48-
query.update(
52+
final_query.update(
4953
{key: value for key, value in facets.dict().items() if value is not None}
5054
)
5155

52-
query.update(
56+
final_query.update(
5357
{key: value for key, value in all_fields.dict().items() if value is not None}
5458
)
5559

5660
response = await work_search_async(
57-
query,
61+
query=final_query,
5862
sort=sort,
5963
page=page,
6064
offset=offset,

0 commit comments

Comments
 (0)