argos/dmp-backend/elastic/src/main/java/eu/eudat/elastic/repository/ElasticRepository.java

53 lines
1.8 KiB
Java
Raw Normal View History

2018-07-11 15:47:36 +02:00
package eu.eudat.elastic.repository;
2023-11-14 11:24:14 +01:00
import co.elastic.clients.elasticsearch.ElasticsearchClient;
2018-07-11 15:47:36 +02:00
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.elastic.criteria.Criteria;
import eu.eudat.elastic.entities.ElasticEntity;
2020-04-06 17:34:25 +02:00
import org.elasticsearch.client.RequestOptions;
2018-07-11 15:47:36 +02:00
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2023-11-09 10:29:42 +01:00
import org.springframework.core.env.Environment;
2018-07-11 15:47:36 +02:00
import java.io.IOException;
/**
* Created by ikalyvas on 7/5/2018.
*/
public abstract class ElasticRepository<T extends ElasticEntity,C extends Criteria> implements Repository<T,C> {
private static final Logger logger = LoggerFactory.getLogger(ElasticRepository.class);
2023-11-14 11:24:14 +01:00
private ElasticsearchClient client;
2018-07-11 15:47:36 +02:00
2023-11-14 11:24:14 +01:00
public ElasticsearchClient getClient() {
2018-07-11 15:47:36 +02:00
return client;
}
2023-11-14 11:24:14 +01:00
public ElasticRepository(ElasticsearchClient client, Environment environment) {
2020-04-06 17:34:25 +02:00
try {
2023-11-14 11:24:14 +01:00
if (!Boolean.TRUE.equals(environment.getProperty("elastic.enabled", boolean.class))){
2023-11-09 10:29:42 +01:00
logger.warn("Unable to connect to Elastic Services");
this.client = null;
return;
}
2023-11-14 11:24:14 +01:00
if (client.ping().value()) {
2020-04-06 17:34:25 +02:00
this.client = client;
}
} catch (IOException e) {
logger.warn("Unable to connect to Elastic Services");
2022-11-25 15:03:59 +01:00
logger.error(e.getMessage(), e);
2020-04-06 17:34:25 +02:00
this.client = null;
}
2018-07-11 15:47:36 +02:00
}
public <T> T transformFromString(String value, Class<T> tClass) {
ObjectMapper mapper = new ObjectMapper();
T item = null;
try {
item = mapper.readValue(value, tClass);
} catch (IOException e) {
logger.error(e.getMessage(), e);
2018-07-11 15:47:36 +02:00
}
return item;
}
}