2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller.sample;
40 import java.io.IOException;
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;
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;
64 * Controller for views that demonstrate Elastic Search features.
67 public class ElasticSearchController extends RestrictedBaseController{
69 @RequestMapping(value = {"/es_search_demo" }, method = RequestMethod.GET)
70 public ModelAndView search() {
71 return new ModelAndView("es_search_demo");
74 @RequestMapping(value = {"/es_suggest_demo" }, method = RequestMethod.GET)
75 public ModelAndView suggest() {
76 return new ModelAndView("es_suggest_demo");
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");
87 JestClientFactory factory = new JestClientFactory();
88 factory.setHttpClientConfig(new HttpClientConfig
89 .Builder("http://todo_elastic_search_server")
92 JestClient client = factory.getObject();
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"
104 +"}").addIndex("customer").build();
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);
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");
118 JestClientFactory factory = new JestClientFactory();
119 factory.setHttpClientConfig(new HttpClientConfig
120 .Builder("http://todo_elastic_search_server")
123 JestClient client = factory.getObject();
125 Search search = new Search.Builder("{\n"
127 +"\"query_string\" : {\n"
128 +"\"query\" : \"name:"+ searchTerm +"\"\n"
131 +"}").addIndex("customer").setParameter(Parameters.SIZE,Integer.valueOf(searchSize)).build();
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);
138 public ResponseEntity<Result> sendResult(Result result) {
139 return new ResponseEntity<Result>(result, HttpStatus.OK);
143 public boolean isRESTfulCall() {