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

  1. Create Spring boot project. There are couple ways to initialize the spring boot.
    1. Spring initializer https://start.spring.io/
    2. Create a spring project from IDE ( IntelliJ or Eclipse).
  2. Add Spring JPA dependency
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>   
    
  3. 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);
    }
    
  4. 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