Home > Database > Spring Boot with Weaviate for vector search

Spring Boot with Weaviate for vector search

# Spring Boot with Weaviate for Vector Search

The world of search is evolving. Traditional keyword-based search systems are giving way to vector search, which utilizes machine learning and semantic understanding to retrieve more relevant results. Weaviate, an open-source vector database, is at the forefront of this revolution. Combining it with Spring Boot, a powerful Java framework for building web applications, enables developers to create efficient, scalable applications with advanced search capabilities.

In this article, we’ll explore how to integrate Spring Boot with Weaviate to implement vector search, complete with explanations, examples, and code snippets.

## What is Weaviate?

Weaviate is a vector database designed for storing and querying high-dimensional vectors generated by machine learning models. It is ideal for tasks such as semantic search, recommendation systems, and natural language processing. Weaviate provides REST and GraphQL APIs for data ingestion, retrieval, and querying.

Key features of Weaviate include:
– **Vector-based search**: Search and retrieve data based on semantic similarity instead of exact matches.
– **Modular machine learning models**: Integrate pre-trained models for automatic vectorization or bring your own.
– **Scalable and extensible**: Built for horizontal scaling and easy integration into applications.

## Why Spring Boot + Weaviate?

Spring Boot simplifies the development of Java-based applications with features like dependency injection, auto-configuration, and embedded web servers. By combining it with Weaviate, developers can build robust applications with advanced vector search capabilities.

### Benefits:
1. **Ease of integration**: Spring Boot’s dependency management and configuration make it easy to integrate APIs like Weaviate’s REST or GraphQL endpoints.
2. **Scalability**: Both Weaviate and Spring Boot are designed to handle large-scale applications and high query loads.
3. **Flexibility**: Spring Boot allows developers to build microservices-based architectures, while Weaviate handles vector-based search efficiently.

## Setting Up Spring Boot and Weaviate

### Prerequisites:
– JDK 8 or higher installed.
– Maven or Gradle for dependency management.
– Docker for running Weaviate locally or on a cloud provider.

### Step 1: Running Weaviate Locally

Weaviate runs as a containerized service. You can use Docker to set it up locally.

“`bash
docker run -d \
-p 8080:8080 \
-e AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=”true” \
-e PERSISTENCE_DATA_PATH=”/var/lib/weaviate” \
-e DEFAULT_VECTORIZER_MODULE=”text2vec-transformers” \
semitechnologies/weaviate:latest
“`

Once the container is running, Weaviate will be accessible on `http://localhost:8080`.

### Step 2: Create a Spring Boot Project

Use Spring Initializr to create a Spring Boot project. Add the following dependencies:
– **Spring Web**: For building REST APIs.
– **Jackson**: For JSON processing.
– **Spring Boot DevTools**: For development convenience.
– **Spring Boot Starter Test**: For testing.

Example Maven dependencies:

“`xml


org.springframework.boot
spring-boot-starter-web


com.fasterxml.jackson.core
jackson-databind


“`

### Step 3: Integrating with Weaviate API

Weaviate provides REST and GraphQL APIs. We’ll use REST APIs to interact with the vector database.

#### Create a Service for Weaviate API Interaction

“`java
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class WeaviateService {
private final RestTemplate restTemplate;
private final String WEAVIATE_URL = “http://localhost:8080/v1”;

public WeaviateService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

public String getSchema() {
String url = WEAVIATE_URL + “/schema”;
return restTemplate.getForObject(url, String.class);
}

public void addData(String className, Object data) {
String url = WEAVIATE_URL + “/objects”;
restTemplate.postForObject(url, data, String.class);
}

public String search(String query) {
String url = WEAVIATE_URL + “/objects/search”;
// Build your search payload
return restTemplate.postForObject(url, query, String.class);
}
}
“`

#### Configure RestTemplate Bean

“`java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
“`

### Step 4: Define a Controller for Handling Requests

Create a simple controller to expose endpoints for interacting with Weaviate.

“`java
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(“/api/weaviate”)
public class WeaviateController {
private final WeaviateService weaviateService;

public WeaviateController(WeaviateService weaviateService) {
this.weaviateService = weaviateService;
}

@GetMapping(“/schema”)
public String getSchema() {
return weaviateService.getSchema();
}

@PostMapping(“/add”)
public void addData(@RequestParam String className, @RequestBody Object data) {
weaviateService.addData(className, data);