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