19bc250179b648386885b0fc6ca0766cadfd3404
[aai/search-data-service.git] / search-data-service / 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>
29  * A 'parsed query' specifies a document field to inspect and a query string which will be parsed by the document store
30  * to generate the exact query to be performed.
31  *
32  * <p>
33  * The query string will be tokenized into 'terms' and 'operators' where:
34  *
35  * <p>
36  * 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>
44  * Operators may be any of the following:
45  * <ul>
46  * <li>+ -- The term to the right of the operator MUST be present to produce a match.</li>
47  * <li>- -- The term to the right of the operator MUST NOT be present to produce a match.</li>
48  * <li>AND -- Both the terms to the left and right of the operator MUST be present to produce a match.</li>
49  * <li>OR -- Either the term to the left or right of the operator MUST be present to produce a match.</li>
50  * <li>NOT -- The term to the right of the operator MUST NOT be present to produce a match.</li>
51  * </ul>
52  *
53  * <p>
54  * The expected JSON structure for a parsed query is as follows:
55  * 
56  * <pre>
57  *     {
58  *         "parsed-query": {
59  *             "field": "fieldname",
60  *             "query-string": "string"
61  *         }
62  *     }
63  * </pre>
64  */
65 public class ParsedQuery {
66
67     /**
68      * The name of the field which the query is to be applied to.
69      */
70     private String field;
71
72     /**
73      * The string to be parsed to generate the full query.
74      */
75     @JsonProperty("query-string")
76     private String queryString;
77
78
79     public String getField() {
80         return field;
81     }
82
83     public void setField(String field) {
84         this.field = field;
85     }
86
87     public String getQueryString() {
88         return queryString;
89     }
90
91     public void setQueryString(String queryString) {
92         this.queryString = queryString;
93     }
94
95
96     /**
97      * This method returns a string which represents this query in syntax that is understandable by ElasticSearch and is
98      * suitable for inclusion 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 }