From 3bc1953e72c91d12ef4b300dea386ac663ad6212 Mon Sep 17 00:00:00 2001 From: Esmaeil Chitgar <84287711+EsmaeilChitgar@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:52:26 +0330 Subject: [PATCH] Add ui you can connect to ui with the following address: http://localhost:8080/search --- pom.xml | 6 +- .../controller/UIController.java | 30 +++++++ .../controller/UserController.java | 13 ++- .../model/Product.java | 38 +++++++++ src/main/resources/application.properties | 3 +- src/main/resources/templates/search.html | 79 +++++++++++++++++++ 6 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/thelivelock/elasticsearch_autocomplete/controller/UIController.java create mode 100644 src/main/java/com/thelivelock/elasticsearch_autocomplete/model/Product.java create mode 100644 src/main/resources/templates/search.html diff --git a/pom.xml b/pom.xml index e60ee8b..34c0541 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,10 @@ org.springframework.boot spring-boot-starter-data-elasticsearch + + org.springframework.boot + spring-boot-starter-thymeleaf + org.springframework.boot spring-boot-starter-web @@ -31,7 +35,7 @@ org.projectlombok lombok - 1.18.10 + 1.18.28 provided diff --git a/src/main/java/com/thelivelock/elasticsearch_autocomplete/controller/UIController.java b/src/main/java/com/thelivelock/elasticsearch_autocomplete/controller/UIController.java new file mode 100644 index 0000000..e3512e3 --- /dev/null +++ b/src/main/java/com/thelivelock/elasticsearch_autocomplete/controller/UIController.java @@ -0,0 +1,30 @@ +package com.thelivelock.elasticsearch_autocomplete.controller; + +import com.thelivelock.elasticsearch_autocomplete.model.User; +import com.thelivelock.elasticsearch_autocomplete.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +import java.util.List; +import java.util.stream.Stream; + +@Controller +public class UIController { + private UserService userService; + + @Autowired + public UIController(UserService userService) { + this.userService = userService; + } + + //when reload localhost:8080/search + @GetMapping("/search") + public String home(Model model) { + List users = this.userService.search(""); + List names = users.stream().flatMap(user -> Stream.of(user.getCountry())).toList(); + model.addAttribute("names", names); + return "search"; + } +} diff --git a/src/main/java/com/thelivelock/elasticsearch_autocomplete/controller/UserController.java b/src/main/java/com/thelivelock/elasticsearch_autocomplete/controller/UserController.java index a603ce8..9c9e8ac 100644 --- a/src/main/java/com/thelivelock/elasticsearch_autocomplete/controller/UserController.java +++ b/src/main/java/com/thelivelock/elasticsearch_autocomplete/controller/UserController.java @@ -7,13 +7,14 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; +import java.util.stream.Stream; @RestController @RequestMapping("/users") public class UserController { - @Autowired private UserService userService; @@ -22,9 +23,17 @@ public List getAllUsers() { return this.userService.listAll(); } - @GetMapping(path = "/search") + @GetMapping(path = "/query") + @ResponseBody public List searchUsers(@RequestParam String keywords) { return this.userService.search(keywords); } + @GetMapping("/suggestion") + @ResponseBody + public List fetchSuggestions(@RequestParam(value = "q", required = false) String query) { + List users = this.userService.search(query); + List names = users.stream().flatMap(user-> Stream.of(user.getCountry())).toList(); + return names; + } } diff --git a/src/main/java/com/thelivelock/elasticsearch_autocomplete/model/Product.java b/src/main/java/com/thelivelock/elasticsearch_autocomplete/model/Product.java new file mode 100644 index 0000000..c4c7f15 --- /dev/null +++ b/src/main/java/com/thelivelock/elasticsearch_autocomplete/model/Product.java @@ -0,0 +1,38 @@ +package com.thelivelock.elasticsearch_autocomplete.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Document(indexName = "productindex") +public class Product { + @Id + private String id; + + @Field(type = FieldType.Text, name = "name") + private String name; + + @Field(type = FieldType.Double, name = "price") + private Double price; + + @Field(type = FieldType.Integer, name = "quantity") + private Integer quantity; + + @Field(type = FieldType.Keyword, name = "category") + private String category; + + @Field(type = FieldType.Text, name = "desc") + private String description; + + @Field(type = FieldType.Keyword, name = "manufacturer") + private String manufacturer; +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 514cc18..4f58229 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,2 @@ # Elasticsearch config -spring.data.elasticsearch.cluster-nodes=localhost:9300 -spring.data.elasticsearch.cluster-name=elasticsearch \ No newline at end of file +spring.elasticsearch.rest.uris=http://localhost:9200 diff --git a/src/main/resources/templates/search.html b/src/main/resources/templates/search.html new file mode 100644 index 0000000..9313cad --- /dev/null +++ b/src/main/resources/templates/search.html @@ -0,0 +1,79 @@ + + + + User Search + + + + + + + + + + + Search Users By Their Country + + + + + Search + + + + + + + + + + + + + + + + \ No newline at end of file
Search Users By Their Country