2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.controller;
22 import java.io.IOException;
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;
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;
46 * Controller for views that demonstrate Elastic Search features.
49 public class ElasticSearchController extends RestrictedBaseController{
51 @RequestMapping(value = {"/es_search_demo" }, method = RequestMethod.GET)
52 public ModelAndView search() {
53 return new ModelAndView("es_search_demo");
56 @RequestMapping(value = {"/es_suggest_demo" }, method = RequestMethod.GET)
57 public ModelAndView suggest() {
58 return new ModelAndView("es_suggest_demo");
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");
69 JestClientFactory factory = new JestClientFactory();
70 factory.setHttpClientConfig(new HttpClientConfig
71 .Builder("http://todo_elastic_search_server")
74 JestClient client = factory.getObject();
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"
86 +"}").addIndex("customer").build();
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);
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");
100 JestClientFactory factory = new JestClientFactory();
101 factory.setHttpClientConfig(new HttpClientConfig
102 .Builder("http://todo_elastic_search_server")
105 JestClient client = factory.getObject();
107 Search search = new Search.Builder("{\n"
109 +"\"query_string\" : {\n"
110 +"\"query\" : \"name:"+ searchTerm +"\"\n"
113 +"}").addIndex("customer").setParameter(Parameters.SIZE,Integer.valueOf(searchSize)).build();
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);
120 public ResponseEntity<Result> sendResult(Result result) {
121 return new ResponseEntity<Result>(result, HttpStatus.OK);
125 public boolean isRESTfulCall() {