Postgresql Full Text Search with Spring Boot - Part 2
Spring Boot integration with Postgresql Full Text Search
In the previous blog (POSTGRESQL Full Text Search with Spring Boot - Part 1) we saw the basic setup of full text search in postgresql. In this we will integrate the full text with spring boot using spring jpa.
Prerequisite
- Postgresql
- Spring Boot
Integration
- Create Spring boot project. There are couple ways to initialize the spring boot.
- Spring initializer https://start.spring.io/
- Create a spring project from IDE ( IntelliJ or Eclipse).
- Add Spring JPA dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
- Create repository for Product.
@Repository public interface ProductRepository extends JpaRepository<Product, UUID> { @Query(value = "select * from product where to_tsvector('english', name) @@ to_tsquery('english', :query) " + "or to_tsvector('english', description) @@ to_tsquery('english', :query)", nativeQuery = true) List<Product> searchProduct(String query); }
- SearchProduct method will return the result using the indexes created in the previous part.
Conclusion
Assuming that our connections are set up with Postgresql, this way we can query the full text indexes in postgresql from spring boot