@@ -91,20 +91,20 @@ class SpatialOp(str, Enum):
9191 S_DISJOINT = "s_disjoint"
9292
9393
94- queryables_mapping = {
95- "id" : "id" ,
96- "collection" : "collection" ,
97- "geometry" : "geometry" ,
98- "datetime" : "properties.datetime" ,
99- "created" : "properties.created" ,
100- "updated" : "properties.updated" ,
101- "cloud_cover" : "properties.eo:cloud_cover" ,
102- "cloud_shadow_percentage" : "properties.s2:cloud_shadow_percentage" ,
103- "nodata_pixel_percentage" : "properties.s2:nodata_pixel_percentage" ,
104- }
105-
106-
107- def to_es_field (field : str ) -> str :
94+ # queryables_mapping = {
95+ # "id": "id",
96+ # "collection": "collection",
97+ # "geometry": "geometry",
98+ # "datetime": "properties.datetime",
99+ # "created": "properties.created",
100+ # "updated": "properties.updated",
101+ # "cloud_cover": "properties.eo:cloud_cover",
102+ # "cloud_shadow_percentage": "properties.s2:cloud_shadow_percentage",
103+ # "nodata_pixel_percentage": "properties.s2:nodata_pixel_percentage",
104+ # }
105+
106+
107+ def to_es_field (queryables_mapping : Dict [ str , Any ], field : str ) -> str :
108108 """
109109 Map a given field to its corresponding Elasticsearch field according to a predefined mapping.
110110
@@ -117,7 +117,7 @@ def to_es_field(field: str) -> str:
117117 return queryables_mapping .get (field , field )
118118
119119
120- def to_es (query : Dict [str , Any ]) -> Dict [str , Any ]:
120+ def to_es (queryables_mapping : Dict [ str , Any ], query : Dict [str , Any ]) -> Dict [str , Any ]:
121121 """
122122 Transform a simplified CQL2 query structure to an Elasticsearch compatible query DSL.
123123
@@ -133,7 +133,13 @@ def to_es(query: Dict[str, Any]) -> Dict[str, Any]:
133133 LogicalOp .OR : "should" ,
134134 LogicalOp .NOT : "must_not" ,
135135 }[query ["op" ]]
136- return {"bool" : {bool_type : [to_es (sub_query ) for sub_query in query ["args" ]]}}
136+ return {
137+ "bool" : {
138+ bool_type : [
139+ to_es (queryables_mapping , sub_query ) for sub_query in query ["args" ]
140+ ]
141+ }
142+ }
137143
138144 elif query ["op" ] in [
139145 ComparisonOp .EQ ,
@@ -150,7 +156,7 @@ def to_es(query: Dict[str, Any]) -> Dict[str, Any]:
150156 ComparisonOp .GTE : "gte" ,
151157 }
152158
153- field = to_es_field (query ["args" ][0 ]["property" ])
159+ field = to_es_field (queryables_mapping , query ["args" ][0 ]["property" ])
154160 value = query ["args" ][1 ]
155161 if isinstance (value , dict ) and "timestamp" in value :
156162 value = value ["timestamp" ]
@@ -173,11 +179,11 @@ def to_es(query: Dict[str, Any]) -> Dict[str, Any]:
173179 return {"range" : {field : {range_op [query ["op" ]]: value }}}
174180
175181 elif query ["op" ] == ComparisonOp .IS_NULL :
176- field = to_es_field (query ["args" ][0 ]["property" ])
182+ field = to_es_field (queryables_mapping , query ["args" ][0 ]["property" ])
177183 return {"bool" : {"must_not" : {"exists" : {"field" : field }}}}
178184
179185 elif query ["op" ] == AdvancedComparisonOp .BETWEEN :
180- field = to_es_field (query ["args" ][0 ]["property" ])
186+ field = to_es_field (queryables_mapping , query ["args" ][0 ]["property" ])
181187 gte , lte = query ["args" ][1 ], query ["args" ][2 ]
182188 if isinstance (gte , dict ) and "timestamp" in gte :
183189 gte = gte ["timestamp" ]
@@ -186,14 +192,14 @@ def to_es(query: Dict[str, Any]) -> Dict[str, Any]:
186192 return {"range" : {field : {"gte" : gte , "lte" : lte }}}
187193
188194 elif query ["op" ] == AdvancedComparisonOp .IN :
189- field = to_es_field (query ["args" ][0 ]["property" ])
195+ field = to_es_field (queryables_mapping , query ["args" ][0 ]["property" ])
190196 values = query ["args" ][1 ]
191197 if not isinstance (values , list ):
192198 raise ValueError (f"Arg { values } is not a list" )
193199 return {"terms" : {field : values }}
194200
195201 elif query ["op" ] == AdvancedComparisonOp .LIKE :
196- field = to_es_field (query ["args" ][0 ]["property" ])
202+ field = to_es_field (queryables_mapping , query ["args" ][0 ]["property" ])
197203 pattern = cql2_like_to_es (query ["args" ][1 ])
198204 return {"wildcard" : {field : {"value" : pattern , "case_insensitive" : True }}}
199205
@@ -203,7 +209,7 @@ def to_es(query: Dict[str, Any]) -> Dict[str, Any]:
203209 SpatialOp .S_WITHIN ,
204210 SpatialOp .S_DISJOINT ,
205211 ]:
206- field = to_es_field (query ["args" ][0 ]["property" ])
212+ field = to_es_field (queryables_mapping , query ["args" ][0 ]["property" ])
207213 geometry = query ["args" ][1 ]
208214
209215 relation_mapping = {
0 commit comments