Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ async def all_collections( # noqa: C901
**kwargs,
)

# NOTE: `FreeTextExtension` - pgstac will only accept `str` so we need to
# join the list[str] with ` OR `
# ref: https://github.com/stac-utils/stac-fastapi-pgstac/pull/263
if q := clean_args.pop("q", None):
clean_args["q"] = " OR ".join(q) if isinstance(q, list) else q

async with request.app.state.get_connection(request, "r") as conn:
q, p = render(
"""
Expand Down Expand Up @@ -260,9 +254,11 @@ async def _search_base( # noqa: C901
search_request.conf = search_request.conf or {}
search_request.conf["nohydrate"] = settings.use_api_hydrate

search_request_json = search_request.model_dump_json(
exclude_none=True, by_alias=True
search_request_json = search_request.model_dump(
exclude_none=True, by_alias=True, mode="json"
)
search_request_json = self._clean_search_args({}, **search_request_json)
search_request_json = orjson.dumps(search_request_json)

try:
async with request.app.state.get_connection(request, "r") as conn:
Expand Down Expand Up @@ -613,10 +609,14 @@ def _clean_search_args( # noqa: C901
else:
includes.add(field)

base_args["fields"] = {"include": includes, "exclude": excludes}
# ensure sets are converted to list to avoid JSON serialization issues
base_args["fields"] = {"include": list(includes), "exclude": list(excludes)}

if q:
base_args["q"] = q
# NOTE: `FreeTextExtension` - pgstac will only accept `str` so we need to
# join the list[str] with ` OR `
# ref: https://github.com/stac-utils/stac-fastapi-pgstac/pull/263
base_args["q"] = " OR ".join(q) if isinstance(q, list) else q

# Remove None values from dict
clean = {}
Expand Down
71 changes: 68 additions & 3 deletions tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,22 +1701,87 @@ async def test_item_search_freetext(app_client, load_test_data, load_test_collec
f"/collections/{test_item['collection']}/items", json=test_item
)
assert resp.status_code == 201
test_item2 = load_test_data("test_item2.json")
resp = await app_client.post(
f"/collections/{test_item['collection']}/items", json=test_item2
)
assert resp.status_code == 201

# free-text
resp = await app_client.get(
# free-text basic (using POST)
resp = await app_client.post(
"/search",
params={"q": "orthorectified"},
json={"q": ["orthorectified"]},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["features"][0]["id"] == "test-item"

resp = await app_client.post(
"/search",
json={"q": ["orthorectified", "yo"]},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["features"][0]["id"] == "test-item"

resp = await app_client.post(
"/search",
json={"q": ["orthorectified", "Reflectance"]},
)
assert resp.json()["numberReturned"] == 2
assert resp.json()["features"][0]["id"] == "test-item"
assert resp.json()["features"][1]["id"] == "test-item2"

# free-text advanced (using POST)
resp = await app_client.post(
"/search",
json={"q": "orthorectified OR yo"},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["features"][0]["id"] == "test-item"

resp = await app_client.post(
"/search",
json={"q": "orthorectified OR Reflectance"},
)
assert resp.json()["numberReturned"] == 2
assert resp.json()["features"][0]["id"] == "test-item"
assert resp.json()["features"][1]["id"] == "test-item2"

resp = await app_client.post(
"/search",
json={"q": "orthorectified AND Reflectance"},
)
assert resp.json()["numberReturned"] == 0

# free-text basic (using GET)
resp = await app_client.get(
"/search",
params={"q": "yo"},
)
assert resp.json()["numberReturned"] == 0

resp = await app_client.get(
"/search",
params={"q": "orthorectified,yo"},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["features"][0]["id"] == "test-item"

# free-text advanced (using GET)
resp = await app_client.get(
"/search",
params={"q": "orthorectified"},
)
assert resp.json()["numberReturned"] == 1
assert resp.json()["features"][0]["id"] == "test-item"

resp = await app_client.get(
"/search",
params={"q": "orthorectified OR Reflectance"},
)
assert resp.json()["numberReturned"] == 2
assert resp.json()["features"][0]["id"] == "test-item"
assert resp.json()["features"][1]["id"] == "test-item2"

resp = await app_client.get(
"/search",
params={"q": "yo"},
Expand Down
Loading