1adf0dba5c8b8d18dd3441f11689018f4d6a54fe
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
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  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.controller.sample;
39
40 import java.io.IOException;
41
42 import org.json.JSONObject;
43 import org.onap.portalapp.model.Result;
44 import org.onap.portalsdk.core.controller.RestrictedBaseController;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.MediaType;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.web.bind.annotation.PathVariable;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestMethod;
51 import org.springframework.web.bind.annotation.RestController;
52 import org.springframework.web.servlet.ModelAndView;
53
54 import io.searchbox.client.JestClient;
55 import io.searchbox.client.JestClientFactory;
56 import io.searchbox.client.config.HttpClientConfig;
57 import io.searchbox.core.Search;
58 import io.searchbox.core.SearchResult;
59 import io.searchbox.core.Suggest;
60 import io.searchbox.core.SuggestResult;
61 import io.searchbox.params.Parameters;
62
63 /**
64  * Controller for views that demonstrate Elastic Search features.
65  */
66 @RestController
67 public class ElasticSearchController extends RestrictedBaseController{
68          
69         @RequestMapping(value = {"/es_search_demo" }, method = RequestMethod.GET)
70         public ModelAndView search() {
71                 return new ModelAndView("es_search_demo");      
72         }
73         
74         @RequestMapping(value = {"/es_suggest_demo" }, method = RequestMethod.GET)
75         public ModelAndView suggest() {
76                 return new ModelAndView("es_suggest_demo");     
77         }
78         
79         @RequestMapping(value="/es_suggest/{task}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)     
80         public ResponseEntity<Result> doSuggest(@PathVariable("task") String task) throws IOException {
81                 JSONObject obj = new JSONObject(task);
82                 String searchTerm = obj.getString("data");
83                 String searchSize = obj.getString("size");
84                 String searchFuzzy = obj.getString("fuzzy");
85                 String resultName = obj.getString("resultname");
86
87                 JestClientFactory factory = new JestClientFactory();
88                 factory.setHttpClientConfig(new HttpClientConfig
89                         .Builder("http://todo_elastic_search_server")
90                     .multiThreaded(true)
91                     .build());
92                 JestClient client = factory.getObject();
93                 
94                 
95                 Suggest suggest = new Suggest.Builder("{\n"
96                         +"\"" + resultName +"\" : {\n"
97                                 +"\"text\" : \""+ searchTerm +"\",\n"
98                                 +"\"completion\" : {\n"
99                                 +"\"field\" : \"suggest\",\n"
100                                 +"\"size\" : " + searchSize + ",\n"
101                                 +"\"fuzzy\" : \"" + searchFuzzy + "\"\n"
102                                 +"}\n"
103                 +"}\n"
104                 +"}").addIndex("customer").build();
105                 
106                 SuggestResult result = client.execute(suggest);
107                 System.err.println(result.getJsonObject().toString());
108                 return new ResponseEntity<Result>(new Result(result.getJsonObject().toString()),HttpStatus.OK);
109         }
110         
111         @RequestMapping(value="/es_search/{task}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)      
112         public ResponseEntity<Result> doSearch(@PathVariable("task") String task) throws IOException {
113                 JSONObject obj = new JSONObject(task);
114                 String searchTerm = obj.getString("data");
115                 String searchSize = obj.getString("size");
116                 // String searchFuzzy = obj.getString("fuzzy");
117                 
118                 JestClientFactory factory = new JestClientFactory();
119                 factory.setHttpClientConfig(new HttpClientConfig
120                         .Builder("http://todo_elastic_search_server")
121                     .multiThreaded(true)
122                     .build());
123                 JestClient client = factory.getObject();
124                 
125                 Search search = new Search.Builder("{\n"
126                         +"\"query\" : {\n"
127                                 +"\"query_string\" : {\n"
128                                         +"\"query\" : \"name:"+ searchTerm +"\"\n"
129                                 +"}\n"
130                 +"}\n"
131                 +"}").addIndex("customer").setParameter(Parameters.SIZE,Integer.valueOf(searchSize)).build();
132                 
133                 SearchResult result = client.execute(search);
134                 System.err.println(result.getJsonObject().toString());
135                 return new ResponseEntity<Result>(new Result(result.getJsonObject().toString()),HttpStatus.OK);
136         }
137         
138         public ResponseEntity<Result> sendResult(Result result) {
139                 return new ResponseEntity<Result>(result, HttpStatus.OK);
140         }
141         
142         @Override
143         public boolean isRESTfulCall() {
144                 return true;
145         }
146 }