e8e3074ede16215e51a33d85124e8ee08c125956
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.sa.searchdbabstraction.searchapi;
22
23 import com.fasterxml.jackson.annotation.JsonProperty;
24
25 /**
26  * This class represents a simple parsed query statement.
27  *
28  * <p>A 'parsed query' specifies a document field to inspect and a query
29  * string which will be parsed by the document store to generate the
30  * exact query to be performed.
31  *
32  * <p>The query string will be tokenized into 'terms' and 'operators' where:
33  *
34  * <p>Terms may be any of the following:
35  * <ul>
36  * <li> single words </li>
37  * <li> exact phrases (denoted by surrounding the phrase with '"' characters) </li>
38  * <li> regular expressions (denoted by surrounding the phrase with '/' characters) </li>
39  * </ul>
40  *
41  * <p>Operators may be any of the following:
42  * <ul>
43  * <li> +   -- The term to the right of the operator MUST be present to produce a match. </li>
44  * <li> -   -- The term to the right of the operator MUST NOT be present to produce a match. </li>
45  * <li> AND -- Both the terms to the left and right of the operator MUST be present to produce a match. </li>
46  * <li> OR  -- Either the term to the left or right of the operator MUST be present to produce a match. </li>
47  * <li> NOT -- The term to the right of the operator MUST NOT be present to produce a match. </li>
48  * </ul>
49  *
50  * <p>The expected JSON structure for a parsed query is as follows:
51  * <pre>
52  *     {
53  *         "parsed-query": {
54  *             "field": "fieldname",
55  *             "query-string": "string"
56  *         }
57  *     }
58  * </pre>
59  */
60 public class ParsedQuery {
61
62   /**
63    * The name of the field which the query is to be applied to.
64    */
65   private String field;
66
67   /**
68    * The string to be parsed to generate the full query.
69    */
70   @JsonProperty("query-string")
71   private String queryString;
72
73
74   public String getField() {
75     return field;
76   }
77
78   public void setField(String field) {
79     this.field = field;
80   }
81
82   public String getQueryString() {
83     return queryString;
84   }
85
86   public void setQueryString(String queryString) {
87     this.queryString = queryString;
88   }
89
90
91   /**
92    * This method returns a string which represents this query in syntax
93    * that is understandable by ElasticSearch and is suitable for inclusion
94    * in an ElasticSearch query string.
95    *
96    * @return - ElasticSearch syntax string.
97    */
98   public String toElasticSearch() {
99
100     StringBuilder sb = new StringBuilder();
101
102     sb.append("{");
103     sb.append("\"query_string\": {");
104     sb.append("\"default_field\": \"").append(field).append("\", ");
105     sb.append("\"query\": \"").append(queryString).append("\"");
106     sb.append("}");
107     sb.append("}");
108
109     return sb.toString();
110   }
111
112   @Override
113   public String toString() {
114     return "{field:" + field + ", query-string: '" + queryString + "'}";
115   }
116 }