-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: eodag.utils.product_types.search_product_types usage
- Loading branch information
Showing
3 changed files
with
103 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2024, CS GROUP - France, https://www.csgroup.eu/ | ||
# | ||
# This file is part of EODAG project | ||
# https://www.github.com/CS-SI/EODAG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, cast | ||
|
||
from whoosh.qparser import QueryParser | ||
from whoosh.searching import Results | ||
|
||
from eodag.utils import Unpack | ||
|
||
if TYPE_CHECKING: | ||
from typing import List, Optional | ||
|
||
from whoosh.index import Index | ||
|
||
from eodag.types import ProductTypeProperties | ||
|
||
|
||
def search_product_types( | ||
index: Index, **search_kwargs: Unpack[ProductTypeProperties] | ||
) -> List[str]: | ||
"""Find product types using given index and search args | ||
:param index: Input nested dictionnary | ||
:type index: :class:`~whoosh.index.Index` | ||
:param search_kwargs: whoosh-compatibles search arguments | ||
:type search_kwargs: Unpack[ProductTypeProperties] | ||
:returns: found product types ids | ||
:rtype: Set[str] | ||
""" | ||
|
||
with index.searcher() as searcher: | ||
results: Optional[Results] = None | ||
# For each search key, do a guess and then upgrade the result (i.e. when | ||
# merging results, if a hit appears in both results, its position is raised | ||
# to the top. This way, the top most result will be the hit that best | ||
# matches the given queries. Put another way, this best guess is the one | ||
# that crosses the highest number of search params from the given queries | ||
for search_key, search_value in search_kwargs.items(): | ||
result = cast( | ||
Results, | ||
searcher.search( | ||
QueryParser(search_key, index.schema).parse(search_value), | ||
limit=None, | ||
), | ||
) | ||
if not results: | ||
results = result | ||
else: | ||
results.upgrade_and_extend(result) | ||
if not results: | ||
return [] | ||
|
||
return [r["ID"] for r in results or []] |