/springai-mongodb

Primary LanguageJavaApache License 2.0Apache-2.0

springai-mongodb

This project is a Spring Boot application that demonstrates the use of MongoDB for storing and searching vector embeddings. The application provides endpoints to add documents and perform semantic searches.

Prerequisites

Before you start, make sure you have the following installed:

  • Java 21
  • Maven 3.9.6
  • MongoDB M10 Cluster
  • cURL (for testing the API endpoints)

Getting Started

Build the Project

mvn clean install

Run the Application

mvn spring-boot:run

The application should now be running on http://localhost:8080.

Adding Sample Documents

To add sample documents to your MongoDB, use the following cURL command:

curl -X POST http://localhost:8080/api/v1/add-docs \
-H "Content-Type: application/json" \
-d '[
  {
    "content": "In 1950s Los Angeles, three vastly different police officers investigate a series of murders with their own unique methods and motives, ultimately uncovering deep corruption within the LAPD.",
    "metadata": {
      "title": "LA Confidential",
      "year": 1997,
      "genre": "Crime, Drama, Mystery",
      "director": "Curtis Hanson"
    }
  },
  {
    "content": "A mysterious Hollywood stuntman and mechanic moonlights as a getaway driver, but finds himself in trouble when he helps out his neighbor, leading to a series of violent events.",
    "metadata": {
      "title": "Drive",
      "year": 2011,
      "genre": "Crime, Drama, Thriller",
      "director": "Nicolas Winding Refn"
    }
  },
  {
    "content": "A loyal and dedicated Hong Kong Inspector teams up with a reckless and loudmouthed LAPD detective to rescue a kidnapped Chinese consuls daughter, forming an unlikely partnership and battling a dangerous crime syndicate.",
    "metadata": {
      "title": "Rush Hour",
      "year": 1998,
      "genre": "Action, Comedy, Crime",
      "director": "Brett Ratner"
    }
  }
]'

Performing a Search

To perform a semantic search for documents related to "a Chinese cop and an American cop team up to save a kidnapped girl," use the following cURL command:

curl -X GET "http://localhost:8080/api/v1/search?query=chinese+cop+and+american+cop+team+up+to+save+a+kidnapped+girl&topK=5&similarityThreshold=0.5"

API Endpoints

Add Documents

  • URL: /api/v1/add-docs
  • Method: POST
  • Request Body: JSON array of documents with content and metadata

Search Documents

  • URL: /api/v1/search
  • Method: GET
  • Query Parameters:
  • query (string): The search query.
  • topK (int): The number of top results to return.
  • similarityThreshold (double): The similarity threshold for filtering results.

Check Application Status

  • URL: /api/v1/status
  • Method: GET