aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/sa/searchdbabstraction/searchapi/SearchStatement.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap/aai/sa/searchdbabstraction/searchapi/SearchStatement.java')
-rw-r--r--src/main/java/org/onap/aai/sa/searchdbabstraction/searchapi/SearchStatement.java414
1 files changed, 207 insertions, 207 deletions
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 f008a7c..9f37856 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
@@ -29,9 +29,12 @@ import org.radeox.util.logging.Logger;
/**
* This class represents the structure of a search statement.
*
- * <p>The expected JSON structure to represent a search statement is as follows:
+ * <p>
+ * The expected JSON structure to represent a search statement is as follows:
*
- * <p><pre>
+ * <p>
+ *
+ * <pre>
* {
* "results-start": int, - Optional: index of starting point in result set.
* "results-size": int, - Optional: maximum number of documents to include in result set.
@@ -60,261 +63,258 @@ import org.radeox.util.logging.Logger;
*/
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();
- List<QueryStatement> notMatchQueries = new ArrayList<QueryStatement>();
- List<QueryStatement> mustQueries = new ArrayList<QueryStatement>();
- List<QueryStatement> shouldQueries = new ArrayList<QueryStatement>();
-
- 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;
+
+ /**
+ * Defines the sort criteria to apply to the query result set. This is optional.
+ */
+ private Sort sort;
- // 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(", ");
+ @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<QueryStatement> notMatchQueries = new ArrayList<QueryStatement>();
+ List<QueryStatement> mustQueries = new ArrayList<QueryStatement>();
+ List<QueryStatement> shouldQueries = new ArrayList<QueryStatement>();
+
+ createQueryLists(queries, mustQueries, shouldQueries, notMatchQueries);
- for (int i = 0; i < aggregations.length; i++) {
- if (i > 0) {
- sb.append(",");
+ 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(", ");
}
- sb.append(aggregations[i].toElasticSearch());
- }
- 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("\"query\": {");
+ sb.append("\"bool\": {");
+
+ sb.append("\"must\": [");
+ AtomicBoolean firstQuery = new AtomicBoolean(true);
+ for (QueryStatement query : mustQueries) {
+
+ if (!firstQuery.compareAndSet(true, false)) {
+ sb.append(", ");
+ }
+
+ sb.append(query.toElasticSearch());
+ }
+ sb.append("], ");
- sb.append("}");
+ sb.append("\"should\": [");
- Logger.debug("Generated raw ElasticSearch query statement: " + sb.toString());
- return sb.toString();
- }
+ firstQuery = new AtomicBoolean(true);
+ for (QueryStatement query : shouldQueries) {
- private void createQueryLists(Query[] queries, List<QueryStatement> mustList,
- List<QueryStatement> mayList, List<QueryStatement> mustNotList) {
+ if (!firstQuery.compareAndSet(true, false)) {
+ sb.append(", ");
+ }
- for (Query query : queries) {
+ sb.append(query.toElasticSearch());
+ }
- if (query.isMust()) {
+ sb.append("],"); // close should list
- if (query.getQueryStatement().isNotMatch()) {
- mustNotList.add(query.getQueryStatement());
- } else {
- mustList.add(query.getQueryStatement());
+ sb.append("\"must_not\": [");
+ firstQuery.set(true);
+ for (QueryStatement query : notMatchQueries) {
+ sb.append(query.toElasticSearch());
}
- } else {
+ sb.append("]");
- if (query.getQueryStatement().isNotMatch()) {
- mustNotList.add(query.getQueryStatement());
- } else {
- mayList.add(query.getQueryStatement());
+ // Add the filter stanza, if one is required.
+ if (isFiltered()) {
+ sb.append(", \"filter\": ").append(filter.toElasticSearch());
}
- }
- }
- }
+ sb.append("}"); // close bool clause
+ sb.append("}"); // close query clause
- @Override
- public String toString() {
+ // Add the sort directive, if one is required.
+ if (sort != null) {
+ sb.append(", \"sort\": ").append(sort.toElasticSearch());
+ }
- StringBuilder sb = new StringBuilder();
+ // Add aggregations
+ if (hasAggregations()) {
+ sb.append(", \"aggs\": {");
- sb.append("SEARCH STATEMENT: {");
+ for (int i = 0; i < aggregations.length; i++) {
+ if (i > 0) {
+ sb.append(",");
+ }
+ sb.append(aggregations[i].toElasticSearch());
+ }
- if (size != null) {
- sb.append("from: ").append(resultsStart).append(", size: ").append(size).append(", ");
- }
+ sb.append("}");
+ }
- if (filter != null) {
- sb.append("filter: ").append(filter.toString()).append(", ");
+ sb.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<QueryStatement> mustList, List<QueryStatement> mayList,
+ List<QueryStatement> mustNotList) {
+
+ for (Query query : queries) {
+
+ if (query.isMust()) {
- if (!firstQuery.compareAndSet(true, false)) {
- sb.append(", ");
+ 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();
+ }
}