def test_overwrite_script(): ubq = UpdateByQuery() ubq = ubq.script( source="ctx._source.likes += params.f", lang="painless", params={"f": 3} ) assert { "script": { "source": "ctx._source.likes += params.f", "lang": "painless", "params": {"f": 3}, } } == ubq.to_dict() ubq = ubq.script(source="ctx._source.likes++") assert {"script": {"source": "ctx._source.likes++"}} == ubq.to_dict()
def test_overwrite_script(): ubq = UpdateByQuery() ubq = ubq.script(source='ctx._source.likes += params.f', lang='painless', params={'f': 3}) assert { 'script': { 'source': 'ctx._source.likes += params.f', 'lang': 'painless', 'params': { 'f': 3 } } } == ubq.to_dict() ubq = ubq.script(source='ctx._source.likes++') assert {'script': {'source': 'ctx._source.likes++'}} == ubq.to_dict()
async def update(self, filter_obj: IFilter, to_update: dict) -> int: """ Update object in Storage by Query. :param IFilter filter_obj: filter object which describes what objects need to update :param dict to_update: dictionary with fields and values which should be updated :return: number of updated entries """ def dict_to_source(__to_update: dict) -> str: """ Convert __to_update dict into elasticsearch source representation. :param __to_update: dictionary with fields and values which should be updated :return: elasticsearch inline representation """ def _value_converter(_value: Any) -> Any: """ Convert value if it is necessary :param _value: :return: """ if isinstance(_value, bool): return str(_value).lower() elif isinstance(_value, datetime): return _value.strftime(self._default_date_format) return _value return " ".join((self._inline_templates.get( type(value), self._default_template).substitute( FIELD_NAME=key, FIELD_VALUE=_value_converter(value)) for key, value in __to_update.items())) def _update(_ubq) -> UpdateByQueryResponse: """ Perform Update by Query in separate thread. :param UpdateByQuery _ubq: UpdateByQuery instance :return: Response object of ElasticSearch DSL module """ return _ubq.execute() _to_update = to_update.copy() # Copy because it will be change below # NOTE: Important: call of the parent update method changes _to_update dict! await super().update(filter_obj, _to_update) # Call the generic code ubq = UpdateByQuery(index=self._index, using=self._es_client) filter_by = self._query_converter.build(filter_obj) ubq = ubq.query(filter_by) source = dict_to_source(_to_update) ubq = ubq.script(source=source, lang=ESWords.PAINLESS) result = await self._loop.run_in_executor(self._tread_pool_exec, _update, ubq) await self._refresh_index( ) # ensure that updated results will be ready immediately return result.updated
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 test_overwrite_script(): ubq = UpdateByQuery() ubq = ubq.script(source='ctx._source.likes += params.f', lang='painless', params={'f': 3}) assert { 'script': { 'source': 'ctx._source.likes += params.f', 'lang': 'painless', 'params': { 'f': 3 } } } == ubq.to_dict() ubq = ubq.script(source='ctx._source.likes++') assert { 'script': { 'source': 'ctx._source.likes++' } } == ubq.to_dict()
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()