Skip to content

Commit 4454b7e

Browse files
committed
free text scratch
1 parent 2b96ad8 commit 4454b7e

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,18 @@ async def landing_page(self, **kwargs) -> stac_types.LandingPage:
225225
return landing_page
226226

227227
async def all_collections(
228-
self, fields: Optional[List[str]] = None, sortby: Optional[str] = None, **kwargs
228+
self,
229+
fields: Optional[List[str]] = None,
230+
sortby: Optional[str] = None,
231+
q: Optional[List[str]] = None,
232+
**kwargs,
229233
) -> stac_types.Collections:
230234
"""Read all collections from the database.
231235
232236
Args:
233237
fields (Optional[List[str]]): Fields to include or exclude from the results.
234238
sortby (Optional[str]): Sorting options for the results.
239+
q (Optional[List[str]]): Free text search terms.
235240
**kwargs: Keyword arguments from the request.
236241
237242
Returns:
@@ -267,7 +272,7 @@ async def all_collections(
267272
sort = parsed_sort
268273

269274
collections, next_token = await self.database.get_all_collections(
270-
token=token, limit=limit, request=request, sort=sort
275+
token=token, limit=limit, request=request, sort=sort, q=q
271276
)
272277

273278
# Apply field filtering if fields parameter was provided

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ async def get_all_collections(
175175
limit: int,
176176
request: Request,
177177
sort: Optional[List[Dict[str, Any]]] = None,
178+
q: Optional[List[str]] = None,
178179
) -> Tuple[List[Dict[str, Any]], Optional[str]]:
179180
"""Retrieve a list of collections from Elasticsearch, supporting pagination.
180181
@@ -183,6 +184,7 @@ async def get_all_collections(
183184
limit (int): The number of results to return.
184185
request (Request): The FastAPI request object.
185186
sort (Optional[List[Dict[str, Any]]]): Optional sort parameter from the request.
187+
q (Optional[List[str]]): Free text search terms.
186188
187189
Returns:
188190
A tuple of (collections, next pagination token if any).
@@ -223,6 +225,38 @@ async def get_all_collections(
223225
if token:
224226
body["search_after"] = [token]
225227

228+
# Apply free text query if provided
229+
if q:
230+
# For collections, we want to search across all relevant fields
231+
should_clauses = []
232+
233+
# For each search term
234+
for term in q:
235+
# Create a multi_match query for each term
236+
for field in [
237+
"id",
238+
"title",
239+
"description",
240+
"keywords",
241+
"summaries.platform",
242+
"summaries.constellation",
243+
"providers.name",
244+
"providers.url",
245+
]:
246+
should_clauses.append(
247+
{
248+
"wildcard": {
249+
field: {"value": f"*{term}*", "case_insensitive": True}
250+
}
251+
}
252+
)
253+
254+
# Add the query to the body using bool query with should clauses
255+
body["query"] = {
256+
"bool": {"should": should_clauses, "minimum_should_match": 1}
257+
}
258+
259+
# Execute the search
226260
response = await self.client.search(
227261
index=COLLECTIONS_INDEX,
228262
body=body,

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ async def get_all_collections(
159159
limit: int,
160160
request: Request,
161161
sort: Optional[List[Dict[str, Any]]] = None,
162+
q: Optional[List[str]] = None,
162163
) -> Tuple[List[Dict[str, Any]], Optional[str]]:
163164
"""Retrieve a list of collections from Elasticsearch, supporting pagination.
164165
@@ -167,6 +168,7 @@ async def get_all_collections(
167168
limit (int): The number of results to return.
168169
request (Request): The FastAPI request object.
169170
sort (Optional[List[Dict[str, Any]]]): Optional sort parameter from the request.
171+
q (Optional[List[str]]): Free text search terms.
170172
171173
Returns:
172174
A tuple of (collections, next pagination token if any).
@@ -207,6 +209,37 @@ async def get_all_collections(
207209
if token:
208210
body["search_after"] = [token]
209211

212+
# Apply free text query if provided
213+
if q:
214+
# For collections, we want to search across all relevant fields
215+
should_clauses = []
216+
217+
# For each search term
218+
for term in q:
219+
# Create a multi_match query for each term
220+
for field in [
221+
"id",
222+
"title",
223+
"description",
224+
"keywords",
225+
"summaries.platform",
226+
"summaries.constellation",
227+
"providers.name",
228+
"providers.url",
229+
]:
230+
should_clauses.append(
231+
{
232+
"wildcard": {
233+
field: {"value": f"*{term}*", "case_insensitive": True}
234+
}
235+
}
236+
)
237+
238+
# Add the query to the body using bool query with should clauses
239+
body["query"] = {
240+
"bool": {"should": should_clauses, "minimum_should_match": 1}
241+
}
242+
210243
response = await self.client.search(
211244
index=COLLECTIONS_INDEX,
212245
body=body,

0 commit comments

Comments
 (0)