Skip to content

Commit a8fb0ef

Browse files
authored
Merge branch 'main' into get-filter-extension
2 parents 4206893 + 5d71684 commit a8fb0ef

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
- Collection-level Assets to the CollectionSerializer [#148](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/148)
1313
- Examples folder with example docker setup for running sfes from pip [#147](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/147)
1414
- GET /search filter extension queries [#163](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/163)
15+
- Added support for GET /search intersection queries [#158](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/158)
1516

1617
### Changed
18+
19+
- Updated core stac-fastapi libraries to 2.4.8 from 2.4.3 [#151](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/151)
20+
- Use aliases on Elasticsearch indices, add number suffix in index name. [#152](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/152)
21+
1722
### Fixed
1823

1924
- Corrected the closing of client connections in ES index management functions [#132](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/132)

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ async def get_search(
335335
base_args["datetime"] = datetime
336336

337337
if intersects:
338-
base_args["intersects"] = intersects
338+
base_args["intersects"] = json.loads(unquote_plus(intersects))
339339

340340
if sortby:
341341
sort_param = []

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
":",
4040
}
4141

42-
DEFAULT_INDICES = f"*,-*kibana*,-{COLLECTIONS_INDEX}"
42+
ITEM_INDICES = f"{ITEMS_INDEX_PREFIX}*,-*kibana*,-{COLLECTIONS_INDEX}*"
4343

4444
DEFAULT_SORT = {
4545
"properties.datetime": {"order": "desc"},
@@ -164,7 +164,7 @@ def indices(collection_ids: Optional[List[str]]) -> str:
164164
A string of comma-separated index names. If `collection_ids` is None, returns the default indices.
165165
"""
166166
if collection_ids is None:
167-
return DEFAULT_INDICES
167+
return ITEM_INDICES
168168
else:
169169
return ",".join([index_by_collection_id(c) for c in collection_ids])
170170

@@ -178,7 +178,8 @@ async def create_collection_index() -> None:
178178
client = AsyncElasticsearchSettings().create_client
179179

180180
await client.indices.create(
181-
index=COLLECTIONS_INDEX,
181+
index=f"{COLLECTIONS_INDEX}-000001",
182+
aliases={COLLECTIONS_INDEX: {}},
182183
mappings=ES_COLLECTIONS_MAPPINGS,
183184
ignore=400, # ignore 400 already exists code
184185
)
@@ -197,9 +198,11 @@ async def create_item_index(collection_id: str):
197198
198199
"""
199200
client = AsyncElasticsearchSettings().create_client
201+
index_name = index_by_collection_id(collection_id)
200202

201203
await client.indices.create(
202-
index=index_by_collection_id(collection_id),
204+
index=f"{index_by_collection_id(collection_id)}-000001",
205+
aliases={index_name: {}},
203206
mappings=ES_ITEMS_MAPPINGS,
204207
settings=ES_ITEMS_SETTINGS,
205208
ignore=400, # ignore 400 already exists code
@@ -215,7 +218,14 @@ async def delete_item_index(collection_id: str):
215218
"""
216219
client = AsyncElasticsearchSettings().create_client
217220

218-
await client.indices.delete(index=index_by_collection_id(collection_id))
221+
name = index_by_collection_id(collection_id)
222+
resolved = await client.indices.resolve_index(name=name)
223+
if "aliases" in resolved and resolved["aliases"]:
224+
[alias] = resolved["aliases"]
225+
await client.indices.delete_alias(index=alias["indices"], name=alias["name"])
226+
await client.indices.delete(index=alias["indices"])
227+
else:
228+
await client.indices.delete(index=name)
219229
await client.close()
220230

221231

@@ -773,14 +783,11 @@ async def bulk_async(
773783
`mk_actions` function is called to generate a list of actions for the bulk insert. If `refresh` is set to True, the
774784
index is refreshed after the bulk insert. The function does not return any value.
775785
"""
776-
await asyncio.get_event_loop().run_in_executor(
777-
None,
778-
lambda: helpers.bulk(
779-
self.sync_client,
780-
mk_actions(collection_id, processed_items),
781-
refresh=refresh,
782-
raise_on_error=False,
783-
),
786+
await helpers.async_bulk(
787+
self.client,
788+
mk_actions(collection_id, processed_items),
789+
refresh=refresh,
790+
raise_on_error=False,
784791
)
785792

786793
def bulk_sync(
@@ -811,7 +818,7 @@ def bulk_sync(
811818
async def delete_items(self) -> None:
812819
"""Danger. this is only for tests."""
813820
await self.client.delete_by_query(
814-
index=DEFAULT_INDICES,
821+
index=ITEM_INDICES,
815822
body={"query": {"match_all": {}}},
816823
wait_for_completion=True,
817824
)

stac_fastapi/elasticsearch/tests/api/test_api.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import uuid
33
from datetime import datetime, timedelta
44

5+
import pytest
6+
57
from ..conftest import create_collection, create_item
68

79
ROUTES = {
@@ -233,7 +235,29 @@ async def test_search_invalid_date(app_client, ctx):
233235
assert resp.status_code == 400
234236

235237

236-
async def test_search_point_intersects(app_client, ctx):
238+
@pytest.mark.asyncio
239+
async def test_search_point_intersects_get(app_client, ctx):
240+
resp = await app_client.get(
241+
'/search?intersects={"type":"Point","coordinates":[150.04,-33.14]}'
242+
)
243+
244+
assert resp.status_code == 200
245+
resp_json = resp.json()
246+
assert len(resp_json["features"]) == 1
247+
248+
249+
@pytest.mark.asyncio
250+
async def test_search_polygon_intersects_get(app_client, ctx):
251+
resp = await app_client.get(
252+
'/search?intersects={"type":"Polygon","coordinates":[[[149.04, -34.14],[149.04, -32.14],[151.04, -32.14],[151.04, -34.14],[149.04, -34.14]]]}'
253+
)
254+
255+
assert resp.status_code == 200
256+
resp_json = resp.json()
257+
assert len(resp_json["features"]) == 1
258+
259+
260+
async def test_search_point_intersects_post(app_client, ctx):
237261
point = [150.04, -33.14]
238262
intersects = {"type": "Point", "coordinates": point}
239263

0 commit comments

Comments
 (0)