diff --git a/spring-boot-elasticsearch/pom.xml b/spring-boot-elasticsearch/pom.xml new file mode 100644 index 000000000..f943b7016 --- /dev/null +++ b/spring-boot-elasticsearch/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.4 + + + com.neo + spring-boot-elasticsearch + 0.0.1-SNAPSHOT + spring-boot-elasticsearch + Demo project for Spring Boot + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter-data-elasticsearch + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/SpringBootElasticsearchApplication.java b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/SpringBootElasticsearchApplication.java new file mode 100644 index 000000000..af90a9c4d --- /dev/null +++ b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/SpringBootElasticsearchApplication.java @@ -0,0 +1,15 @@ +package com.neo.spring_boot_elasticsearch; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; + +@SpringBootApplication +@EnableElasticsearchRepositories +public class SpringBootElasticsearchApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootElasticsearchApplication.class, args); + } + +} diff --git a/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/controller/ProductController.java b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/controller/ProductController.java new file mode 100644 index 000000000..8dec13a23 --- /dev/null +++ b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/controller/ProductController.java @@ -0,0 +1,29 @@ +package com.neo.spring_boot_elasticsearch.controller; + +import com.neo.spring_boot_elasticsearch.models.Product; +import com.neo.spring_boot_elasticsearch.service.ProductService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/api/product") +public class ProductController { + + + private final ProductService productService; + + public ProductController(ProductService productService) { + this.productService = productService; + } + + @GetMapping("/findByProductName/{name}") + public List findByProductName(@RequestParam String name) + { + return productService.findByProductName(name); + + } +} diff --git a/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/models/Product.java b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/models/Product.java new file mode 100644 index 000000000..44d649417 --- /dev/null +++ b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/models/Product.java @@ -0,0 +1,28 @@ +package com.neo.spring_boot_elasticsearch.models; + +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; + +@Document(indexName = "products") +public class Product { + + @Id + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/repository/ProductRepository.java b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/repository/ProductRepository.java new file mode 100644 index 000000000..6a69dbbf7 --- /dev/null +++ b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/repository/ProductRepository.java @@ -0,0 +1,13 @@ +package com.neo.spring_boot_elasticsearch.repository; + +import com.neo.spring_boot_elasticsearch.models.Product; +import org.springframework.data.elasticsearch.annotations.Query; +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; + +import java.util.List; + +public interface ProductRepository extends ElasticsearchRepository { + + @Query("{\"wildcard\": {\"name\": \"*?0*\"}}") + List findByNameWildcard(String name); +} diff --git a/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/service/ProductService.java b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/service/ProductService.java new file mode 100644 index 000000000..f96d035d8 --- /dev/null +++ b/spring-boot-elasticsearch/src/main/java/com/neo/spring_boot_elasticsearch/service/ProductService.java @@ -0,0 +1,20 @@ +package com.neo.spring_boot_elasticsearch.service; + +import com.neo.spring_boot_elasticsearch.models.Product; +import com.neo.spring_boot_elasticsearch.repository.ProductRepository; + +import java.util.List; + +public class ProductService { + + private final ProductRepository productRepository; + + public ProductService(ProductRepository productRepository) { + this.productRepository = productRepository; + } + + public List findByProductName(String name) + { + return productRepository.findByNameWildcard(name); + } +} diff --git a/spring-boot-elasticsearch/src/main/resources/application.properties b/spring-boot-elasticsearch/src/main/resources/application.properties new file mode 100644 index 000000000..d957fb15a --- /dev/null +++ b/spring-boot-elasticsearch/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=spring-boot-elasticsearch diff --git a/spring-boot-elasticsearch/src/test/java/com/neo/spring_boot_elasticsearch/SpringBootElasticsearchApplicationTests.java b/spring-boot-elasticsearch/src/test/java/com/neo/spring_boot_elasticsearch/SpringBootElasticsearchApplicationTests.java new file mode 100644 index 000000000..523ab0296 --- /dev/null +++ b/spring-boot-elasticsearch/src/test/java/com/neo/spring_boot_elasticsearch/SpringBootElasticsearchApplicationTests.java @@ -0,0 +1,13 @@ +package com.neo.spring_boot_elasticsearch; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringBootElasticsearchApplicationTests { + + @Test + void contextLoads() { + } + +}