-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathElasticSearchController.java
More file actions
104 lines (86 loc) · 4.64 KB
/
Copy pathElasticSearchController.java
File metadata and controls
104 lines (86 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.visualpathit.account.controller;
import java.io.IOException;
import java.util.List;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.visualpathit.account.model.User;
import com.visualpathit.account.service.UserService;
import com.visualpathit.account.utils.ElasticsearchUtil;
@Controller
public class ElasticSearchController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user/elasticsearch", method = RequestMethod.GET)
public String insert(final Model model) throws IOException {
List<User> users = userService.getList();
try (RestHighLevelClient client = ElasticsearchUtil.getRestHighLevelClient()) {
for (User user : users) {
IndexRequest indexRequest = new IndexRequest("users").id(String.valueOf(user.getId()))
.source(XContentFactory.jsonBuilder()
.startObject()
.field("name", user.getUsername())
.field("DOB", user.getDateOfBirth())
.field("fatherName", user.getFatherName())
.field("motherName", user.getMotherName())
.field("gender", user.getGender())
.field("nationality", user.getNationality())
.field("phoneNumber", user.getPhoneNumber())
.endObject());
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
String res = response.getResult().toString();
System.out.println(res);
}
}
model.addAttribute("result", "Users indexed successfully");
return "elasticeSearchRes";
}
@RequestMapping(value = "/rest/users/view/{id}", method = RequestMethod.GET)
public String view(@PathVariable final String id, final Model model) throws IOException {
try (RestHighLevelClient client = ElasticsearchUtil.getRestHighLevelClient()) {
GetRequest getRequest = new GetRequest("users", id);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString());
model.addAttribute("res", getResponse.getSource().get("name"));
}
return "elasticeSearchRes";
}
@RequestMapping(value = "/rest/users/update/{id}", method = RequestMethod.GET)
public String update(@PathVariable final String id, final Model model) throws IOException {
try (RestHighLevelClient client = ElasticsearchUtil.getRestHighLevelClient()) {
UpdateRequest updateRequest = new UpdateRequest("users", id)
.doc(XContentFactory.jsonBuilder()
.startObject()
.field("gender", "male")
.endObject());
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
model.addAttribute("res", updateResponse.status());
}
return "elasticeSearchRes";
}
@RequestMapping(value = "/rest/users/delete/{id}", method = RequestMethod.GET)
public String delete(@PathVariable final String id, final Model model) throws IOException {
try (RestHighLevelClient client = ElasticsearchUtil.getRestHighLevelClient()) {
DeleteRequest deleteRequest = new DeleteRequest("users", id);
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.getResult().toString());
model.addAttribute("res", deleteResponse.getResult().toString());
}
return "elasticeSearchRes";
}
}