44such as converting bounding boxes to polygon representations.
55"""
66
7- import re
87from typing import Any , Dict , List , Optional , Set , Union
98
10- from stac_fastapi .core .models .patch import ElasticPath
9+ from stac_fastapi .core .models .patch import ElasticPath , ESCommandSet
1110from stac_fastapi .types .stac import (
1211 Item ,
1312 PatchAddReplaceTest ,
@@ -173,7 +172,7 @@ def merge_to_operations(data: Dict) -> List:
173172
174173
175174def check_commands (
176- commands : List [ str ] ,
175+ commands : ESCommandSet ,
177176 op : str ,
178177 path : ElasticPath ,
179178 from_path : bool = False ,
@@ -188,28 +187,28 @@ def check_commands(
188187
189188 """
190189 if path .nest :
191- commands .append (
190+ commands .add (
192191 f"if (!ctx._source.containsKey('{ path .nest } '))"
193192 f"{{Debug.explain('{ path .nest } does not exist');}}"
194193 )
195194
196195 if path .index or op in ["remove" , "replace" , "test" ] or from_path :
197- commands .append (
198- f"if (!ctx._source.{ path .nest } .containsKey('{ path .key } '))"
196+ commands .add (
197+ f"if (!ctx._source.{ path .es_nest } .containsKey('{ path .key } '))"
199198 f"{{Debug.explain('{ path .key } does not exist in { path .nest } ');}}"
200199 )
201200
202201 if from_path and path .index is not None :
203- commands .append (
204- f"if ((ctx._source.{ path .location } instanceof ArrayList"
205- f" && ctx._source.{ path .location } .size() < { path .index } )"
202+ commands .add (
203+ f"if ((ctx._source.{ path .es_location } instanceof ArrayList"
204+ f" && ctx._source.{ path .es_location } .size() < { path .index } )"
206205 f" || (!(ctx._source.properties.hello instanceof ArrayList)"
207- f" && !ctx._source.{ path .location } .containsKey('{ path .index } ')))"
206+ f" && !ctx._source.{ path .es_location } .containsKey('{ path .index } ')))"
208207 f"{{Debug.explain('{ path .path } does not exist');}}"
209208 )
210209
211210
212- def remove_commands (commands : List [ str ] , path : ElasticPath ) -> None :
211+ def remove_commands (commands : ESCommandSet , path : ElasticPath ) -> None :
213212 """Remove value at path.
214213
215214 Args:
@@ -218,14 +217,18 @@ def remove_commands(commands: List[str], path: ElasticPath) -> None:
218217
219218 """
220219 if path .index is not None :
221- commands .append (f"def temp = ctx._source.{ path .location } .remove({ path .index } );" )
220+ commands .add (
221+ f"def { path .variable_name } = ctx._source.{ path .es_location } .remove({ path .index } );"
222+ )
222223
223224 else :
224- commands .append (f"def temp = ctx._source.{ path .nest } .remove('{ path .key } ');" )
225+ commands .add (
226+ f"def { path .variable_name } = ctx._source.{ path .es_nest } .remove('{ path .key } ');"
227+ )
225228
226229
227230def add_commands (
228- commands : List [ str ] ,
231+ commands : ESCommandSet ,
229232 operation : PatchOperation ,
230233 path : ElasticPath ,
231234 from_path : ElasticPath ,
@@ -239,23 +242,27 @@ def add_commands(
239242
240243 """
241244 if from_path is not None :
242- value = "temp" if operation .op == "move" else f"ctx._source.{ from_path .path } "
245+ value = (
246+ from_path .variable_name
247+ if operation .op == "move"
248+ else f"ctx._source.{ from_path .es_path } "
249+ )
243250 else :
244251 value = operation .json_value
245252
246253 if path .index is not None :
247- commands .append (
248- f"if (ctx._source.{ path .location } instanceof ArrayList)"
249- f"{{ctx._source.{ path .location } .{ 'add' if operation .op in ['add' , 'move' ] else 'set' } ({ path .index } , { value } )}}"
250- f"else{{ctx._source.{ path .path } = { value } }}"
254+ commands .add (
255+ f"if (ctx._source.{ path .es_location } instanceof ArrayList)"
256+ f"{{ctx._source.{ path .es_location } .{ 'add' if operation .op in ['add' , 'move' ] else 'set' } ({ path .index } , { value } )}}"
257+ f"else{{ctx._source.{ path .es_path } = { value } }}"
251258 )
252259
253260 else :
254- commands .append (f"ctx._source.{ path .path } = { value } ;" )
261+ commands .add (f"ctx._source.{ path .es_path } = { value } ;" )
255262
256263
257264def test_commands (
258- commands : List [ str ] , operation : PatchOperation , path : ElasticPath
265+ commands : ESCommandSet , operation : PatchOperation , path : ElasticPath
259266) -> None :
260267 """Test value at path.
261268
@@ -264,10 +271,10 @@ def test_commands(
264271 operation (PatchOperation): operation to run
265272 path (ElasticPath): path for value to be tested
266273 """
267- commands .append (
268- f"if (ctx._source.{ path .location } != { operation .json_value } )"
269- f"{{Debug.explain('Test failed for: { path .path } | "
270- f"{ operation .json_value } != ' + ctx._source.{ path .location } );}}"
274+ commands .add (
275+ f"if (ctx._source.{ path .es_path } != { operation .json_value } )"
276+ f"{{Debug.explain('Test failed ` { path .path } ` | "
277+ f"{ operation .json_value } != ' + ctx._source.{ path .es_path } );}}"
271278 )
272279
273280
@@ -282,18 +289,13 @@ def commands_to_source(commands: List[str]) -> str:
282289 """
283290 seen : Set [str ] = set ()
284291 seen_add = seen .add
285- regex = re .compile (r"([^.' ]*:[^.' ]*)[. ]" )
292+ # regex = re.compile(r"([^.' ]*:[^.' ]*)[. ; ]")
286293 source = ""
287294
288295 # filter duplicate lines
289296 for command in commands :
290297 if command not in seen :
291298 seen_add (command )
292- # extension terms with using `:` must be swapped out
293- if matches := regex .findall (command ):
294- for match in matches :
295- command = command .replace (f".{ match } " , f"['{ match } ']" )
296-
297299 source += command
298300
299301 return source
@@ -308,7 +310,7 @@ def operations_to_script(operations: List) -> Dict:
308310 Returns:
309311 Dict: elasticsearch update script.
310312 """
311- commands : List = []
313+ commands : ESCommandSet = ESCommandSet ()
312314 for operation in operations :
313315 path = ElasticPath (path = operation .path )
314316 from_path = (
@@ -333,7 +335,7 @@ def operations_to_script(operations: List) -> Dict:
333335 if operation .op == "test" :
334336 test_commands (commands = commands , operation = operation , path = path )
335337
336- source = commands_to_source ( commands = commands )
338+ source = "" . join ( commands )
337339
338340 return {
339341 "source" : source ,
0 commit comments