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