elastic subqueries

This commit is contained in:
CITE\spapacharalampous 2024-08-27 17:16:05 +03:00
parent b375a1db0f
commit 783cd3e86d
5 changed files with 81 additions and 9 deletions

View File

@ -18,8 +18,24 @@ import java.util.*;
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class NestedReferenceElasticQuery extends ElasticNestedQuery<NestedReferenceElasticQuery, NestedReferenceElasticEntity, UUID> {
private Collection<UUID> ids;
private String nestedPath;
public NestedReferenceElasticQuery ids(UUID value) {
this.ids = List.of(value);
return this;
}
public NestedReferenceElasticQuery ids(UUID... value) {
this.ids = Arrays.asList(value);
return this;
}
public NestedReferenceElasticQuery ids(Collection<UUID> value) {
this.ids = value;
return this;
}
@Override
public NestedReferenceElasticQuery nestedPath(String value) {
this.nestedPath = value;
@ -50,7 +66,16 @@ public class NestedReferenceElasticQuery extends ElasticNestedQuery<NestedRefere
@Override
protected Query applyFilters() {
return null;
List<Query> predicates= new ArrayList<>();
if (this.ids != null) {
predicates.add(this.containsUUID(this.elasticFieldOf(NestedReferenceElasticEntity._id), this.ids)._toQuery());
}
if (!predicates.isEmpty()) {
return this.and(predicates);
} else {
return null;
}
}
@Override

View File

@ -47,9 +47,11 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
private Collection<PlanAccessType> accessTypes;
private Collection<Integer> versions;
private Collection<UUID> groupIds;
private Collection<UUID> blueprintIds;
private NestedCollaboratorElasticQuery planUserSubQuery;
private NestedDescriptionElasticQuery descriptionSubQuery;
private NestedPlanDescriptionTemplateElasticQuery planDescriptionTemplateSubQuery;
private NestedReferenceElasticQuery referenceSubQuery;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@ -67,6 +69,11 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
this.planDescriptionTemplateSubQuery = subQuery;
return this;
}
public PlanElasticQuery referenceSubQuery(NestedReferenceElasticQuery subQuery) {
this.referenceSubQuery = subQuery;
return this;
}
public PlanElasticQuery like(String value) {
this.like = value;
@ -178,6 +185,21 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
return this;
}
public PlanElasticQuery blueprintIds(UUID value) {
this.blueprintIds = List.of(value);
return this;
}
public PlanElasticQuery blueprintIds(UUID... value) {
this.blueprintIds = Arrays.asList(value);
return this;
}
public PlanElasticQuery blueprintIds(Collection<UUID> value) {
this.blueprintIds = value;
return this;
}
public PlanElasticQuery authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
@ -297,6 +319,9 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
if (this.groupIds != null) {
predicates.add(this.containsUUID(this.elasticFieldOf(PlanElasticEntity._groupId), this.groupIds)._toQuery());
}
if (this.blueprintIds != null) {
predicates.add(this.containsUUID(this.elasticFieldOf(PlanElasticEntity._blueprintId), this.blueprintIds)._toQuery());
}
if (this.versions != null) {
predicates.add(this.contains(this.elasticFieldOf(PlanElasticEntity._version), this.versions.toArray(new Integer[this.versions.size()]))._toQuery());
}
@ -321,6 +346,9 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
if (this.planDescriptionTemplateSubQuery != null) {
predicates.add(this.nestedQuery( this.planDescriptionTemplateSubQuery.nestedPath(PlanElasticEntity._planDescriptionTemplates)).build()._toQuery());
}
if (this.referenceSubQuery != null) {
predicates.add(this.nestedQuery( this.referenceSubQuery.nestedPath(PlanElasticEntity._references)).build()._toQuery());
}
if (!predicates.isEmpty()) {
return this.and(predicates);

View File

@ -160,7 +160,12 @@ public class DescriptionLookup extends Lookup {
if (this.finalizedAfter != null) query.finalizedAfter(this.finalizedAfter);
if (this.finalizedBefore != null) query.finalizedBefore(this.finalizedBefore);
if (this.planSubQuery != null) query.planSubQuery(this.planSubQuery.enrichElasticInner(queryFactory));
// TODO: add reference ?
if (this.tenantSubQuery != null) throw new UnsupportedOperationException("");
if (this.descriptionTemplateSubQuery != null) throw new UnsupportedOperationException("");
if (this.descriptionReferenceSubQuery != null) throw new UnsupportedOperationException("");
if (this.descriptionTagSubQuery != null) throw new UnsupportedOperationException("");
this.enrichCommon(query);
return query;

View File

@ -171,8 +171,11 @@ public class PlanLookup extends Lookup {
if (this.versions != null) query.versions(this.versions);
if (this.versionStatuses != null) query.versionStatuses(this.versionStatuses);
if (this.planDescriptionTemplateSubQuery != null) query.planDescriptionTemplateSubQuery(this.planDescriptionTemplateSubQuery.enrichElasticInner(queryFactory));
if (this.planUserSubQuery != null) query.planSubQuery(this.planUserSubQuery.enrichElasticInner(queryFactory));
// TODO ??
if (this.planUserSubQuery != null) query.planSubQuery(this.planUserSubQuery.enrichElasticInner(queryFactory));
if (this.planBlueprintSubQuery != null && this.planBlueprintSubQuery.getIds() != null && !this.planBlueprintSubQuery.getIds().isEmpty()) query.blueprintIds(this.planBlueprintSubQuery.getIds());
if (this.planReferenceSubQuery != null) query.referenceSubQuery(this.planReferenceSubQuery.enrichElasticInner(queryFactory));
if (this.tenantSubQuery != null) throw new UnsupportedOperationException();
this.enrichCommon(query);
return query;
@ -190,12 +193,12 @@ public class PlanLookup extends Lookup {
if (this.versionStatuses != null) query.versionStatuses(this.versionStatuses);
if (this.planDescriptionTemplateSubQuery != null) throw new UnsupportedOperationException("");
if (this.planUserSubQuery != null) query.planSubQuery(this.planUserSubQuery.enrichElasticInner(queryFactory));
// TODO ??
if (this.planBlueprintSubQuery != null && this.planBlueprintSubQuery.getIds() != null && !this.planBlueprintSubQuery.getIds().isEmpty()) throw new UnsupportedOperationException("");
if (this.planReferenceSubQuery != null) throw new UnsupportedOperationException("");
if (this.tenantSubQuery != null) throw new UnsupportedOperationException();
return query;
}
public boolean useElastic() {
return this.like != null && !this.like.isBlank();
}
public boolean useElastic() { return this.like != null && !this.like.isBlank(); }
}

View File

@ -1,5 +1,6 @@
package org.opencdmp.query.lookup;
import org.opencdmp.elastic.query.NestedReferenceElasticQuery;
import org.opencdmp.query.PlanReferenceQuery;
import gr.cite.tools.data.query.Lookup;
import gr.cite.tools.data.query.QueryFactory;
@ -50,4 +51,14 @@ public class PlanReferenceLookup extends Lookup {
return query;
}
public NestedReferenceElasticQuery enrichElasticInner(QueryFactory queryFactory) {
NestedReferenceElasticQuery query = queryFactory.query(NestedReferenceElasticQuery.class);
if (this.ids != null) throw new UnsupportedOperationException("");
if (this.planIds != null) throw new UnsupportedOperationException("");
if (this.referenceIds != null) query.ids(this.referenceIds);
this.enrichCommon(query);
return query;
}
}