Loading src/main/java/es/redmic/elasticsearchlib/common/repository/RBaseESRepository.java +0 −5 Original line number Diff line number Diff line Loading @@ -684,11 +684,6 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte return (W) ElasticSearchUtils.parseMGetHit(result, wrapperType); } @SuppressWarnings("unchecked") protected Map<String, Object> convertTModelToSource(TModel modelToIndex) { return objectMapper.convertValue(modelToIndex, Map.class); } public String[] getIndex() { return INDEX; } Loading src/main/java/es/redmic/elasticsearchlib/common/utils/ElasticPersistenceUtils.java +101 −0 Original line number Diff line number Diff line Loading @@ -3,32 +3,133 @@ package es.redmic.elasticsearchlib.common.utils; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.WriteRequest.RefreshPolicy; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.ObjectMapper; import es.redmic.elasticsearchlib.config.EsClientProvider; import es.redmic.exception.common.ExceptionType; import es.redmic.exception.elasticsearch.ESUpdateException; import es.redmic.models.es.common.dto.EventApplicationResult; import es.redmic.models.es.common.model.BaseES; @Component public class ElasticPersistenceUtils<TModel extends BaseES<?>> { protected static Logger logger = LogManager.getLogger(); @Autowired EsClientProvider ESProvider; @Autowired protected ObjectMapper objectMapper; protected static String SCRIPT_ENGINE = "groovy"; public EventApplicationResult save(String index, String type, TModel model, String id) { // @formatter:off IndexResponse result = ESProvider.getClient() .prepareIndex(index, type) .setSource(convertTModelToSource(model)) .setId(id) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .actionGet(); // @formatter:on if (!result.status().equals(RestStatus.CREATED)) { logger.debug("Error indexando en " + index + " " + type); return new EventApplicationResult(ExceptionType.ES_INDEX_DOCUMENT.toString()); } return new EventApplicationResult(true); } public EventApplicationResult update(String index, String type, TModel model, String id) { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE); updateRequest.index(index); updateRequest.type(type); updateRequest.id(id); updateRequest.doc(convertTModelToSource(model)); updateRequest.fetchSource(true); try { ESProvider.getClient().update(updateRequest).get(); } catch (InterruptedException | ExecutionException e) { logger.debug("Error modificando el item con id " + id + " en " + index + " " + type); return new EventApplicationResult(ExceptionType.ES_UPDATE_DOCUMENT.toString()); } return new EventApplicationResult(true); } public EventApplicationResult update(String index, String type, String id, XContentBuilder doc) { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE); updateRequest.index(index); updateRequest.type(type); updateRequest.id(id); updateRequest.doc(doc); updateRequest.fetchSource(true); try { ESProvider.getClient().update(updateRequest).get(); } catch (Exception e) { logger.debug("Error modificando el item con id " + id + " en " + index + " " + type); return new EventApplicationResult(ExceptionType.ES_UPDATE_DOCUMENT.toString()); } return new EventApplicationResult(true); } public EventApplicationResult delete(String index, String type, String id) { // @formatter:off DeleteResponse result = ESProvider.getClient() .prepareDelete(index, type, id) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .actionGet(); // @formatter:on if (!result.status().equals(RestStatus.OK)) { logger.debug("Error borrando el item con id " + id + " en " + index + " " + type); return new EventApplicationResult(ExceptionType.DELETE_ITEM_EXCEPTION.toString()); } return new EventApplicationResult(true); } @SuppressWarnings("unchecked") protected Map<String, Object> convertTModelToSource(TModel modelToIndex) { return objectMapper.convertValue(modelToIndex, Map.class); } public List<UpdateRequest> getUpdateRequest(String[] index, String[] type, String id, Map<String, Object> fields) { return getUpdateRequest(index, type, id, fields, null, null); Loading src/main/java/es/redmic/elasticsearchlib/data/repository/RWDataESRepository.java +12 −75 Original line number Diff line number Diff line package es.redmic.elasticsearchlib.data.repository; import java.util.concurrent.ExecutionException; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.WriteRequest.RefreshPolicy; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; import org.springframework.beans.factory.annotation.Autowired; import es.redmic.elasticsearchlib.common.repository.IRWBaseESRepository; import es.redmic.elasticsearchlib.common.utils.ElasticPersistenceUtils; import es.redmic.exception.common.ExceptionType; import es.redmic.models.es.common.dto.EventApplicationResult; import es.redmic.models.es.common.model.BaseES; import es.redmic.models.es.common.query.dto.SimpleQueryDTO; public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO extends SimpleQueryDTO> extends RDataESRepository<TModel, TQueryDTO> { extends RDataESRepository<TModel, TQueryDTO> implements IRWBaseESRepository<TModel> { @Autowired ElasticPersistenceUtils<TModel> elasticPersistenceUtils; Loading @@ -30,6 +23,7 @@ public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO ext super(index, type); } @Override public EventApplicationResult save(TModel modelToIndex) { EventApplicationResult checkInsert = checkInsertConstraintsFulfilled(modelToIndex); Loading @@ -38,26 +32,11 @@ public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO ext return checkInsert; } // @formatter:off IndexResponse result = ESProvider.getClient() .prepareIndex(getIndex()[0], getType()[0]) .setSource(convertTModelToSource(modelToIndex)) .setId((modelToIndex.getId() != null) ? modelToIndex.getId().toString() : null) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .actionGet(); // @formatter:on if (!result.status().equals(RestStatus.CREATED)) { LOGGER.debug("Error indexando en " + getIndex()[0] + " " + getType()[0]); return new EventApplicationResult(ExceptionType.ES_INDEX_DOCUMENT.toString()); } return new EventApplicationResult(true); return elasticPersistenceUtils.save(getIndex()[0], getType()[0], modelToIndex, (modelToIndex.getId() != null) ? modelToIndex.getId().toString() : null); } @Override public EventApplicationResult update(TModel modelToIndex) { EventApplicationResult checkUpdate = checkUpdateConstraintsFulfilled(modelToIndex); Loading @@ -66,45 +45,17 @@ public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO ext return checkUpdate; } UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE); updateRequest.index(getIndex()[0]); updateRequest.type(getType()[0]); updateRequest.id(modelToIndex.getId().toString()); updateRequest.doc(convertTModelToSource(modelToIndex)); updateRequest.fetchSource(true); try { ESProvider.getClient().update(updateRequest).get(); } catch (InterruptedException | ExecutionException e) { LOGGER.debug("Error modificando el item con id " + modelToIndex.getId() + " en " + getIndex()[0] + " " + getType()[0]); return new EventApplicationResult(ExceptionType.ES_UPDATE_DOCUMENT.toString()); } return new EventApplicationResult(true); return elasticPersistenceUtils.update(getIndex()[0], getType()[0], modelToIndex, modelToIndex.getId().toString()); } @Override public EventApplicationResult update(String id, XContentBuilder doc) { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE); updateRequest.index(getIndex()[0]); updateRequest.type(getType()[0]); updateRequest.id(id); updateRequest.doc(doc); updateRequest.fetchSource(true); try { ESProvider.getClient().update(updateRequest).get(); } catch (Exception e) { LOGGER.debug("Error modificando el item con id " + id + " en " + getIndex()[0] + " " + getType()[0]); return new EventApplicationResult(ExceptionType.ES_UPDATE_DOCUMENT.toString()); } return new EventApplicationResult(true); return elasticPersistenceUtils.update(getIndex()[0], getType()[0], id, doc); } @Override public EventApplicationResult delete(String id) { EventApplicationResult checkDelete = checkDeleteConstraintsFulfilled(id); Loading @@ -113,21 +64,7 @@ public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO ext return checkDelete; } // @formatter:off DeleteResponse result = ESProvider.getClient() .prepareDelete(getIndex()[0], getType()[0], id) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .actionGet(); // @formatter:on if (!result.status().equals(RestStatus.OK)) { LOGGER.debug("Error borrando el item con id " + id + " en " + getIndex()[0] + " " + getType()[0]); return new EventApplicationResult(ExceptionType.DELETE_ITEM_EXCEPTION.toString()); } return new EventApplicationResult(true); return elasticPersistenceUtils.delete(getIndex()[0], getType()[0], id); } /* Loading Loading
src/main/java/es/redmic/elasticsearchlib/common/repository/RBaseESRepository.java +0 −5 Original line number Diff line number Diff line Loading @@ -684,11 +684,6 @@ public abstract class RBaseESRepository<TModel extends BaseES<?>, TQueryDTO exte return (W) ElasticSearchUtils.parseMGetHit(result, wrapperType); } @SuppressWarnings("unchecked") protected Map<String, Object> convertTModelToSource(TModel modelToIndex) { return objectMapper.convertValue(modelToIndex, Map.class); } public String[] getIndex() { return INDEX; } Loading
src/main/java/es/redmic/elasticsearchlib/common/utils/ElasticPersistenceUtils.java +101 −0 Original line number Diff line number Diff line Loading @@ -3,32 +3,133 @@ package es.redmic.elasticsearchlib.common.utils; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.WriteRequest.RefreshPolicy; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.ObjectMapper; import es.redmic.elasticsearchlib.config.EsClientProvider; import es.redmic.exception.common.ExceptionType; import es.redmic.exception.elasticsearch.ESUpdateException; import es.redmic.models.es.common.dto.EventApplicationResult; import es.redmic.models.es.common.model.BaseES; @Component public class ElasticPersistenceUtils<TModel extends BaseES<?>> { protected static Logger logger = LogManager.getLogger(); @Autowired EsClientProvider ESProvider; @Autowired protected ObjectMapper objectMapper; protected static String SCRIPT_ENGINE = "groovy"; public EventApplicationResult save(String index, String type, TModel model, String id) { // @formatter:off IndexResponse result = ESProvider.getClient() .prepareIndex(index, type) .setSource(convertTModelToSource(model)) .setId(id) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .actionGet(); // @formatter:on if (!result.status().equals(RestStatus.CREATED)) { logger.debug("Error indexando en " + index + " " + type); return new EventApplicationResult(ExceptionType.ES_INDEX_DOCUMENT.toString()); } return new EventApplicationResult(true); } public EventApplicationResult update(String index, String type, TModel model, String id) { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE); updateRequest.index(index); updateRequest.type(type); updateRequest.id(id); updateRequest.doc(convertTModelToSource(model)); updateRequest.fetchSource(true); try { ESProvider.getClient().update(updateRequest).get(); } catch (InterruptedException | ExecutionException e) { logger.debug("Error modificando el item con id " + id + " en " + index + " " + type); return new EventApplicationResult(ExceptionType.ES_UPDATE_DOCUMENT.toString()); } return new EventApplicationResult(true); } public EventApplicationResult update(String index, String type, String id, XContentBuilder doc) { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE); updateRequest.index(index); updateRequest.type(type); updateRequest.id(id); updateRequest.doc(doc); updateRequest.fetchSource(true); try { ESProvider.getClient().update(updateRequest).get(); } catch (Exception e) { logger.debug("Error modificando el item con id " + id + " en " + index + " " + type); return new EventApplicationResult(ExceptionType.ES_UPDATE_DOCUMENT.toString()); } return new EventApplicationResult(true); } public EventApplicationResult delete(String index, String type, String id) { // @formatter:off DeleteResponse result = ESProvider.getClient() .prepareDelete(index, type, id) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .actionGet(); // @formatter:on if (!result.status().equals(RestStatus.OK)) { logger.debug("Error borrando el item con id " + id + " en " + index + " " + type); return new EventApplicationResult(ExceptionType.DELETE_ITEM_EXCEPTION.toString()); } return new EventApplicationResult(true); } @SuppressWarnings("unchecked") protected Map<String, Object> convertTModelToSource(TModel modelToIndex) { return objectMapper.convertValue(modelToIndex, Map.class); } public List<UpdateRequest> getUpdateRequest(String[] index, String[] type, String id, Map<String, Object> fields) { return getUpdateRequest(index, type, id, fields, null, null); Loading
src/main/java/es/redmic/elasticsearchlib/data/repository/RWDataESRepository.java +12 −75 Original line number Diff line number Diff line package es.redmic.elasticsearchlib.data.repository; import java.util.concurrent.ExecutionException; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.WriteRequest.RefreshPolicy; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; import org.springframework.beans.factory.annotation.Autowired; import es.redmic.elasticsearchlib.common.repository.IRWBaseESRepository; import es.redmic.elasticsearchlib.common.utils.ElasticPersistenceUtils; import es.redmic.exception.common.ExceptionType; import es.redmic.models.es.common.dto.EventApplicationResult; import es.redmic.models.es.common.model.BaseES; import es.redmic.models.es.common.query.dto.SimpleQueryDTO; public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO extends SimpleQueryDTO> extends RDataESRepository<TModel, TQueryDTO> { extends RDataESRepository<TModel, TQueryDTO> implements IRWBaseESRepository<TModel> { @Autowired ElasticPersistenceUtils<TModel> elasticPersistenceUtils; Loading @@ -30,6 +23,7 @@ public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO ext super(index, type); } @Override public EventApplicationResult save(TModel modelToIndex) { EventApplicationResult checkInsert = checkInsertConstraintsFulfilled(modelToIndex); Loading @@ -38,26 +32,11 @@ public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO ext return checkInsert; } // @formatter:off IndexResponse result = ESProvider.getClient() .prepareIndex(getIndex()[0], getType()[0]) .setSource(convertTModelToSource(modelToIndex)) .setId((modelToIndex.getId() != null) ? modelToIndex.getId().toString() : null) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .actionGet(); // @formatter:on if (!result.status().equals(RestStatus.CREATED)) { LOGGER.debug("Error indexando en " + getIndex()[0] + " " + getType()[0]); return new EventApplicationResult(ExceptionType.ES_INDEX_DOCUMENT.toString()); } return new EventApplicationResult(true); return elasticPersistenceUtils.save(getIndex()[0], getType()[0], modelToIndex, (modelToIndex.getId() != null) ? modelToIndex.getId().toString() : null); } @Override public EventApplicationResult update(TModel modelToIndex) { EventApplicationResult checkUpdate = checkUpdateConstraintsFulfilled(modelToIndex); Loading @@ -66,45 +45,17 @@ public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO ext return checkUpdate; } UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE); updateRequest.index(getIndex()[0]); updateRequest.type(getType()[0]); updateRequest.id(modelToIndex.getId().toString()); updateRequest.doc(convertTModelToSource(modelToIndex)); updateRequest.fetchSource(true); try { ESProvider.getClient().update(updateRequest).get(); } catch (InterruptedException | ExecutionException e) { LOGGER.debug("Error modificando el item con id " + modelToIndex.getId() + " en " + getIndex()[0] + " " + getType()[0]); return new EventApplicationResult(ExceptionType.ES_UPDATE_DOCUMENT.toString()); } return new EventApplicationResult(true); return elasticPersistenceUtils.update(getIndex()[0], getType()[0], modelToIndex, modelToIndex.getId().toString()); } @Override public EventApplicationResult update(String id, XContentBuilder doc) { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.setRefreshPolicy(RefreshPolicy.IMMEDIATE); updateRequest.index(getIndex()[0]); updateRequest.type(getType()[0]); updateRequest.id(id); updateRequest.doc(doc); updateRequest.fetchSource(true); try { ESProvider.getClient().update(updateRequest).get(); } catch (Exception e) { LOGGER.debug("Error modificando el item con id " + id + " en " + getIndex()[0] + " " + getType()[0]); return new EventApplicationResult(ExceptionType.ES_UPDATE_DOCUMENT.toString()); } return new EventApplicationResult(true); return elasticPersistenceUtils.update(getIndex()[0], getType()[0], id, doc); } @Override public EventApplicationResult delete(String id) { EventApplicationResult checkDelete = checkDeleteConstraintsFulfilled(id); Loading @@ -113,21 +64,7 @@ public abstract class RWDataESRepository<TModel extends BaseES<?>, TQueryDTO ext return checkDelete; } // @formatter:off DeleteResponse result = ESProvider.getClient() .prepareDelete(getIndex()[0], getType()[0], id) .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .execute() .actionGet(); // @formatter:on if (!result.status().equals(RestStatus.OK)) { LOGGER.debug("Error borrando el item con id " + id + " en " + getIndex()[0] + " " + getType()[0]); return new EventApplicationResult(ExceptionType.DELETE_ITEM_EXCEPTION.toString()); } return new EventApplicationResult(true); return elasticPersistenceUtils.delete(getIndex()[0], getType()[0], id); } /* Loading