Skip to content

Commit 7ab88d4

Browse files
committed
es filter methods to sfeos helpers
1 parent 67e1c4c commit 7ab88d4

File tree

3 files changed

+47
-46
lines changed

3 files changed

+47
-46
lines changed

stac_fastapi/core/stac_fastapi/core/extensions/filter.py

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Filter extension logic for es conversion."""
1+
"""Filter extension logic for conversion."""
22

33
# """
44
# Implements Filter Extension.
@@ -62,8 +62,8 @@
6262
},
6363
}
6464

65-
_cql2_like_patterns = re.compile(r"\\.|[%_]|\\$")
66-
_valid_like_substitutions = {
65+
cql2_like_patterns = re.compile(r"\\.|[%_]|\\$")
66+
valid_like_substitutions = {
6767
"\\\\": "\\",
6868
"\\%": "%",
6969
"\\_": "_",
@@ -72,33 +72,6 @@
7272
}
7373

7474

75-
def _replace_like_patterns(match: re.Match) -> str:
76-
pattern = match.group()
77-
try:
78-
return _valid_like_substitutions[pattern]
79-
except KeyError:
80-
raise ValueError(f"'{pattern}' is not a valid escape sequence")
81-
82-
83-
def cql2_like_to_es(string: str) -> str:
84-
"""
85-
Convert CQL2 "LIKE" characters to Elasticsearch "wildcard" characters.
86-
87-
Args:
88-
string (str): The string containing CQL2 wildcard characters.
89-
90-
Returns:
91-
str: The converted string with Elasticsearch compatible wildcards.
92-
93-
Raises:
94-
ValueError: If an invalid escape sequence is encountered.
95-
"""
96-
return _cql2_like_patterns.sub(
97-
repl=_replace_like_patterns,
98-
string=string,
99-
)
100-
101-
10275
class LogicalOp(str, Enum):
10376
"""Enumeration for logical operators used in constructing Elasticsearch queries."""
10477

@@ -134,16 +107,3 @@ class SpatialOp(str, Enum):
134107
S_CONTAINS = "s_contains"
135108
S_WITHIN = "s_within"
136109
S_DISJOINT = "s_disjoint"
137-
138-
139-
def to_es_field(queryables_mapping: Dict[str, Any], field: str) -> str:
140-
"""
141-
Map a given field to its corresponding Elasticsearch field according to a predefined mapping.
142-
143-
Args:
144-
field (str): The field name from a user query or filter.
145-
146-
Returns:
147-
str: The mapped field name suitable for Elasticsearch queries.
148-
"""
149-
return queryables_mapping.get(field, field)

stac_fastapi/sfeos_helpers/stac_fastapi/sfeos_helpers/filter.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Shared filter extension methods for stac-fastapi elasticsearch and opensearch backends."""
22

3+
import re
34
from collections import deque
45
from typing import Any, Dict, Optional
56

@@ -12,14 +13,54 @@
1213
ComparisonOp,
1314
LogicalOp,
1415
SpatialOp,
15-
cql2_like_to_es,
16-
to_es_field,
16+
cql2_like_patterns,
17+
valid_like_substitutions,
1718
)
1819
from stac_fastapi.extensions.core.filter.client import AsyncBaseFiltersClient
1920

2021
from .mappings import ES_MAPPING_TYPE_TO_JSON
2122

2223

24+
def _replace_like_patterns(match: re.Match) -> str:
25+
pattern = match.group()
26+
try:
27+
return valid_like_substitutions[pattern]
28+
except KeyError:
29+
raise ValueError(f"'{pattern}' is not a valid escape sequence")
30+
31+
32+
def cql2_like_to_es(string: str) -> str:
33+
"""
34+
Convert CQL2 "LIKE" characters to Elasticsearch "wildcard" characters.
35+
36+
Args:
37+
string (str): The string containing CQL2 wildcard characters.
38+
39+
Returns:
40+
str: The converted string with Elasticsearch compatible wildcards.
41+
42+
Raises:
43+
ValueError: If an invalid escape sequence is encountered.
44+
"""
45+
return cql2_like_patterns.sub(
46+
repl=_replace_like_patterns,
47+
string=string,
48+
)
49+
50+
51+
def to_es_field(queryables_mapping: Dict[str, Any], field: str) -> str:
52+
"""
53+
Map a given field to its corresponding Elasticsearch field according to a predefined mapping.
54+
55+
Args:
56+
field (str): The field name from a user query or filter.
57+
58+
Returns:
59+
str: The mapped field name suitable for Elasticsearch queries.
60+
"""
61+
return queryables_mapping.get(field, field)
62+
63+
2364
def to_es(queryables_mapping: Dict[str, Any], query: Dict[str, Any]) -> Dict[str, Any]:
2465
"""
2566
Transform a simplified CQL2 query structure to an Elasticsearch compatible query DSL.

stac_fastapi/tests/extensions/test_cql2_like_to_es.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from stac_fastapi.core.extensions.filter import cql2_like_to_es
3+
from stac_fastapi.sfeos_helpers.filter import cql2_like_to_es
44

55

66
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)