2018-07-11 15:47:36 +02:00
|
|
|
package eu.eudat.elastic.repository;
|
|
|
|
|
|
|
|
import eu.eudat.elastic.criteria.DatasetCriteria;
|
|
|
|
import eu.eudat.elastic.entities.Dataset;
|
2020-03-11 10:04:49 +01:00
|
|
|
import eu.eudat.elastic.entities.Tag;
|
2018-07-11 15:47:36 +02:00
|
|
|
import org.elasticsearch.action.get.GetRequest;
|
|
|
|
import org.elasticsearch.action.get.GetResponse;
|
|
|
|
import org.elasticsearch.action.index.IndexRequest;
|
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
2020-03-09 15:47:32 +01:00
|
|
|
import org.elasticsearch.client.RequestOptions;
|
2018-07-11 15:47:36 +02:00
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
2020-03-17 11:41:27 +01:00
|
|
|
import org.elasticsearch.client.core.CountRequest;
|
|
|
|
import org.elasticsearch.client.core.CountResponse;
|
|
|
|
import org.elasticsearch.client.indices.GetIndexRequest;
|
2018-07-11 15:47:36 +02:00
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
2020-03-11 10:04:49 +01:00
|
|
|
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
2018-07-11 15:47:36 +02:00
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2020-03-09 15:47:32 +01:00
|
|
|
import java.util.UUID;
|
2018-07-11 15:47:36 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-03-17 11:41:27 +01:00
|
|
|
import java.util.stream.Stream;
|
2018-07-11 15:47:36 +02:00
|
|
|
|
|
|
|
@Service("datasetRepository")
|
|
|
|
public class DatasetRepository extends ElasticRepository<Dataset, DatasetCriteria> {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public DatasetRepository(RestHighLevelClient client) {
|
|
|
|
super(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Dataset createOrUpdate(Dataset entity) throws IOException {
|
|
|
|
XContentBuilder builder = XContentFactory.jsonBuilder();
|
2020-03-09 15:47:32 +01:00
|
|
|
IndexRequest request = new IndexRequest("datasets").id(entity.getId()).source(entity.toElasticEntity(builder));//new IndexRequest("datasets", "doc", entity.getId()).source(entity.toElasticEntity(builder));
|
|
|
|
this.getClient().index(request, RequestOptions.DEFAULT);
|
2018-07-11 15:47:36 +02:00
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Dataset findDocument(String id) throws IOException {
|
2020-03-09 15:47:32 +01:00
|
|
|
GetRequest request = new GetRequest("datasets",id);
|
|
|
|
GetResponse response = this.getClient().get(request, RequestOptions.DEFAULT);
|
2018-07-11 15:47:36 +02:00
|
|
|
return new Dataset().fromElasticEntity(response.getSourceAsMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-17 11:41:27 +01:00
|
|
|
public List<Dataset> query(DatasetCriteria criteria) throws IOException {
|
2020-03-09 15:47:32 +01:00
|
|
|
SearchRequest searchRequest = new SearchRequest("datasets");
|
2018-07-11 15:47:36 +02:00
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
|
|
2020-03-09 15:47:32 +01:00
|
|
|
CountRequest countRequest = new CountRequest("datasets");
|
2020-03-17 11:41:27 +01:00
|
|
|
countRequest.query(QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery("status.keyword", Stream.of(Dataset.Status.DELETED.getValue(), Dataset.Status.CANCELED.getValue()).collect(Collectors.toList()))));
|
2020-03-09 15:47:32 +01:00
|
|
|
CountResponse countResponse = getClient().count(countRequest, RequestOptions.DEFAULT);
|
|
|
|
Long count = countResponse.getCount();
|
|
|
|
|
|
|
|
searchSourceBuilder.size(count.intValue());
|
|
|
|
|
2020-03-17 11:41:27 +01:00
|
|
|
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery("status.keyword", Stream.of(Dataset.Status.DELETED.getValue(), Dataset.Status.CANCELED.getValue()).collect(Collectors.toList())));
|
2020-03-16 17:50:29 +01:00
|
|
|
if (criteria.isPublic()) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("public.keyword", "true"));
|
2020-03-17 11:43:20 +01:00
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("status.keyword", Dataset.Status.FINALISED.getValue()));
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("lastPublicVersion.keyword", "true"));
|
2020-03-16 17:50:29 +01:00
|
|
|
}
|
2020-03-17 11:41:27 +01:00
|
|
|
if (criteria.getLike() != null && !criteria.getLike().isEmpty()) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.queryStringQuery("*" + criteria.getLike() + "*").fields(Stream.of(new Object[][] {
|
|
|
|
{ "label", 1.0f },
|
|
|
|
{ "description", 1.0f },
|
|
|
|
}).collect(Collectors.toMap(data -> (String) data[0], data -> (Float) data[1]))));
|
2020-03-09 15:47:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (criteria.getDatasetTemplates() != null && criteria.getDatasetTemplates().size() > 0) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("template.keyword", criteria.getDatasetTemplates().stream().map(UUID::toString).collect(Collectors.toList())));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (criteria.getStatus() != null) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("status.keyword", criteria.getStatus().toString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (criteria.getDmps() != null && criteria.getDmps().size() > 0) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("dmp.keyword", criteria.getDmps().stream().map(UUID::toString).collect(Collectors.toList())));
|
|
|
|
}
|
|
|
|
|
2020-03-11 10:04:49 +01:00
|
|
|
if (criteria.getGroupIds() != null && criteria.getGroupIds().size() > 0) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("group.keyword", criteria.getGroupIds().stream().map(UUID::toString).collect(Collectors.toList())));
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:47:32 +01:00
|
|
|
if (criteria.getGrants() != null && criteria.getGrants().size() > 0) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("grant.keyword", criteria.getGrants().stream().map(UUID::toString).collect(Collectors.toList())));
|
|
|
|
}
|
|
|
|
|
2020-03-16 17:50:29 +01:00
|
|
|
if (criteria.getGrantStatus() != null) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("grantStatus.keyword", criteria.getGrantStatus().toString()));
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:47:32 +01:00
|
|
|
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())));
|
|
|
|
}
|
|
|
|
|
2020-03-17 11:41:27 +01:00
|
|
|
if (!criteria.isPublic()) {
|
|
|
|
if (criteria.getAllowAllVersions() != null && !criteria.getAllowAllVersions()) {
|
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termQuery("lastVersion.keyword", "true"));
|
|
|
|
}
|
2020-03-11 10:04:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 14:29:37 +01:00
|
|
|
if (criteria.getOrganiztions() != null && criteria.getOrganiztions().size() > 0) {
|
2020-03-11 10:04:49 +01:00
|
|
|
boolQuery = boolQuery.should(QueryBuilders.termsQuery("organizations.id.keyword", criteria.getOrganiztions()));
|
|
|
|
}
|
|
|
|
|
|
|
|
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())));
|
|
|
|
}
|
|
|
|
|
2020-03-16 10:09:24 +01:00
|
|
|
if (boolQuery.should().isEmpty() && boolQuery.mustNot().isEmpty()) {
|
2020-03-09 15:47:32 +01:00
|
|
|
boolQuery.should(QueryBuilders.matchAllQuery());
|
|
|
|
} else {
|
|
|
|
boolQuery.minimumShouldMatch(boolQuery.should().size());
|
|
|
|
}
|
2018-07-11 15:47:36 +02:00
|
|
|
searchSourceBuilder.query(boolQuery);
|
|
|
|
searchRequest.source(searchSourceBuilder);
|
2020-03-09 15:47:32 +01:00
|
|
|
SearchResponse response = this.getClient().search(searchRequest, RequestOptions.DEFAULT);
|
2018-07-11 15:47:36 +02:00
|
|
|
return Arrays.stream(response.getHits().getHits()).map(x -> this.transformFromString(x.getSourceAsString(), Dataset.class)).collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean exists() throws IOException {
|
2020-03-09 15:47:32 +01:00
|
|
|
GetIndexRequest request = new GetIndexRequest("datasets");
|
|
|
|
// request.indices("datasets");
|
|
|
|
return this.getClient().indices().exists(request, RequestOptions.DEFAULT);
|
2018-07-11 15:47:36 +02:00
|
|
|
}
|
2020-03-11 10:04:49 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear() throws IOException {
|
|
|
|
if (exists()) {
|
|
|
|
DeleteByQueryRequest delete = new DeleteByQueryRequest("datasets");
|
|
|
|
delete.setQuery(QueryBuilders.matchAllQuery());
|
|
|
|
this.getClient().deleteByQuery(delete, RequestOptions.DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 15:47:36 +02:00
|
|
|
}
|