Renaming openecomp to onap
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / searchapi / ParsedQuery.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sa.searchdbabstraction.searchapi;
24
25 import com.fasterxml.jackson.annotation.JsonProperty;
26
27 /**
28  * This class represents a simple parsed query statement.
29  *
30  * <p>A 'parsed query' specifies a document field to inspect and a query
31  * string which will be parsed by the document store to generate the
32  * exact query to be performed.
33  *
34  * <p>The query string will be tokenized into 'terms' and 'operators' where:
35  *
36  * <p>Terms may be any of the following:
37  * <ul>
38  * <li> single words </li>
39  * <li> exact phrases (denoted by surrounding the phrase with '"' characters) </li>
40  * <li> regular expressions (denoted by surrounding the phrase with '/' characters) </li>
41  * </ul>
42  *
43  * <p>Operators may be any of the following:
44  * <ul>
45  * <li> +   -- The term to the right of the operator MUST be present to produce a match. </li>
46  * <li> -   -- The term to the right of the operator MUST NOT be present to produce a match. </li>
47  * <li> AND -- Both the terms to the left and right of the operator MUST be present to produce a match. </li>
48  * <li> OR  -- Either the term to the left or right of the operator MUST be present to produce a match. </li>
49  * <li> NOT -- The term to the right of the operator MUST NOT be present to produce a match. </li>
50  * </ul>
51  *
52  * <p>The expected JSON structure for a parsed query is as follows:
53  * <pre>
54  *     {
55  *         "parsed-query": {
56  *             "field": "fieldname",
57  *             "query-string": "string"
58  *         }
59  *     }
60  * </pre>
61  */
62 public class ParsedQuery {
63
64   /**
65    * The name of the field which the query is to be applied to.
66    */
67   private String field;
68
69   /**
70    * The string to be parsed to generate the full query.
71    */
72   @JsonProperty("query-string")
73   private String queryString;
74
75
76   public String getField() {
77     return field;
78   }
79
80   public void setField(String field) {
81     this.field = field;
82   }
83
84   public String getQueryString() {
85     return queryString;
86   }
87
88   public void setQueryString(String queryString) {
89     this.queryString = queryString;
90   }
91
92
93   /**
94    * This method returns a string which represents this query in syntax
95    * that is understandable by ElasticSearch and is suitable for inclusion
96    * in an ElasticSearch query string.
97    *
98    * @return - ElasticSearch syntax string.
99    */
100   public String toElasticSearch() {
101
102     StringBuilder sb = new StringBuilder();
103
104     sb.append("{");
105     sb.append("\"query_string\": {");
106     sb.append("\"default_field\": \"").append(field).append("\", ");
107     sb.append("\"query\": \"").append(queryString).append("\"");
108     sb.append("}");
109     sb.append("}");
110
111     return sb.toString();
112   }
113
114   @Override
115   public String toString() {
116     return "{field:" + field + ", query-string: '" + queryString + "'}";
117   }
118 }