-
Notifications
You must be signed in to change notification settings - Fork 15
/
SearchResource.java
executable file
·52 lines (38 loc) · 1.45 KB
/
SearchResource.java
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
package com.techprimers.elastic.resource;
import com.techprimers.elastic.model.Users;
import com.techprimers.elastic.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/rest/search")
public class SearchResource {
@Autowired
UsersRepository usersRepository;
@Autowired
ElasticsearchTemplate template;
@GetMapping(value = "/name/{text}")
public List<Users> searchName(@PathVariable final String text) {
return usersRepository.findByName(text);
}
@GetMapping(value = "/salary/{salary}")
public List<Users> searchSalary(@PathVariable final Long salary) {
return usersRepository.findBySalary(salary);
}
@GetMapping(value = "/all")
public List<Users> searchAll() {
List<Users> usersList = new ArrayList<>();
Iterable<Users> userses = usersRepository.findAll();
userses.forEach(usersList::add);
return usersList;
}
@GetMapping(value = "/delete")
public boolean delete() {
return template.deleteIndex(Users.class);
}
}