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) @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class NestedReferenceElasticQuery extends ElasticNestedQuery<NestedReferenceElasticQuery, NestedReferenceElasticEntity, UUID> { public class NestedReferenceElasticQuery extends ElasticNestedQuery<NestedReferenceElasticQuery, NestedReferenceElasticEntity, UUID> {
private Collection<UUID> ids;
private String nestedPath; 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 @Override
public NestedReferenceElasticQuery nestedPath(String value) { public NestedReferenceElasticQuery nestedPath(String value) {
this.nestedPath = value; this.nestedPath = value;
@ -50,8 +66,17 @@ public class NestedReferenceElasticQuery extends ElasticNestedQuery<NestedRefere
@Override @Override
protected Query applyFilters() { protected Query applyFilters() {
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; return null;
} }
}
@Override @Override
public NestedReferenceElasticEntity convert(Map<String, Object> rawData, Set<String> columns) { public NestedReferenceElasticEntity convert(Map<String, Object> rawData, Set<String> columns) {

View File

@ -47,9 +47,11 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
private Collection<PlanAccessType> accessTypes; private Collection<PlanAccessType> accessTypes;
private Collection<Integer> versions; private Collection<Integer> versions;
private Collection<UUID> groupIds; private Collection<UUID> groupIds;
private Collection<UUID> blueprintIds;
private NestedCollaboratorElasticQuery planUserSubQuery; private NestedCollaboratorElasticQuery planUserSubQuery;
private NestedDescriptionElasticQuery descriptionSubQuery; private NestedDescriptionElasticQuery descriptionSubQuery;
private NestedPlanDescriptionTemplateElasticQuery planDescriptionTemplateSubQuery; private NestedPlanDescriptionTemplateElasticQuery planDescriptionTemplateSubQuery;
private NestedReferenceElasticQuery referenceSubQuery;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None); private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@ -68,6 +70,11 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
return this; return this;
} }
public PlanElasticQuery referenceSubQuery(NestedReferenceElasticQuery subQuery) {
this.referenceSubQuery = subQuery;
return this;
}
public PlanElasticQuery like(String value) { public PlanElasticQuery like(String value) {
this.like = value; this.like = value;
return this; return this;
@ -178,6 +185,21 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
return this; 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) { public PlanElasticQuery authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values; this.authorize = values;
return this; return this;
@ -297,6 +319,9 @@ public class PlanElasticQuery extends ElasticQuery<PlanElasticEntity, UUID> {
if (this.groupIds != null) { if (this.groupIds != null) {
predicates.add(this.containsUUID(this.elasticFieldOf(PlanElasticEntity._groupId), this.groupIds)._toQuery()); 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) { if (this.versions != null) {
predicates.add(this.contains(this.elasticFieldOf(PlanElasticEntity._version), this.versions.toArray(new Integer[this.versions.size()]))._toQuery()); 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) { if (this.planDescriptionTemplateSubQuery != null) {
predicates.add(this.nestedQuery( this.planDescriptionTemplateSubQuery.nestedPath(PlanElasticEntity._planDescriptionTemplates)).build()._toQuery()); 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()) { if (!predicates.isEmpty()) {
return this.and(predicates); 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.finalizedAfter != null) query.finalizedAfter(this.finalizedAfter);
if (this.finalizedBefore != null) query.finalizedBefore(this.finalizedBefore); if (this.finalizedBefore != null) query.finalizedBefore(this.finalizedBefore);
if (this.planSubQuery != null) query.planSubQuery(this.planSubQuery.enrichElasticInner(queryFactory)); 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); this.enrichCommon(query);
return query; return query;

View File

@ -172,7 +172,10 @@ public class PlanLookup extends Lookup {
if (this.versionStatuses != null) query.versionStatuses(this.versionStatuses); if (this.versionStatuses != null) query.versionStatuses(this.versionStatuses);
if (this.planDescriptionTemplateSubQuery != null) query.planDescriptionTemplateSubQuery(this.planDescriptionTemplateSubQuery.enrichElasticInner(queryFactory)); if (this.planDescriptionTemplateSubQuery != null) query.planDescriptionTemplateSubQuery(this.planDescriptionTemplateSubQuery.enrichElasticInner(queryFactory));
if (this.planUserSubQuery != null) query.planSubQuery(this.planUserSubQuery.enrichElasticInner(queryFactory)); if (this.planUserSubQuery != null) query.planSubQuery(this.planUserSubQuery.enrichElasticInner(queryFactory));
// TODO ?? 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); this.enrichCommon(query);
return query; return query;
@ -190,12 +193,12 @@ public class PlanLookup extends Lookup {
if (this.versionStatuses != null) query.versionStatuses(this.versionStatuses); if (this.versionStatuses != null) query.versionStatuses(this.versionStatuses);
if (this.planDescriptionTemplateSubQuery != null) throw new UnsupportedOperationException(""); if (this.planDescriptionTemplateSubQuery != null) throw new UnsupportedOperationException("");
if (this.planUserSubQuery != null) query.planSubQuery(this.planUserSubQuery.enrichElasticInner(queryFactory)); 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; return query;
} }
public boolean useElastic() { public boolean useElastic() { return this.like != null && !this.like.isBlank(); }
return this.like != null && !this.like.isBlank();
}
} }

View File

@ -1,5 +1,6 @@
package org.opencdmp.query.lookup; package org.opencdmp.query.lookup;
import org.opencdmp.elastic.query.NestedReferenceElasticQuery;
import org.opencdmp.query.PlanReferenceQuery; import org.opencdmp.query.PlanReferenceQuery;
import gr.cite.tools.data.query.Lookup; import gr.cite.tools.data.query.Lookup;
import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.data.query.QueryFactory;
@ -50,4 +51,14 @@ public class PlanReferenceLookup extends Lookup {
return query; 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;
}
} }