@@ -103,7 +103,6 @@ def check_commands(
103103 """
104104 if path .nest :
105105 part_nest = ""
106-
107106 for index , path_part in enumerate (path .parts ):
108107
109108 # Create nested dictionaries if not present for merge operations
@@ -126,20 +125,21 @@ def check_commands(
126125
127126 part_nest += f"['{ path_part } ']"
128127
129- if path .index or from_path or op in ["remove" , "replace" , "test" ]:
130- commands .add (
131- f"if (!ctx._source{ path .es_nest } .containsKey('{ path .key } '))"
132- f"{{Debug.explain('{ path .key } does not exist in { path .nest } ');}}"
133- )
128+ if from_path or op in ["remove" , "replace" , "test" ]:
134129
135- if from_path and path .index is not None :
136- commands .add (
137- f"if ((ctx._source{ path .es_location } instanceof ArrayList"
138- f" && ctx._source{ path .es_location } .size() < { abs (path .index )} )"
139- f" || (!(ctx._source{ path .es_location } instanceof ArrayList)"
140- f" && !ctx._source{ path .es_location } .containsKey('{ path .index } ')))"
141- f"{{Debug.explain('{ path .es_location } does not exist');}}"
142- )
130+ if isinstance (path .key , int ):
131+ commands .add (
132+ f"if ((ctx._source{ path .es_nest } instanceof ArrayList"
133+ f" && ctx._source{ path .es_nest } .size() < { abs (path .key )} )"
134+ f" || (!(ctx._source{ path .es_nest } instanceof ArrayList)"
135+ f" && !ctx._source{ path .es_nest } .containsKey('{ path .key } ')))"
136+ f"{{Debug.explain('{ path .key } does not exist in { path .nest } ');}}"
137+ )
138+ else :
139+ commands .add (
140+ f"if (!ctx._source{ path .es_nest } .containsKey('{ path .key } '))"
141+ f"{{Debug.explain('{ path .key } does not exist in { path .nest } ');}}"
142+ )
143143
144144
145145def remove_commands (commands : ESCommandSet , path : ElasticPath ) -> None :
@@ -150,15 +150,15 @@ def remove_commands(commands: ESCommandSet, path: ElasticPath) -> None:
150150 path (ElasticPath): Path to value to be removed
151151
152152 """
153- if path .index is not None :
153+ if isinstance ( path .key , int ) :
154154 commands .add (
155- f"def { path .variable_name } = ctx._source{ path .es_location } .remove({ path .es_index } );"
155+ f"if ((ctx._source{ path .es_nest } instanceof ArrayList"
156+ f"{{def { path .variable_name } = ctx._source{ path .es_nest } .remove({ path .es_key } );}} else "
156157 )
157158
158- else :
159- commands .add (
160- f"def { path .variable_name } = ctx._source{ path .es_nest } .remove('{ path .key } ');"
161- )
159+ commands .add (
160+ f"def { path .variable_name } = ctx._source{ path .es_nest } .remove('{ path .key } ');"
161+ )
162162
163163
164164def add_commands (
@@ -180,21 +180,21 @@ def add_commands(
180180 value = (
181181 from_path .variable_name
182182 if operation .op == "move"
183- else f"ctx._source{ from_path .es_location } "
183+ else f"ctx._source{ from_path .es_path } "
184184 )
185+
185186 else :
186187 value = f"params.{ path .param_key } "
187188 params [path .param_key ] = operation .value
188189
189- if path .index is not None :
190+ if isinstance ( path .key , int ) :
190191 commands .add (
191- f"if (ctx._source{ path .es_location } instanceof ArrayList)"
192- f"{{ctx._source{ path .es_location } .{ 'add' if operation .op in ['add' , 'move' ] else 'set' } ({ path .es_index } , { value } )}}"
193- f"else{{ctx._source { path . es_location } [' { path . index } '] = { value } }} "
192+ f"if (ctx._source{ path .es_nest } instanceof ArrayList)"
193+ f"{{ctx._source{ path .es_nest } .{ 'add' if operation .op in ['add' , 'move' ] else 'set' } ({ path .es_key } , { value } )}}"
194+ f" else "
194195 )
195196
196- else :
197- commands .add (f"ctx._source{ path .es_location } = { value } ;" )
197+ commands .add (f"ctx._source{ path .es_location } = { value } ;" )
198198
199199
200200def test_commands (
@@ -210,10 +210,19 @@ def test_commands(
210210 value = f"params.{ path .param_key } "
211211 params [path .param_key ] = operation .value
212212
213+ if isinstance (path .key , int ):
214+ commands .add (
215+ f"if (ctx._source{ path .es_nest } instanceof ArrayList)"
216+ f"{{if (ctx._source{ path .es_nest } [{ path .es_key } ] != { value } )"
217+ f"{{Debug.explain('Test failed `{ path .es_path } `"
218+ f" != ' + ctx._source{ path .es_path } );}}"
219+ f"}} else "
220+ )
221+
213222 commands .add (
214- f"if (ctx._source{ path .es_location } != { value } )"
215- f"{{Debug.explain('Test failed `{ path .location } `"
216- f" != ' + ctx._source{ path .es_location } );}}"
223+ f"if (ctx._source{ path .es_path } != { value } )"
224+ f"{{Debug.explain('Test failed `{ path .es_path } `"
225+ f" != ' + ctx._source{ path .es_path } );}}"
217226 )
218227
219228
0 commit comments