Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Elasticsearch usage for wildcard search functionality in … #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions spring-boot-elasticsearch/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.neo</groupId>
<artifactId>spring-boot-elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-elasticsearch</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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<Product> findByProductName(@RequestParam String name)
{
return productService.findByProductName(name);

}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Product,Integer> {

@Query("{\"wildcard\": {\"name\": \"*?0*\"}}")
List<Product> findByNameWildcard(String name);
}
Original file line number Diff line number Diff line change
@@ -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<Product> findByProductName(String name)
{
return productRepository.findByNameWildcard(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=spring-boot-elasticsearch
Original file line number Diff line number Diff line change
@@ -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() {
}

}