From 783cd3e86d5d18c5e50a49d8137c368ff1c5bab4 Mon Sep 17 00:00:00 2001 From: "CITE\\spapacharalampous" Date: Tue, 27 Aug 2024 17:16:05 +0300 Subject: [PATCH] elastic subqueries --- .../query/NestedReferenceElasticQuery.java | 27 +++++++++++++++++- .../elastic/query/PlanElasticQuery.java | 28 +++++++++++++++++++ .../query/lookup/DescriptionLookup.java | 7 ++++- .../org/opencdmp/query/lookup/PlanLookup.java | 17 ++++++----- .../query/lookup/PlanReferenceLookup.java | 11 ++++++++ 5 files changed, 81 insertions(+), 9 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/elastic/query/NestedReferenceElasticQuery.java b/backend/core/src/main/java/org/opencdmp/elastic/query/NestedReferenceElasticQuery.java index 478c63b44..e4ad7a5b4 100644 --- a/backend/core/src/main/java/org/opencdmp/elastic/query/NestedReferenceElasticQuery.java +++ b/backend/core/src/main/java/org/opencdmp/elastic/query/NestedReferenceElasticQuery.java @@ -18,8 +18,24 @@ import java.util.*; @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class NestedReferenceElasticQuery extends ElasticNestedQuery { + private Collection 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 value) { + this.ids = value; + return this; + } + @Override public NestedReferenceElasticQuery nestedPath(String value) { this.nestedPath = value; @@ -50,7 +66,16 @@ public class NestedReferenceElasticQuery extends ElasticNestedQuery 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 diff --git a/backend/core/src/main/java/org/opencdmp/elastic/query/PlanElasticQuery.java b/backend/core/src/main/java/org/opencdmp/elastic/query/PlanElasticQuery.java index 4a7345063..7ec9a8b64 100644 --- a/backend/core/src/main/java/org/opencdmp/elastic/query/PlanElasticQuery.java +++ b/backend/core/src/main/java/org/opencdmp/elastic/query/PlanElasticQuery.java @@ -47,9 +47,11 @@ public class PlanElasticQuery extends ElasticQuery { private Collection accessTypes; private Collection versions; private Collection groupIds; + private Collection blueprintIds; private NestedCollaboratorElasticQuery planUserSubQuery; private NestedDescriptionElasticQuery descriptionSubQuery; private NestedPlanDescriptionTemplateElasticQuery planDescriptionTemplateSubQuery; + private NestedReferenceElasticQuery referenceSubQuery; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); @@ -67,6 +69,11 @@ public class PlanElasticQuery extends ElasticQuery { 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 { 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 value) { + this.blueprintIds = value; + return this; + } + public PlanElasticQuery authorize(EnumSet values) { this.authorize = values; return this; @@ -297,6 +319,9 @@ public class PlanElasticQuery extends ElasticQuery { 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 { 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); diff --git a/backend/core/src/main/java/org/opencdmp/query/lookup/DescriptionLookup.java b/backend/core/src/main/java/org/opencdmp/query/lookup/DescriptionLookup.java index a19555deb..d2ab224e2 100644 --- a/backend/core/src/main/java/org/opencdmp/query/lookup/DescriptionLookup.java +++ b/backend/core/src/main/java/org/opencdmp/query/lookup/DescriptionLookup.java @@ -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; diff --git a/backend/core/src/main/java/org/opencdmp/query/lookup/PlanLookup.java b/backend/core/src/main/java/org/opencdmp/query/lookup/PlanLookup.java index 7bdd56b3e..de1cb71ae 100644 --- a/backend/core/src/main/java/org/opencdmp/query/lookup/PlanLookup.java +++ b/backend/core/src/main/java/org/opencdmp/query/lookup/PlanLookup.java @@ -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(); } } diff --git a/backend/core/src/main/java/org/opencdmp/query/lookup/PlanReferenceLookup.java b/backend/core/src/main/java/org/opencdmp/query/lookup/PlanReferenceLookup.java index 60de6bf35..e1eff14d5 100644 --- a/backend/core/src/main/java/org/opencdmp/query/lookup/PlanReferenceLookup.java +++ b/backend/core/src/main/java/org/opencdmp/query/lookup/PlanReferenceLookup.java @@ -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; + } }