X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fsa%2Fsearchdbabstraction%2Fsearchapi%2FSearchStatement.java;h=d14f8df73a61b2c663d5a689c50e33055685c706;hb=ecd35f117eb0118f9cbd96fe132a1c359214bfc7;hp=c02133b1ad94196e065f340a8684191605277864;hpb=d6348739c632fb69c8833078effbb902adc7f702;p=aai%2Fsearch-data-service.git diff --git a/src/main/java/org/onap/aai/sa/searchdbabstraction/searchapi/SearchStatement.java b/src/main/java/org/onap/aai/sa/searchdbabstraction/searchapi/SearchStatement.java index c02133b..d14f8df 100644 --- a/src/main/java/org/onap/aai/sa/searchdbabstraction/searchapi/SearchStatement.java +++ b/src/main/java/org/onap/aai/sa/searchdbabstraction/searchapi/SearchStatement.java @@ -21,18 +21,20 @@ package org.onap.aai.sa.searchdbabstraction.searchapi; import com.fasterxml.jackson.annotation.JsonProperty; -import org.radeox.util.logging.Logger; - import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import org.radeox.util.logging.Logger; /** * This class represents the structure of a search statement. * - *
The expected JSON structure to represent a search statement is as follows: + *
+ * The expected JSON structure to represent a search statement is as follows: * - *
+ *+ * + *
* { * "results-start": int, - Optional: index of starting point in result set. * "results-size": int, - Optional: maximum number of documents to include in result set. @@ -61,261 +63,257 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class SearchStatement { - /** - * Defines the filters that should be applied before running the - * actual queries. This is optional. - */ - private Filter filter; + /** + * Defines the filters that should be applied before running the actual queries. This is optional. + */ + private Filter filter; - /** - * The list of queries to be applied to the document store. - */ - private Query[] queries; - - /** - * The list of aggregations to be applied to the search - */ - private Aggregation[] aggregations; - - /** - * Defines the sort criteria to apply to the query result set. - * This is optional. - */ - private Sort sort; - - @JsonProperty("results-start") - private Integer resultsStart; - - @JsonProperty("results-size") - private Integer size; - - public Filter getFilter() { - return filter; - } - - public void setFilter(Filter filter) { - this.filter = filter; - } - - public Query[] getQueries() { - return queries; - } - - public void setQueries(Query[] queries) { - this.queries = queries; - } - - public Sort getSort() { - return sort; - } - - public void setSort(Sort sort) { - this.sort = sort; - } - - public boolean isFiltered() { - return filter != null; - } - - public Aggregation[] getAggregations() { - return aggregations; - } - - public void setAggregations(Aggregation[] aggregations) { - this.aggregations = aggregations; - } - - public boolean hasAggregations() { - return aggregations != null && aggregations.length > 0; - } - - public Integer getFrom() { - return resultsStart; - } - - public void setFrom(Integer from) { - this.resultsStart = from; - } - - public Integer getSize() { - return size; - } - - public void setSize(Integer size) { - this.size = size; - } - - /** - * This method returns a string which represents this statement in syntax - * that is understandable by ElasticSearch and is suitable for inclusion - * in an ElasticSearch query string. - * - * @return - ElasticSearch syntax string. - */ - public String toElasticSearch() { - - StringBuilder sb = new StringBuilder(); - ListnotMatchQueries = new ArrayList (); - List mustQueries = new ArrayList (); - List shouldQueries = new ArrayList (); - - createQueryLists(queries, mustQueries, shouldQueries, notMatchQueries); - - sb.append("{"); - - sb.append("\"version\": true,"); - - // If the client has specified an index into the results for the first - // document in the result set then include that in the ElasticSearch - // query. - if (resultsStart != null) { - sb.append("\"from\": ").append(resultsStart).append(", "); - } + /** + * The list of queries to be applied to the document store. + */ + private Query[] queries; + + /** + * The list of aggregations to be applied to the search + */ + private Aggregation[] aggregations; - // If the client has specified a maximum number of documents to be returned - // in the result set then include that in the ElasticSearch query. - if (size != null) { - sb.append("\"size\": ").append(size).append(", "); + /** + * Defines the sort criteria to apply to the query result set. This is optional. + */ + private Sort sort; + + @JsonProperty("results-start") + private Integer resultsStart; + + @JsonProperty("results-size") + private Integer size; + + public Filter getFilter() { + return filter; } - sb.append("\"query\": {"); - sb.append("\"bool\": {"); + public void setFilter(Filter filter) { + this.filter = filter; + } - sb.append("\"must\": ["); - AtomicBoolean firstQuery = new AtomicBoolean(true); - for (QueryStatement query : mustQueries) { + public Query[] getQueries() { + return queries; + } - if (!firstQuery.compareAndSet(true, false)) { - sb.append(", "); - } + public void setQueries(Query[] queries) { + this.queries = queries; + } - sb.append(query.toElasticSearch()); + public Sort getSort() { + return sort; } - sb.append("], "); - sb.append("\"should\": ["); + public void setSort(Sort sort) { + this.sort = sort; + } - firstQuery = new AtomicBoolean(true); - for (QueryStatement query : shouldQueries) { + public boolean isFiltered() { + return filter != null; + } - if (!firstQuery.compareAndSet(true, false)) { - sb.append(", "); - } + public Aggregation[] getAggregations() { + return aggregations; + } - sb.append(query.toElasticSearch()); + public void setAggregations(Aggregation[] aggregations) { + this.aggregations = aggregations; } - sb.append("],"); // close should list + public boolean hasAggregations() { + return aggregations != null && aggregations.length > 0; + } - sb.append("\"must_not\": ["); - firstQuery.set(true); - for (QueryStatement query : notMatchQueries) { - sb.append(query.toElasticSearch()); + public Integer getFrom() { + return resultsStart; } - sb.append("]"); - // Add the filter stanza, if one is required. - if (isFiltered()) { - sb.append(", \"filter\": ").append(filter.toElasticSearch()); + public void setFrom(Integer from) { + this.resultsStart = from; } - sb.append("}"); // close bool clause - sb.append("}"); // close query clause + public Integer getSize() { + return size; + } - // Add the sort directive, if one is required. - if (sort != null) { - sb.append(", \"sort\": ").append(sort.toElasticSearch()); + public void setSize(Integer size) { + this.size = size; } - // Add aggregations - if (hasAggregations()) { - sb.append(", \"aggs\": {"); + /** + * This method returns a string which represents this statement in syntax that is understandable by ElasticSearch + * and is suitable for inclusion in an ElasticSearch query string. + * + * @return - ElasticSearch syntax string. + */ + public String toElasticSearch() { + StringBuilder sb = new StringBuilder(); + List notMatchQueries = new ArrayList<>(); + List mustQueries = new ArrayList<>(); + List shouldQueries = new ArrayList<>(); + + createQueryLists(queries, mustQueries, shouldQueries, notMatchQueries); + + sb.append("{"); + + sb.append("\"version\": true,"); + + // If the client has specified an index into the results for the first + // document in the result set then include that in the ElasticSearch + // query. + if (resultsStart != null) { + sb.append("\"from\": ").append(resultsStart).append(", "); + } - for (int i = 0; i < aggregations.length; i++) { - if (i > 0) { - sb.append(","); + // If the client has specified a maximum number of documents to be returned + // in the result set then include that in the ElasticSearch query. + if (size != null) { + sb.append("\"size\": ").append(size).append(", "); } - sb.append(aggregations[i].toElasticSearch()); - } - sb.append("}"); - } + sb.append("\"query\": {"); + sb.append("\"bool\": {"); + + sb.append("\"must\": ["); + AtomicBoolean firstQuery = new AtomicBoolean(true); + for (QueryStatement query : mustQueries) { - sb.append("}"); + if (!firstQuery.compareAndSet(true, false)) { + sb.append(", "); + } - Logger.debug("Generated raw ElasticSearch query statement: " + sb.toString()); - return sb.toString(); - } + sb.append(query.toElasticSearch()); + } + sb.append("], "); - private void createQueryLists(Query[] queries, List mustList, - List mayList, List mustNotList) { + sb.append("\"should\": ["); - for (Query query : queries) { + firstQuery = new AtomicBoolean(true); + for (QueryStatement query : shouldQueries) { - if (query.isMust()) { + if (!firstQuery.compareAndSet(true, false)) { + sb.append(", "); + } - if (query.getQueryStatement().isNotMatch()) { - mustNotList.add(query.getQueryStatement()); - } else { - mustList.add(query.getQueryStatement()); + sb.append(query.toElasticSearch()); } - } else { - if (query.getQueryStatement().isNotMatch()) { - mustNotList.add(query.getQueryStatement()); - } else { - mayList.add(query.getQueryStatement()); + sb.append("],"); // close should list + + sb.append("\"must_not\": ["); + firstQuery.set(true); + for (QueryStatement query : notMatchQueries) { + sb.append(query.toElasticSearch()); } - } - } - } + sb.append("]"); + // Add the filter stanza, if one is required. + if (isFiltered()) { + sb.append(", \"filter\": ").append(filter.toElasticSearch()); + } - @Override - public String toString() { + sb.append("}"); // close bool clause + sb.append("}"); // close query clause - StringBuilder sb = new StringBuilder(); + // Add the sort directive, if one is required. + if (sort != null) { + sb.append(", \"sort\": ").append(sort.toElasticSearch()); + } - sb.append("SEARCH STATEMENT: {"); + // Add aggregations + if (hasAggregations()) { + sb.append(", \"aggs\": {"); - if (size != null) { - sb.append("from: ").append(resultsStart).append(", size: ").append(size).append(", "); - } + for (int i = 0; i < aggregations.length; i++) { + if (i > 0) { + sb.append(","); + } + sb.append(aggregations[i].toElasticSearch()); + } + + sb.append("}"); + } + + sb.append("}"); - if (filter != null) { - sb.append("filter: ").append(filter.toString()).append(", "); + Logger.debug("Generated raw ElasticSearch query statement: " + sb.toString()); + return sb.toString(); } - sb.append("queries: ["); - AtomicBoolean firstQuery = new AtomicBoolean(true); - if (queries != null) { - for (Query query : queries) { + private void createQueryLists(Query[] queries, List mustList, List mayList, + List mustNotList) { - if (!firstQuery.compareAndSet(true, false)) { - sb.append(", "); + for (Query query : queries) { + + if (query.isMust()) { + + if (query.getQueryStatement().isNotMatch()) { + mustNotList.add(query.getQueryStatement()); + } else { + mustList.add(query.getQueryStatement()); + } + } else { + + if (query.getQueryStatement().isNotMatch()) { + mustNotList.add(query.getQueryStatement()); + } else { + mayList.add(query.getQueryStatement()); + } + } } - sb.append(query.toString()); - } } - sb.append("]"); - sb.append("aggregations: ["); - firstQuery = new AtomicBoolean(true); - if (aggregations != null) { - for (Aggregation agg : aggregations) { + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + + sb.append("SEARCH STATEMENT: {"); - if (!firstQuery.compareAndSet(true, false)) { - sb.append(", "); + if (size != null) { + sb.append("from: ").append(resultsStart).append(", size: ").append(size).append(", "); } - sb.append(agg.toString()); - } - } - sb.append("]"); - sb.append("]}"); + if (filter != null) { + sb.append("filter: ").append(filter.toString()).append(", "); + } + + sb.append("queries: ["); + AtomicBoolean firstQuery = new AtomicBoolean(true); + if (queries != null) { + for (Query query : queries) { + + if (!firstQuery.compareAndSet(true, false)) { + sb.append(", "); + } + sb.append(query.toString()); + } + } + sb.append("]"); + + sb.append("aggregations: ["); + firstQuery = new AtomicBoolean(true); - return sb.toString(); - } + if (aggregations != null) { + for (Aggregation agg : aggregations) { + + if (!firstQuery.compareAndSet(true, false)) { + sb.append(", "); + } + sb.append(agg.toString()); + } + } + sb.append("]"); + + sb.append("]}"); + + return sb.toString(); + } }