@@ -32,9 +32,7 @@ def __init__(
32
32
):
33
33
super ().__init__ (target , stream_name , schema , key_properties )
34
34
self .client = self ._authenticated_client ()
35
- self .index_schema_fields = self .config .get ("index_schema_fields" , {}).get (
36
- self .stream_name , {}
37
- )
35
+ self .index_schema_fields = self .config .get ("index_schema_fields" , {}).get (self .stream_name , {})
38
36
self .metadata_fields = self .config .get ("metadata_fields" , {}).get (self .stream_name , {})
39
37
self .index_mappings = self .config .get ("index_mappings" , {}).get (self .stream_name , {})
40
38
self .index_name = None
@@ -99,9 +97,7 @@ def _build_fields(
99
97
for k , v in mapping .items ():
100
98
match = jsonpath_ng .parse (v ).find (record )
101
99
if len (match ) == 0 :
102
- self .logger .warning (
103
- f"schema key { k } with json path { v } could not be found in record: { record } "
104
- )
100
+ self .logger .warning (f"schema key { k } with json path { v } could not be found in record: { record } " )
105
101
schemas [k ] = v
106
102
else :
107
103
if len (match ) > 1 :
@@ -154,23 +150,24 @@ def create_index(self, index: str) -> None:
154
150
mappings = {
155
151
key : value ["mapping" ][key ]["type" ]
156
152
for key , value in self .client .indices .get_field_mapping (
157
- index = index , fields = self .index_mappings .keys ()
158
- )["mappings" ].items ()
153
+ index = index , fields = list ( self .index_mappings .keys () )
154
+ )[index ][ "mappings" ].items ()
159
155
}
160
- if not all (self .index_mappings [key ] == value for key , value in mappings ):
161
- self .logger .warning (
162
- f"Index { index } already exists with different mappings. Recreate index with new mappings."
163
- )
164
- elif mappings .keys () != self .index_mappings .keys ():
165
- self .logger .info (
166
- f"Index { index } exists but with different fields. Updating mapping for existing index."
167
- )
168
- self .client .indices .put_mapping (index = index , body = self .index_mappings )
156
+ if not all (self .index_mappings [key ]["type" ] == value for key , value in mappings .items ()):
157
+ try :
158
+ self .client .indices .put_mapping (index = index , body = {"properties" : self .index_mappings })
159
+ except elasticsearch .exceptions .BadRequestError as e :
160
+ if e .message == "illegal_argument_exception" :
161
+ self .logger .warning (
162
+ f"Failed to update mapping for index { index } : { e } , recreate index to apply new mappings."
163
+ )
164
+ else :
165
+ raise e
169
166
else :
170
167
self .logger .debug (f"Index { index } already exists, skipping creation." )
171
168
else :
172
169
self .logger .info (f"Creating index { index } with mappings: { self .index_mappings } " )
173
- self .client .indices .create (index = index , mappings = self .index_mappings )
170
+ self .client .indices .create (index = index , mappings = { "properties" : self .index_mappings } )
174
171
175
172
def _authenticated_client (self ) -> elasticsearch .Elasticsearch :
176
173
"""Generate a newly authenticated Elasticsearch client.
@@ -212,9 +209,7 @@ def process_batch(self, context: dict[str, Any]) -> None:
212
209
Args:
213
210
context: Dictionary containing batch processing context including records.
214
211
"""
215
- updated_records , distinct_indices = self .build_request_body_and_distinct_indices (
216
- context ["records" ]
217
- )
212
+ updated_records , distinct_indices = self .build_request_body_and_distinct_indices (context ["records" ])
218
213
for index in distinct_indices :
219
214
self .create_index (index )
220
215
try :
0 commit comments