Merge Dataset Index to the Dmp Index (ref #252)
This commit is contained in:
parent
e0211a019c
commit
96576b16a8
|
@ -2,46 +2,64 @@ package eu.eudat.elastic.repository;
|
||||||
|
|
||||||
import eu.eudat.elastic.criteria.DatasetCriteria;
|
import eu.eudat.elastic.criteria.DatasetCriteria;
|
||||||
import eu.eudat.elastic.entities.Dataset;
|
import eu.eudat.elastic.entities.Dataset;
|
||||||
|
import eu.eudat.elastic.entities.Dmp;
|
||||||
import eu.eudat.elastic.entities.Tag;
|
import eu.eudat.elastic.entities.Tag;
|
||||||
import org.elasticsearch.action.get.GetRequest;
|
import org.apache.lucene.search.join.ScoreMode;
|
||||||
import org.elasticsearch.action.get.GetResponse;
|
|
||||||
import org.elasticsearch.action.index.IndexRequest;
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.action.search.SearchRequest;
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.client.RequestOptions;
|
import org.elasticsearch.client.RequestOptions;
|
||||||
import org.elasticsearch.client.RestHighLevelClient;
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
import org.elasticsearch.client.core.CountRequest;
|
|
||||||
import org.elasticsearch.client.core.CountResponse;
|
|
||||||
import org.elasticsearch.client.indices.GetIndexRequest;
|
import org.elasticsearch.client.indices.GetIndexRequest;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.InnerHitBuilder;
|
||||||
|
import org.elasticsearch.index.query.NestedQueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
import org.elasticsearch.search.SearchHits;
|
||||||
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.filter.FiltersAggregationBuilder;
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.filter.ParsedFilters;
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder;
|
||||||
|
import org.elasticsearch.search.aggregations.bucket.nested.ParsedNested;
|
||||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@Service("datasetRepository")
|
@Service("datasetRepository")
|
||||||
public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteria> {
|
public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteria> {
|
||||||
|
|
||||||
@Autowired
|
private final DmpRepository dmpRepository;
|
||||||
public DatasetRepository(RestHighLevelClient client) {
|
|
||||||
|
public DatasetRepository(RestHighLevelClient client, DmpRepository dmpRepository) {
|
||||||
super(client);
|
super(client);
|
||||||
|
this.dmpRepository = dmpRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dataset createOrUpdate(Dataset entity) throws IOException {
|
public Dataset createOrUpdate(Dataset entity) throws IOException {
|
||||||
if (this.getClient() != null) {
|
if (this.getClient() != null) {
|
||||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||||
IndexRequest request = new IndexRequest("datasets").id(entity.getId()).source(entity.toElasticEntity(builder));//new IndexRequest("datasets", "doc", entity.getId()).source(entity.toElasticEntity(builder));
|
Dmp dmp = this.dmpRepository.findDocument(entity.getDmp().toString());
|
||||||
|
boolean found = false;
|
||||||
|
for(int i = 0; i < dmp.getDatasets().size(); i++) {
|
||||||
|
if (dmp.getDatasets().get(i).getId().equals(entity.getId())) {
|
||||||
|
dmp.getDatasets().set(i, entity);
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
dmp.getDatasets().add(entity);
|
||||||
|
}
|
||||||
|
IndexRequest request = new IndexRequest("dmps").id(dmp.getId().toString()).source(dmp.toElasticEntity(builder));//new IndexRequest("datasets", "doc", entity.getId()).source(entity.toElasticEntity(builder));
|
||||||
this.getClient().index(request, RequestOptions.DEFAULT);
|
this.getClient().index(request, RequestOptions.DEFAULT);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
@ -51,9 +69,20 @@ public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteri
|
||||||
@Override
|
@Override
|
||||||
public Dataset findDocument(String id) throws IOException {
|
public Dataset findDocument(String id) throws IOException {
|
||||||
if (this.getClient() != null) {
|
if (this.getClient() != null) {
|
||||||
GetRequest request = new GetRequest("datasets", id);
|
SearchRequest searchRequest = new SearchRequest("dmps");
|
||||||
GetResponse response = this.getClient().get(request, RequestOptions.DEFAULT);
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||||
return new Dataset().fromElasticEntity(response.getSourceAsMap());
|
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().should(QueryBuilders.termQuery("datasets.id.keyword", id));
|
||||||
|
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery( "datasets", boolQuery, ScoreMode.Avg).innerHit(new InnerHitBuilder());
|
||||||
|
searchSourceBuilder.query(nestedQueryBuilder);
|
||||||
|
searchRequest.source(searchSourceBuilder);
|
||||||
|
SearchResponse response = this.getClient().search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
return ((Stream<Dataset>)Arrays.stream(response.getHits().getHits())
|
||||||
|
.map(hit -> hit.getInnerHits().values()).flatMap(Collection::stream)
|
||||||
|
.map(SearchHits::getHits).flatMap(Arrays::stream)
|
||||||
|
.map(x -> new Dataset().fromElasticEntity(this.transformFromString(x.getSourceAsString(), Map.class)))).findFirst().orElse(null);
|
||||||
|
// GetRequest request = new GetRequest("datasets", id);
|
||||||
|
// GetResponse response = this.getClient().get(request, RequestOptions.DEFAULT);
|
||||||
|
// return new Dataset().fromElasticEntity(response.getSourceAsMap());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -61,70 +90,81 @@ public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteri
|
||||||
@Override
|
@Override
|
||||||
public List<Dataset> query(DatasetCriteria criteria) throws IOException {
|
public List<Dataset> query(DatasetCriteria criteria) throws IOException {
|
||||||
if (this.getClient() != null) {
|
if (this.getClient() != null) {
|
||||||
SearchRequest searchRequest = new SearchRequest("datasets");
|
SearchRequest searchRequest = new SearchRequest("dmps");
|
||||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||||
|
|
||||||
CountRequest countRequest = new CountRequest("datasets");
|
/*CountRequest countRequest = new CountRequest("dmps").routing("datasets").routing("id");
|
||||||
countRequest.query(QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery("status.keyword", Stream.of(Dataset.Status.DELETED.getValue(), Dataset.Status.CANCELED.getValue()).collect(Collectors.toList()))));
|
countRequest.query(QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery("datasets.status.keyword", Stream.of(Dataset.Status.DELETED.getValue(), Dataset.Status.CANCELED.getValue()).collect(Collectors.toList()))));
|
||||||
CountResponse countResponse = getClient().count(countRequest, RequestOptions.DEFAULT);
|
CountResponse countResponse = getClient().count(countRequest, RequestOptions.DEFAULT);
|
||||||
Long count = countResponse.getCount();
|
Long count = countResponse.getCount();*/
|
||||||
|
|
||||||
|
SearchRequest countRequest = new SearchRequest("dmps");
|
||||||
|
NestedAggregationBuilder nestedAggregationBuilder = AggregationBuilders.nested("by_dataset", "datasets");
|
||||||
|
FiltersAggregationBuilder filtersAggregationBuilder = AggregationBuilders.filters("dataset_query", QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery("datasets.status.keyword", Stream.of(Dataset.Status.DELETED.getValue(), Dataset.Status.CANCELED.getValue()).collect(Collectors.toList()))));
|
||||||
|
nestedAggregationBuilder.subAggregation(filtersAggregationBuilder);
|
||||||
|
SearchSourceBuilder countSourceBuilder = new SearchSourceBuilder();
|
||||||
|
countSourceBuilder.aggregation(nestedAggregationBuilder);
|
||||||
|
countRequest.source(countSourceBuilder);
|
||||||
|
SearchResponse countResponse = getClient().search(countRequest, RequestOptions.DEFAULT);
|
||||||
|
Long count = ((ParsedFilters)((ParsedNested)countResponse.getAggregations().asMap().get("by_dataset")).getAggregations().get("dataset_query")).getBuckets().get(0).getDocCount();
|
||||||
|
|
||||||
|
|
||||||
searchSourceBuilder.size(count.intValue());
|
searchSourceBuilder.size(count.intValue());
|
||||||
|
|
||||||
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery("status.keyword", Stream.of(Dataset.Status.DELETED.getValue(), Dataset.Status.CANCELED.getValue()).collect(Collectors.toList())));
|
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery("datasets.status", Stream.of(Dataset.Status.DELETED.getValue(), Dataset.Status.CANCELED.getValue()).collect(Collectors.toList())));
|
||||||
if (criteria.isPublic()) {
|
if (criteria.isPublic()) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termQuery("public.keyword", "true"));
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("datasets.public", "true"));
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termQuery("status.keyword", Dataset.Status.FINALISED.getValue()));
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("datasets.status", Dataset.Status.FINALISED.getValue()));
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termQuery("lastPublicVersion.keyword", "true"));
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("datasets.lastPublicVersion", "true"));
|
||||||
}
|
}
|
||||||
if (criteria.getLike() != null && !criteria.getLike().isEmpty()) {
|
if (criteria.getLike() != null && !criteria.getLike().isEmpty()) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.queryStringQuery(criteria.getLike()).fields(Stream.of(new Object[][]{
|
boolQuery = boolQuery.should(QueryBuilders.queryStringQuery(criteria.getLike()).allowLeadingWildcard(true).fields(Stream.of(new Object[][]{
|
||||||
{"label", 1.0f},
|
{"datasets.label", 1.0f},
|
||||||
{"description", 1.0f},
|
{"datasets.description", 1.0f},
|
||||||
{"formData", 1.0f}
|
{"datasets.formData", 1.0f}
|
||||||
}).collect(Collectors.toMap(data -> (String) data[0], data -> (Float) data[1]))));
|
}).collect(Collectors.toMap(data -> (String) data[0], data -> (Float) data[1]))));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getDatasetTemplates() != null && criteria.getDatasetTemplates().size() > 0) {
|
if (criteria.getDatasetTemplates() != null && criteria.getDatasetTemplates().size() > 0) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termsQuery("template.keyword", criteria.getDatasetTemplates().stream().map(UUID::toString).collect(Collectors.toList())));
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("datasets.template", criteria.getDatasetTemplates().stream().map(UUID::toString).collect(Collectors.toList())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getStatus() != null) {
|
if (criteria.getStatus() != null) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termQuery("status.keyword", criteria.getStatus().toString()));
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("datasets.status", criteria.getStatus().toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getDmps() != null && criteria.getDmps().size() > 0) {
|
if (criteria.getDmps() != null && criteria.getDmps().size() > 0) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termsQuery("dmp.keyword", criteria.getDmps().stream().map(UUID::toString).collect(Collectors.toList())));
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("datasets.dmp", criteria.getDmps().stream().map(UUID::toString).collect(Collectors.toList())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getGroupIds() != null && criteria.getGroupIds().size() > 0) {
|
if (criteria.getGroupIds() != null && criteria.getGroupIds().size() > 0) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termsQuery("group.keyword", criteria.getGroupIds().stream().map(UUID::toString).collect(Collectors.toList())));
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("datasets.group", criteria.getGroupIds().stream().map(UUID::toString).collect(Collectors.toList())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getGrants() != null && criteria.getGrants().size() > 0) {
|
if (criteria.getGrants() != null && criteria.getGrants().size() > 0) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termsQuery("grant.keyword", criteria.getGrants().stream().map(UUID::toString).collect(Collectors.toList())));
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("datasets.grant", criteria.getGrants().stream().map(UUID::toString).collect(Collectors.toList())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getGrantStatus() != null) {
|
if (criteria.getGrantStatus() != null) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termQuery("grantStatus.keyword", criteria.getGrantStatus().toString()));
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("datasets.grantStatus", criteria.getGrantStatus().toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getCollaborators() != null && criteria.getCollaborators().size() > 0) {
|
if (criteria.getCollaborators() != null && criteria.getCollaborators().size() > 0) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termsQuery("collaborators.id.keyword", criteria.getCollaborators().stream().map(UUID::toString).collect(Collectors.toList())));
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("datasets.collaborators.id", criteria.getCollaborators().stream().map(UUID::toString).collect(Collectors.toList())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!criteria.isPublic()) {
|
if (!criteria.isPublic()) {
|
||||||
if (criteria.getAllowAllVersions() != null && !criteria.getAllowAllVersions()) {
|
if (criteria.getAllowAllVersions() != null && !criteria.getAllowAllVersions()) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termQuery("lastVersion.keyword", "true"));
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("datasets.lastVersion", "true"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getOrganiztions() != null && criteria.getOrganiztions().size() > 0) {
|
if (criteria.getOrganiztions() != null && criteria.getOrganiztions().size() > 0) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termsQuery("organizations.id.keyword", criteria.getOrganiztions()));
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("datasets.organizations.id", criteria.getOrganiztions()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (criteria.getTags() != null && criteria.getTags().size() > 0) {
|
if (criteria.getTags() != null && criteria.getTags().size() > 0) {
|
||||||
boolQuery = boolQuery.should(QueryBuilders.termsQuery("tags.name.keyword", criteria.getTags().stream().map(Tag::getName).collect(Collectors.toList())));
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("datasets.tags.name", criteria.getTags().stream().map(Tag::getName).collect(Collectors.toList())));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (boolQuery.should().isEmpty() && boolQuery.mustNot().isEmpty()) {
|
if (boolQuery.should().isEmpty() && boolQuery.mustNot().isEmpty()) {
|
||||||
|
@ -132,10 +172,15 @@ public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteri
|
||||||
} else {
|
} else {
|
||||||
boolQuery.minimumShouldMatch(boolQuery.should().size());
|
boolQuery.minimumShouldMatch(boolQuery.should().size());
|
||||||
}
|
}
|
||||||
searchSourceBuilder.query(boolQuery);
|
|
||||||
|
NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("datasets", boolQuery, ScoreMode.Avg).innerHit(new InnerHitBuilder());
|
||||||
|
searchSourceBuilder.query(nestedQueryBuilder);
|
||||||
searchRequest.source(searchSourceBuilder);
|
searchRequest.source(searchSourceBuilder);
|
||||||
SearchResponse response = this.getClient().search(searchRequest, RequestOptions.DEFAULT);
|
SearchResponse response = this.getClient().search(searchRequest, RequestOptions.DEFAULT);
|
||||||
return Arrays.stream(response.getHits().getHits()).map(x -> this.transformFromString(x.getSourceAsString(), Dataset.class)).collect(Collectors.toList());
|
return ((Stream<Dataset>)Arrays.stream(response.getHits().getHits())
|
||||||
|
.map(hit -> hit.getInnerHits().values()).flatMap(Collection::stream)
|
||||||
|
.map(SearchHits::getHits).flatMap(Arrays::stream)
|
||||||
|
.map(x -> new Dataset().fromElasticEntity(this.transformFromString(x.getSourceAsString(), Map.class)))).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -143,7 +188,7 @@ public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteri
|
||||||
@Override
|
@Override
|
||||||
public boolean exists() throws IOException {
|
public boolean exists() throws IOException {
|
||||||
if (this.getClient() != null) {
|
if (this.getClient() != null) {
|
||||||
GetIndexRequest request = new GetIndexRequest("datasets");
|
GetIndexRequest request = new GetIndexRequest("dmps");
|
||||||
// request.indices("datasets");
|
// request.indices("datasets");
|
||||||
return this.getClient().indices().exists(request, RequestOptions.DEFAULT);
|
return this.getClient().indices().exists(request, RequestOptions.DEFAULT);
|
||||||
}
|
}
|
||||||
|
@ -152,10 +197,11 @@ public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteri
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() throws IOException {
|
public void clear() throws IOException {
|
||||||
if (exists()) {
|
//DON'T
|
||||||
|
/* if (exists()) {
|
||||||
DeleteByQueryRequest delete = new DeleteByQueryRequest("datasets");
|
DeleteByQueryRequest delete = new DeleteByQueryRequest("datasets");
|
||||||
delete.setQuery(QueryBuilders.matchAllQuery());
|
delete.setQuery(QueryBuilders.matchAllQuery());
|
||||||
this.getClient().deleteByQuery(delete, RequestOptions.DEFAULT);
|
this.getClient().deleteByQuery(delete, RequestOptions.DEFAULT);
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,43 +1,63 @@
|
||||||
package eu.eudat.elastic.repository;
|
package eu.eudat.elastic.repository;
|
||||||
|
|
||||||
import eu.eudat.elastic.criteria.DmpCriteria;
|
import eu.eudat.elastic.criteria.DmpCriteria;
|
||||||
import eu.eudat.elastic.entities.Dataset;
|
|
||||||
import eu.eudat.elastic.entities.Dmp;
|
import eu.eudat.elastic.entities.Dmp;
|
||||||
import eu.eudat.elastic.entities.Tag;
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||||
import org.elasticsearch.action.get.GetRequest;
|
import org.elasticsearch.action.get.GetRequest;
|
||||||
import org.elasticsearch.action.get.GetResponse;
|
import org.elasticsearch.action.get.GetResponse;
|
||||||
import org.elasticsearch.action.index.IndexRequest;
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.action.index.IndexResponse;
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
import org.elasticsearch.action.search.SearchRequest;
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
||||||
import org.elasticsearch.client.RequestOptions;
|
import org.elasticsearch.client.RequestOptions;
|
||||||
import org.elasticsearch.client.RestHighLevelClient;
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
import org.elasticsearch.client.core.CountRequest;
|
import org.elasticsearch.client.core.CountRequest;
|
||||||
import org.elasticsearch.client.core.CountResponse;
|
import org.elasticsearch.client.core.CountResponse;
|
||||||
|
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||||
import org.elasticsearch.client.indices.GetIndexRequest;
|
import org.elasticsearch.client.indices.GetIndexRequest;
|
||||||
|
import org.elasticsearch.client.indices.PutMappingRequest;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
||||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@Service("dmpRepository")
|
@Service("dmpRepository")
|
||||||
public class DmpRepository extends ElasticRepository<Dmp, DmpCriteria> {
|
public class DmpRepository extends ElasticRepository<Dmp, DmpCriteria> {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(DmpRepository.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public DmpRepository(RestHighLevelClient client) {
|
public DmpRepository(RestHighLevelClient client) {
|
||||||
super(client);
|
super(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void generateMapping() throws IOException {
|
||||||
|
if (this.getClient() != null) {
|
||||||
|
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||||
|
builder.startObject();
|
||||||
|
builder.startObject("properties");
|
||||||
|
builder.startObject("datasets");
|
||||||
|
builder.field("type", "nested");
|
||||||
|
builder.endObject();
|
||||||
|
builder.endObject();
|
||||||
|
builder.endObject();
|
||||||
|
PutMappingRequest putMappingRequest = new PutMappingRequest("dmps");
|
||||||
|
putMappingRequest.source(builder);
|
||||||
|
this.getClient().indices().putMapping(putMappingRequest, RequestOptions.DEFAULT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dmp createOrUpdate(Dmp entity) throws IOException {
|
public Dmp createOrUpdate(Dmp entity) throws IOException {
|
||||||
if (this.getClient() != null) {
|
if (this.getClient() != null) {
|
||||||
|
@ -129,6 +149,20 @@ public class DmpRepository extends ElasticRepository<Dmp, DmpCriteria> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean createIndex() {
|
||||||
|
try {
|
||||||
|
if (!this.exists()) {
|
||||||
|
CreateIndexRequest createIndexRequest = new CreateIndexRequest("dmps");
|
||||||
|
this.getClient().indices().create(createIndexRequest, RequestOptions.DEFAULT);
|
||||||
|
this.generateMapping();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean exists() throws IOException {
|
public boolean exists() throws IOException {
|
||||||
if (this.getClient() != null) {
|
if (this.getClient() != null) {
|
||||||
|
@ -144,6 +178,8 @@ public class DmpRepository extends ElasticRepository<Dmp, DmpCriteria> {
|
||||||
DeleteByQueryRequest delete = new DeleteByQueryRequest("dmps");
|
DeleteByQueryRequest delete = new DeleteByQueryRequest("dmps");
|
||||||
delete.setQuery(QueryBuilders.matchAllQuery());
|
delete.setQuery(QueryBuilders.matchAllQuery());
|
||||||
this.getClient().deleteByQuery(delete, RequestOptions.DEFAULT);
|
this.getClient().deleteByQuery(delete, RequestOptions.DEFAULT);
|
||||||
|
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("dmps");
|
||||||
|
this.getClient().indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1738,6 +1738,7 @@ public class DataManagementPlanManager {
|
||||||
|
|
||||||
public void generateIndex(Principal principal) {
|
public void generateIndex(Principal principal) {
|
||||||
if (principal.getAuthorities().contains(Authorities.ADMIN.getValue())) {
|
if (principal.getAuthorities().contains(Authorities.ADMIN.getValue())) {
|
||||||
|
if (apiContext.getOperationsContext().getElasticRepository().getDmpRepository().createIndex()) {
|
||||||
List<DMP> dmps = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().asQueryable().toList();
|
List<DMP> dmps = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().asQueryable().toList();
|
||||||
dmps.forEach(dmp -> {
|
dmps.forEach(dmp -> {
|
||||||
try {
|
try {
|
||||||
|
@ -1748,6 +1749,7 @@ public class DataManagementPlanManager {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void clearIndex(Principal principal) throws IOException {
|
public void clearIndex(Principal principal) throws IOException {
|
||||||
if (principal.getAuthorities().contains(Authorities.ADMIN.getValue())) {
|
if (principal.getAuthorities().contains(Authorities.ADMIN.getValue())) {
|
||||||
|
|
|
@ -134,7 +134,7 @@ public class DatasetManager {
|
||||||
datasets = datasetRepository.exists() ?
|
datasets = datasetRepository.exists() ?
|
||||||
datasetRepository.query(datasetCriteria) : null;
|
datasetRepository.query(datasetCriteria) : null;
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
logger.warn(ex.getMessage());
|
logger.warn(ex.getMessage(), ex);
|
||||||
datasets = null;
|
datasets = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,16 +30,16 @@ export class IndexManagmentComponent extends BaseComponent implements OnInit {
|
||||||
|
|
||||||
generateIndex(ev: Event) {
|
generateIndex(ev: Event) {
|
||||||
(ev.srcElement as HTMLButtonElement).disabled = true;
|
(ev.srcElement as HTMLButtonElement).disabled = true;
|
||||||
this.datasetService.generateIndex().pipe(takeUntil(this._destroyed)).subscribe(
|
// this.datasetService.generateIndex().pipe(takeUntil(this._destroyed)).subscribe(
|
||||||
response => {
|
// response => {
|
||||||
(ev.srcElement as HTMLButtonElement).disabled = false;
|
// (ev.srcElement as HTMLButtonElement).disabled = false;
|
||||||
this.onCallbackSuccess();
|
// this.onCallbackSuccess();
|
||||||
},
|
// },
|
||||||
error => {
|
// error => {
|
||||||
(ev.srcElement as HTMLButtonElement).disabled = false;
|
// (ev.srcElement as HTMLButtonElement).disabled = false;
|
||||||
this.onCallbackError(error);
|
// this.onCallbackError(error);
|
||||||
}
|
// }
|
||||||
);
|
// );
|
||||||
this.dmpService.generateIndex().pipe(takeUntil(this._destroyed)).subscribe(
|
this.dmpService.generateIndex().pipe(takeUntil(this._destroyed)).subscribe(
|
||||||
response => {
|
response => {
|
||||||
(ev.srcElement as HTMLButtonElement).disabled = false;
|
(ev.srcElement as HTMLButtonElement).disabled = false;
|
||||||
|
@ -54,16 +54,16 @@ export class IndexManagmentComponent extends BaseComponent implements OnInit {
|
||||||
|
|
||||||
clearIndex(ev: Event) {
|
clearIndex(ev: Event) {
|
||||||
(ev.srcElement as HTMLButtonElement).disabled = true;
|
(ev.srcElement as HTMLButtonElement).disabled = true;
|
||||||
this.datasetService.clearIndex().pipe(takeUntil(this._destroyed)).subscribe(
|
// this.datasetService.clearIndex().pipe(takeUntil(this._destroyed)).subscribe(
|
||||||
response => {
|
// response => {
|
||||||
(ev.srcElement as HTMLButtonElement).disabled = false;
|
// (ev.srcElement as HTMLButtonElement).disabled = false;
|
||||||
this.onCallbackSuccess();
|
// this.onCallbackSuccess();
|
||||||
},
|
// },
|
||||||
error => {
|
// error => {
|
||||||
(ev.srcElement as HTMLButtonElement).disabled = false;
|
// (ev.srcElement as HTMLButtonElement).disabled = false;
|
||||||
this.onCallbackError(error);
|
// this.onCallbackError(error);
|
||||||
}
|
// }
|
||||||
);
|
// );
|
||||||
this.dmpService.clearIndex().pipe(takeUntil(this._destroyed)).subscribe(
|
this.dmpService.clearIndex().pipe(takeUntil(this._destroyed)).subscribe(
|
||||||
response => {
|
response => {
|
||||||
(ev.srcElement as HTMLButtonElement).disabled = false;
|
(ev.srcElement as HTMLButtonElement).disabled = false;
|
||||||
|
|
Loading…
Reference in New Issue