Skip to content

Commit a576ea6

Browse files
authored
Merge pull request #42 from mongodb/development
Merge development to main to trigger copy utility
2 parents 6feef45 + 35a101b commit a576ea6

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

mflix/server/java-spring/.env.example

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# Replace with your MongoDB Atlas connection string or local MongoDB URI
33
MONGODB_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/sample_mflix?retryWrites=true&w=majority
44

5-
# Voyage AI Configuration
5+
# Optional: Voyage AI Configuration
66
# API key for Voyage AI embedding model (required for Vector Search)
7-
VOYAGE_API_KEY=your_voyage_api_key
7+
# Get your API key from https://www.voyageai.com/
8+
# Uncomment the following line to enable vector search
9+
# VOYAGE_API_KEY=your-api-key
810

911
# Server Configuration
1012
# Port on which the Spring Boot application will run

mflix/server/java-spring/src/main/java/com/mongodb/samplemflix/config/DatabaseVerification.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,24 @@ private void verifyMoviesCollection() {
138138
*/
139139
private void createTextSearchIndex(MongoCollection<Document> moviesCollection) {
140140
try {
141-
// Check if the text search index already exists
142-
boolean indexExists = false;
141+
// Check if any text search index already exists
142+
// MongoDB only allows one text index per collection, so we check for any text index
143+
// not just one with our specific name
144+
boolean textIndexExists = false;
145+
String existingTextIndexName = null;
146+
143147
for (Document index : moviesCollection.listIndexes()) {
144-
if (TEXT_INDEX_NAME.equals(index.getString("name"))) {
145-
indexExists = true;
146-
logger.info("Text search index '{}' already exists", TEXT_INDEX_NAME);
148+
Document key = index.get("key", Document.class);
149+
if (key != null && key.containsKey("_fts")) {
150+
// _fts is the internal field MongoDB uses for text indexes
151+
textIndexExists = true;
152+
existingTextIndexName = index.getString("name");
153+
logger.info("Text search index '{}' already exists on movies collection", existingTextIndexName);
147154
break;
148155
}
149156
}
150157

151-
if (!indexExists) {
158+
if (!textIndexExists) {
152159
// Create compound text index on plot, title, and fullplot fields
153160
// The background option allows the index to be built without blocking other operations
154161
IndexOptions indexOptions = new IndexOptions()

mflix/server/java-spring/src/main/java/com/mongodb/samplemflix/service/MovieServiceImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,9 +819,10 @@ public List<VectorSearchResult> vectorSearchMovies(String query, Integer limit)
819819
}
820820

821821
// Check if Voyage API key is configured
822-
if (voyageApiKey == null || voyageApiKey.trim().isEmpty()) {
822+
if (voyageApiKey == null || voyageApiKey.trim().isEmpty() ||
823+
voyageApiKey.equals("your_voyage_api_key")) {
823824
throw new ValidationException(
824-
"Vector search unavailable: VOYAGE_API_KEY not configured. Please add your API key to the application.properties file"
825+
"Vector search unavailable: VOYAGE_API_KEY not configured. Please add your Voyage AI API key to the .env file"
825826
);
826827
}
827828

@@ -978,6 +979,10 @@ private List<Double> generateVoyageEmbedding(String text, String apiKey) throws
978979

979980
// Check for successful response
980981
if (response.statusCode() != 200) {
982+
// Handle authentication errors specifically
983+
if (response.statusCode() == 401) {
984+
throw new IOException("Invalid Voyage AI API key. Please check your VOYAGE_API_KEY in the .env file");
985+
}
981986
throw new IOException("Voyage AI API returned status code " + response.statusCode() + ": " + response.body());
982987
}
983988

mflix/server/java-spring/src/test/java/com/mongodb/samplemflix/service/MovieServiceTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,16 @@ void testVectorSearchMovies_MissingApiKey() {
745745
assertThrows(ValidationException.class, () -> movieService.vectorSearchMovies("test query", 10));
746746
}
747747

748+
@Test
749+
@DisplayName("Should throw ValidationException when API key is placeholder value in vector search")
750+
void testVectorSearchMovies_PlaceholderApiKey() {
751+
// Arrange
752+
ReflectionTestUtils.setField(movieService, "voyageApiKey", "your_voyage_api_key");
753+
754+
// Act & Assert
755+
assertThrows(ValidationException.class, () -> movieService.vectorSearchMovies("test query", 10));
756+
}
757+
748758
@Test
749759
@DisplayName("Should enforce limit constraints in vector search")
750760
void testVectorSearchMovies_LimitConstraints() {

0 commit comments

Comments
 (0)