helpers.bulk는 성공 로그를 남기지 않는데 대신 튜블을 반환한다.
success, failed = helpers.bulk(es, actions)
(2, [])
#success=2
#failed = none
첫번째 원소는 성공적으로 처리된 문서의 수.
두번째 원소는 오류가 발생한 문서이다.
근데 실패하면 일단 에러 로그가 생긴다.
발생하는 에러 로그가 너무 길어서 어떤 _id에서 원인이 무엇인지만 뽑아서 출력되게 함
#에러 로그
('1 document(s) failed to index.', [{'update': {'_index': 'my-index', '_id': '1234', 'status': 400, 'error': {'type': 'document_parsing_exception', 'reason': "[1:30] failed to parse field [DATE] of type [date] in document with id '1234'. Preview of field's value: '9999'", 'caused_by': {'type': 'illegal_argument_exception', 'reason': 'failed to parse date field [9999] with format [basic_date]', 'caused_by': {'type': 'date_time_parse_exception', 'reason': "date_time_parse_exception: Text '9999' could not be parsed at index 4"}}}, 'data': {'doc': {'DATE': '9999'}}}}])
json형식의 에러 로그에서 error 부분의(e.error) 정보 출력
def updateIdx(actions):
try:
success, failed =helpers.bulk(es, actions)
logger.info(f"Successfully indexed {success} documents. ")
except Exception as e:
for error_item in e.errors:
if 'update' in error_item and 'error' in error_item['update']:
idx_name= error_item['update']['_index']
error_reason = error_item['update']['error'].get('reason', 'No reason provided')
logger.error(f"An error occurred during bulk operation {idx_name} {error_reason}")