209 lines
9.2 KiB
Java
209 lines
9.2 KiB
Java
package eu.dnetlib.ariadneplus.elasticsearch;
|
|
|
|
import eu.dnetlib.ariadneplus.elasticsearch.model.*;
|
|
import eu.dnetlib.ariadneplus.reader.ResourceManager;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.apache.http.HttpHost;
|
|
import org.elasticsearch.action.bulk.BulkRequest;
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
import org.elasticsearch.action.index.IndexRequest;
|
|
import org.elasticsearch.client.RequestOptions;
|
|
import org.elasticsearch.client.RestClient;
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
import org.elasticsearch.common.geo.GeoPoint;
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Service
|
|
public class BulkUpload {
|
|
|
|
private static final Log log = LogFactory.getLog(BulkUpload.class);
|
|
|
|
@Value("${elasticsearch.hostname}")
|
|
private String elasticSearchHostName;
|
|
|
|
@Value("${elasticsearch.indexname}")
|
|
private String elasticSearchIndexName;
|
|
|
|
private RestHighLevelClient client;
|
|
|
|
public void init(String elasticSearchHostName, String elasticSearchIndexName) throws IOException {
|
|
this.elasticSearchIndexName = elasticSearchIndexName;
|
|
client = new RestHighLevelClient(
|
|
RestClient.builder(
|
|
new HttpHost(elasticSearchHostName,9200,"http")));
|
|
|
|
}
|
|
|
|
@PostConstruct
|
|
public void init() throws IOException {
|
|
client = new RestHighLevelClient(
|
|
RestClient.builder(
|
|
new HttpHost(elasticSearchHostName,9200,"http")));
|
|
|
|
}
|
|
|
|
public int index(ResourceManager manager, boolean isCollection) {
|
|
BulkRequest request = new BulkRequest();
|
|
int esResponseCode = 0;
|
|
while (manager.hasNext()){
|
|
|
|
try {
|
|
Object next = manager.next();
|
|
AriadnePlusEntry ace = ((AriadnePlusEntry) next);
|
|
if (isCollection) {
|
|
ace.setResourceType("collection");
|
|
if (ace.getSpatial()==null) {
|
|
ace.setSpatial(Arrays.asList(new Spatial()));
|
|
}
|
|
}
|
|
else {
|
|
ace.setResourceType("dataset");
|
|
if (ace.getSpatial()!=null) {
|
|
ace.getSpatial()
|
|
.stream()
|
|
.filter(s -> Objects.nonNull(s.getLat()) && Objects.nonNull(s.getLon()))
|
|
.forEach(s -> {
|
|
double lat = Double.parseDouble(s.getLat());
|
|
double lon = Double.parseDouble(s.getLon());
|
|
GeoPoint geopoint = new GeoPoint(lat, lon);
|
|
s.setGeopoint(geopoint);
|
|
});
|
|
// TODO update following check according to new model definition
|
|
if (ace.getSpatial().size()==2) {
|
|
Spatial uniqueSpatial = new Spatial();
|
|
boolean uniquePlaceNameFound = ace.getSpatial().stream().filter(s -> s.getPlaceName()!=null).count()==1;
|
|
boolean uniqueLocationFound = ace.getSpatial().stream().filter(s -> s.getGeopoint()!=null).count()==1;
|
|
if (uniquePlaceNameFound&&uniqueLocationFound) {
|
|
ace.getSpatial().stream().filter(s -> s.getPlaceName()!=null).forEach(s -> {
|
|
uniqueSpatial.setPlaceName(s.getPlaceName());
|
|
});
|
|
ace.getSpatial().stream().filter(s -> s.getGeopoint()!=null).forEach(s -> {
|
|
uniqueSpatial.setGeopoint(s.getGeopoint());
|
|
});
|
|
ace.getSpatial().clear();
|
|
ace.setSpatial(Arrays.asList(uniqueSpatial));
|
|
}
|
|
}
|
|
// else {
|
|
// if (ace.getSpatial()!=null) {
|
|
// Set<String> items = new HashSet<>();
|
|
// List<Spatial> spatialsNoDup = ace.getSpatial().stream()
|
|
// .filter(s -> !items.add(s.getPlaceName()))
|
|
// .collect(Collectors.toList());
|
|
// ace.getSpatial().clear();
|
|
// ace.setSpatial(spatialsNoDup);
|
|
// }
|
|
//
|
|
// }
|
|
List<Spatial> dedupSpatials = removeDuplicates(ace.getSpatial());
|
|
ace.getSpatial().clear();
|
|
ace.setSpatial(dedupSpatials);
|
|
|
|
// TODO update following check according to new model definition
|
|
// if (ace.getSpatial().size()>1) {
|
|
// ace.getSpatial().removeIf(s -> (s.getPlaceName()!=null&&s.getPlaceName().equals("Name not provided")&&Objects.isNull(s.getLocation())));
|
|
// }
|
|
}
|
|
else {
|
|
ace.setSpatial(Arrays.asList(new Spatial()));
|
|
}
|
|
}
|
|
|
|
if (ace.getTemporal()!=null) {
|
|
ace.getTemporal().stream()
|
|
.filter(t->t.getMatchingPeriodOName()!=null)
|
|
.forEach(t->{
|
|
t.setPeriodName(t.getMatchingPeriodOName());
|
|
});
|
|
}
|
|
|
|
if (!isCollection) {
|
|
String uniqueIsPartOf = ace.getUniqueIsPartOf();
|
|
if (uniqueIsPartOf != null) {
|
|
ace.setIsPartOf(Arrays.asList(uniqueIsPartOf));
|
|
}
|
|
if (ace.getContributor() != null) {
|
|
ace.getContributor().clear();
|
|
ace.setContributor(ace.getCreator());
|
|
}
|
|
}
|
|
|
|
String[] splits = ace.getIdentifier().split("/");
|
|
|
|
// if (ace.getAatSubjects() != null && ace.getDerivedSubject() != null) {
|
|
// String aatSource = ace.getAatSubjects().get(0).getId();
|
|
// ace.getDerivedSubject().forEach(d -> {
|
|
// d.setSource(aatSource);
|
|
// });
|
|
// String [] aatSourceSplit = aatSource.split("/");
|
|
// String aatSubjectId = aatSourceSplit[aatSourceSplit.length-1];
|
|
// ace.getAatSubjects().forEach(s -> s.setId(aatSubjectId));
|
|
// }
|
|
|
|
String idES = splits[splits.length-1];
|
|
request.add(new IndexRequest(elasticSearchIndexName).id(idES)
|
|
.source(ace.toJson(),XContentType.JSON));
|
|
long start = System.currentTimeMillis();
|
|
BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
|
|
long end = System.currentTimeMillis();
|
|
if (bulkResponse!=null) {
|
|
esResponseCode = bulkResponse.status().getStatus();
|
|
// log.info("Indexing to ES completed with status: " + bulkResponse.status());
|
|
if (bulkResponse.hasFailures()) {
|
|
log.error("FailureMessage: " + bulkResponse.buildFailureMessage());
|
|
esResponseCode = -7;
|
|
}
|
|
}
|
|
else {
|
|
esResponseCode = -3;
|
|
}
|
|
log.debug(idES+" es_index_time(sec): "+(end-start)/1000+" response_code: "+esResponseCode);
|
|
} catch (Throwable t) {
|
|
t.printStackTrace();
|
|
log.error("Indexing "+t.getMessage());
|
|
return -1;
|
|
}
|
|
}
|
|
return esResponseCode;
|
|
}
|
|
|
|
public static List<Spatial> removeDuplicates(List<Spatial> spatialList) {
|
|
Map<String, List<Spatial>> duplicatesMap = getDuplicatesMap(spatialList);
|
|
return duplicatesMap.values().stream()
|
|
.filter(spatials -> spatials!=null)
|
|
.map(spatials -> spatials.get(0))
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
private static Map<String, List<Spatial>> getDuplicatesMap(List<Spatial> spatialList) {
|
|
return spatialList.stream().collect(Collectors.groupingBy(BulkUpload::uniqueAttributes));
|
|
}
|
|
|
|
private static String uniqueAttributes(Spatial spatial){
|
|
if(Objects.isNull(spatial)){
|
|
return StringUtils.EMPTY;
|
|
}
|
|
String name = "";
|
|
if (!Objects.isNull(spatial.getPlaceName())) {
|
|
name = spatial.getPlaceName();
|
|
}
|
|
String lat = "";
|
|
String lon = "";
|
|
if (!Objects.isNull(spatial.getGeopoint())) {
|
|
lat = Double.toString(spatial.getGeopoint().getLat());
|
|
lon = Double.toString(spatial.getGeopoint().getLon());
|
|
}
|
|
String uniqueAttribute = (name) + (lat) + (lon);
|
|
return uniqueAttribute;
|
|
}
|
|
}
|