Skip to content

Commit 2f7dd73

Browse files
committed
fix(java): ensure successful start regardless of index existence
1 parent 9428260 commit 2f7dd73

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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()

0 commit comments

Comments
 (0)