fix elastic recent search

This commit is contained in:
Efstratios Giannopoulos 2024-05-03 15:00:44 +03:00
parent 7158fdb6c3
commit 08d29aac5b
4 changed files with 48 additions and 31 deletions

View File

@ -49,9 +49,15 @@ public class DmpElasticQuery extends ElasticQuery<DmpElasticEntity, UUID> {
private Collection<DmpAccessType> accessTypes;
private Collection<Integer> versions;
private Collection<UUID> groupIds;
private NestedCollaboratorElasticQuery dmpUserSubQuery;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
public DmpElasticQuery dmpSubQuery(NestedCollaboratorElasticQuery subQuery) {
this.dmpUserSubQuery = subQuery;
return this;
}
public DmpElasticQuery like(String value) {
this.like = value;
return this;
@ -296,6 +302,9 @@ public class DmpElasticQuery extends ElasticQuery<DmpElasticEntity, UUID> {
if (this.accessTypes != null) {
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._accessType), this.accessTypes.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[this.accessTypes.size()]))._toQuery());
}
if (this.dmpUserSubQuery != null) {
predicates.add(this.nestedQuery( this.dmpUserSubQuery.nestedPath(DmpElasticEntity._collaborators)).build()._toQuery());
}
if (!predicates.isEmpty()) {
return this.and(predicates);

View File

@ -1,11 +1,6 @@
package org.opencdmp.elastic.query;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.commons.enums.DmpAccessType;
import org.opencdmp.commons.enums.DmpStatus;
import org.opencdmp.commons.enums.DmpVersionStatus;
import org.opencdmp.elastic.data.DmpElasticEntity;
import org.opencdmp.elastic.data.nested.NestedDmpElasticEntity;
import gr.cite.tools.data.query.FieldResolver;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.elastic.configuration.ElasticProperties;
@ -14,6 +9,11 @@ import gr.cite.tools.elastic.query.ElasticField;
import gr.cite.tools.elastic.query.ElasticFields;
import gr.cite.tools.elastic.query.ElasticInnerObjectQuery;
import gr.cite.tools.elastic.query.ElasticNestedQuery;
import org.opencdmp.commons.enums.DmpAccessType;
import org.opencdmp.commons.enums.DmpStatus;
import org.opencdmp.commons.enums.DmpVersionStatus;
import org.opencdmp.elastic.data.DescriptionElasticEntity;
import org.opencdmp.elastic.data.nested.NestedDmpElasticEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
@ -35,9 +35,13 @@ public class InnerObjectDmpElasticQuery extends ElasticInnerObjectQuery<InnerObj
private Collection<DmpAccessType> accessTypes;
private Collection<Integer> versions;
private Collection<UUID> groupIds;
private NestedCollaboratorElasticQuery dmpUserSubQuery;
public InnerObjectDmpElasticQuery dmpSubQuery(NestedCollaboratorElasticQuery subQuery) {
this.dmpUserSubQuery = subQuery;
return this;
}
public InnerObjectDmpElasticQuery like(String value) {
this.like = value;
@ -159,7 +163,7 @@ public class InnerObjectDmpElasticQuery extends ElasticInnerObjectQuery<InnerObj
private final QueryFactory queryFactory;
@Autowired()
@Autowired
public InnerObjectDmpElasticQuery(ElasticsearchTemplate elasticsearchTemplate, ElasticProperties elasticProperties, QueryFactory queryFactory) {
super(elasticsearchTemplate, elasticProperties);
this.queryFactory = queryFactory;
@ -179,34 +183,37 @@ public class InnerObjectDmpElasticQuery extends ElasticInnerObjectQuery<InnerObj
protected Query applyFilters() {
List<Query> predicates = new ArrayList<>();
if (like != null && !like.isBlank()) {
if (!like.startsWith("*")) like = "*" + like;
if (!like.endsWith("*")) like = like + "*";
if (this.like != null && !this.like.isBlank()) {
if (!this.like.startsWith("*")) this.like = "*" + this.like;
if (!this.like.endsWith("*")) this.like = this.like + "*";
ElasticFields elasticFields = new ElasticFields(this.entityClass(), List.of(this.getInnerPath()));
elasticFields.add(DmpElasticEntity._label, true);
elasticFields.add(DmpElasticEntity._descriptions, true);
predicates.add(this.like(elasticFields, List.of(like))._toQuery());
elasticFields.add(NestedDmpElasticEntity._label, true);
elasticFields.add(NestedDmpElasticEntity._description, true);
predicates.add(this.like(elasticFields, List.of(this.like))._toQuery());
}
if (ids != null) {
predicates.add(this.containsUUID(this.elasticFieldOf(DmpElasticEntity._id).disableInfer(true), ids)._toQuery());
if (this.ids != null) {
predicates.add(this.containsUUID(this.elasticFieldOf(NestedDmpElasticEntity._id).disableInfer(true), this.ids)._toQuery());
}
if (groupIds != null) {
predicates.add(this.containsUUID(this.elasticFieldOf(DmpElasticEntity._groupId).disableInfer(true), groupIds)._toQuery());
if (this.groupIds != null) {
predicates.add(this.containsUUID(this.elasticFieldOf(NestedDmpElasticEntity._groupId).disableInfer(true), this.groupIds)._toQuery());
}
if (versions != null) {
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._version).disableInfer(true), versions.toArray(new Integer[versions.size()]))._toQuery());
if (this.versions != null) {
predicates.add(this.contains(this.elasticFieldOf(NestedDmpElasticEntity._version).disableInfer(true), this.versions.toArray(new Integer[this.versions.size()]))._toQuery());
}
if (excludedIds != null) {
predicates.add(this.not(this.containsUUID(this.elasticFieldOf(DmpElasticEntity._id).disableInfer(true), excludedIds)._toQuery())._toQuery());
if (this.excludedIds != null) {
predicates.add(this.not(this.containsUUID(this.elasticFieldOf(NestedDmpElasticEntity._id).disableInfer(true), this.excludedIds)._toQuery())._toQuery());
}
if (statuses != null) {
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._status).disableInfer(true), statuses.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[statuses.size()]))._toQuery());
if (this.statuses != null) {
predicates.add(this.contains(this.elasticFieldOf(NestedDmpElasticEntity._status).disableInfer(true), this.statuses.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[this.statuses.size()]))._toQuery());
}
if (versionStatuses != null) {
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._versionStatus).disableInfer(true), versionStatuses.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[versionStatuses.size()]))._toQuery());
if (this.versionStatuses != null) {
predicates.add(this.contains(this.elasticFieldOf(NestedDmpElasticEntity._versionStatus).disableInfer(true), this.versionStatuses.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[this.versionStatuses.size()]))._toQuery());
}
if (accessTypes != null) {
predicates.add(this.contains(this.elasticFieldOf(DmpElasticEntity._accessType).disableInfer(true), accessTypes.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[accessTypes.size()]))._toQuery());
if (this.accessTypes != null) {
predicates.add(this.contains(this.elasticFieldOf(NestedDmpElasticEntity._accessType).disableInfer(true), this.accessTypes.stream().map(x-> x.getValue()).collect(Collectors.toList()).toArray(new Short[this.accessTypes.size()]))._toQuery());
}
if (this.dmpUserSubQuery != null) {
predicates.add(this.nestedQuery( this.dmpUserSubQuery.nestedPath(DescriptionElasticEntity._dmp + "." + NestedDmpElasticEntity._collaborators)).build()._toQuery());
}
if (!predicates.isEmpty()) {

View File

@ -152,7 +152,7 @@ public class DmpLookup extends Lookup {
if (this.versions != null) query.versions(this.versions);
if (this.versionStatuses != null) query.versionStatuses(this.versionStatuses);
if (this.dmpDescriptionTemplateSubQuery != null) throw new UnsupportedOperationException("");
if (this.dmpUserSubQuery != null) throw new UnsupportedOperationException("");
if (this.dmpUserSubQuery != null) query.dmpSubQuery(this.dmpUserSubQuery.enrichElasticInner(queryFactory));
this.enrichCommon(query);
@ -170,7 +170,7 @@ public class DmpLookup extends Lookup {
if (this.versions != null) query.versions(this.versions);
if (this.versionStatuses != null) query.versionStatuses(this.versionStatuses);
if (this.dmpDescriptionTemplateSubQuery != null) throw new UnsupportedOperationException("");
if (this.dmpUserSubQuery != null) throw new UnsupportedOperationException("");
if (this.dmpUserSubQuery != null) query.dmpSubQuery(this.dmpUserSubQuery.enrichElasticInner(queryFactory));
return query;
}

View File

@ -84,11 +84,12 @@ public class DmpUserLookup extends Lookup {
return query;
}
public NestedCollaboratorElasticQuery enrichElastic(QueryFactory queryFactory) {
public NestedCollaboratorElasticQuery enrichElasticInner(QueryFactory queryFactory) {
NestedCollaboratorElasticQuery query = queryFactory.query(NestedCollaboratorElasticQuery.class);
if (this.ids != null) query.ids(this.ids);
if (this.userIds != null) query.userIds(this.userIds);
if (this.userRoles != null) query.userRoles(this.userRoles);
if (this.sectionIds != null) throw new UnsupportedOperationException("");
this.enrichCommon(query);