def handler(event, context): es_endpoint = os.getenv('ELASTICSEARCH_SERVICE_ENDPOINT') photo_id = event["photo_id"] # Connect to Elasticsearch service try: es = es_client.get_elasticsearch_client(es_endpoint) except Exception: logging.exception('Failed to connect to Elasticsearch cluster') return response(500, { 'error': 'elasticsearch-client-connection', 'message': 'Elasticsearch service is not available' }) try: update = UpdateByQuery(using=es).index(cars_index_name) update = update.filter('term', photoId=photo_id) update = update.script(source='ctx._source.photoId = params.nullPhoto', params={'nullPhoto': None}) update.execute() return response(200, {'result': 'Update seccessfull'}) except Exception: logging.exception('Failed to cenzor photo') return response(500, { 'error': 'car-photo-cenzor-fail', 'message': 'Failed to cenzor requested photo' })
def remove_from_field(doc_type_name, field_name, field_value): """Remove a value from all documents in the doc_type's index.""" doc_type = next(cls for cls in get_doc_types() if cls.__name__ == doc_type_name) script = ( f"if (ctx._source.{field_name}.contains(params.value)) {{" f"ctx._source.{field_name}.remove(ctx._source.{field_name}.indexOf(params.value))" f"}}" ) update = UpdateByQuery(using=es7_client(), index=doc_type._index._name) update = update.filter("term", **{field_name: field_value}) update = update.script(source=script, params={"value": field_value}, conflicts="proceed") # refresh index to ensure search fetches all matches doc_type._index.refresh() update.execute()