{"record":{"id":"fa2e1bb5808a6fe1","repo":"spring-projects/spring-ai","slug":"neither-createindex-nor-ensureindex-method-fou","errorCode":null,"errorMessage":"Neither createIndex() nor ensureIndex() method found on IndexOperations. This may indicate an unsupported Spring Data MongoDB version.","messagePattern":"Neither createIndex\\(\\) nor ensureIndex\\(\\) method found on IndexOperations\\. This may indicate an unsupported Spring Data MongoDB version\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-mongodb/src/main/java/org/springframework/ai/model/chat/memory/repository/mongo/autoconfigure/MongoChatMemoryIndexCreatorAutoConfiguration.java","lineNumber":125,"sourceCode":"\t * @param index the index definition\n\t * @throws IllegalStateException if neither method is available or invocation fails\n\t */\n\tprivate void createIndexSafely(final IndexOperations indexOps, final IndexDefinition index) {\n\t\ttry {\n\t\t\t// Try new API (Spring Data MongoDB 4.5.x+)\n\t\t\tMethod method = IndexOperations.class.getMethod(\"createIndex\", IndexDefinition.class);\n\t\t\tmethod.invoke(indexOps, index);\n\t\t\tlogger.debug(\"Created index using createIndex() method\");\n\t\t}\n\t\tcatch (NoSuchMethodException createIndexNotFound) {\n\t\t\t// Fall back to old API (Spring Data MongoDB 4.2.x - 4.4.x)\n\t\t\ttry {\n\t\t\t\tMethod method = IndexOperations.class.getMethod(\"ensureIndex\", IndexDefinition.class);\n\t\t\t\tmethod.invoke(indexOps, index);\n\t\t\t\tlogger.debug(\"Created index using ensureIndex() method\");\n\t\t\t}\n\t\t\tcatch (NoSuchMethodException ensureIndexNotFound) {\n\t\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\t\"Neither createIndex() nor ensureIndex() method found on IndexOperations. \"\n\t\t\t\t\t\t\t\t+ \"This may indicate an unsupported Spring Data MongoDB version.\",\n\t\t\t\t\t\tensureIndexNotFound);\n\t\t\t}\n\t\t\tcatch (ReflectiveOperationException ex) {\n\t\t\t\tthrow new IllegalStateException(\"Failed to invoke ensureIndex() method\", ex);\n\t\t\t}\n\t\t}\n\t\tcatch (ReflectiveOperationException ex) {\n\t\t\tthrow new IllegalStateException(\"Failed to invoke createIndex() method\", ex);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":107,"sourceCodeEnd":140,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-mongodb/src/main/java/org/springframework/ai/model/chat/memory/repository/mongo/autoconfigure/MongoChatMemoryIndexCreatorAutoConfiguration.java#L107-L140","documentation":"MongoChatMemory's index creator uses reflection on Spring Data MongoDB's IndexOperations, first trying createIndex() and then ensureIndex(). If neither reflective lookup succeeds, it concludes the classpath contains an unsupported Spring Data MongoDB version whose IndexOperations API has changed, and throws this IllegalStateException with the NoSuchMethodException as cause.","triggerScenarios":"createIndexSafely is invoked by createMainIndex/createOrUpdateTtlIndex during bean initialization and both Method lookups (createIndex, then ensureIndex) on IndexOperations.class throw NoSuchMethodException — i.e. no method with signature (IndexDefinition) exists on the resolved IndexOperations class.","commonSituations":"Mixing Spring Boot versions where spring-data-mongodb on the classpath is older or newer than the version Spring AI was built against; dependency conflicts pulling a stripped/proxied IndexOperations; using a custom IndexOperations implementation.","solutions":["Upgrade/align spring-data-mongodb to a version compatible with your Spring AI release (check spring-ai-starter_mongodb dependencies in the BOM).","Run mvn dependency:tree (or gradle dependencies) and resolve version conflicts so only one spring-data-mongodb version is on the classpath.","Pin the Spring Boot/Spring AI BOM versions together so Spring Data MongoDB matches what the auto-configuration expects."],"exampleFix":"// before (pom.xml)\n<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-mongodb</artifactId><version>3.4.0</version></dependency>\n// after\n<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-mongodb</artifactId></dependency> <!-- version managed by Spring Boot BOM -->","handlingStrategy":"validation","validationCode":"try {\n    IndexOperations.class.getMethod(\"ensureIndex\", IndexDefinition.class);\n} catch (NoSuchMethodException e) {\n    throw new IllegalStateException(\"Incompatible spring-data-mongodb on classpath; align versions with the Spring AI BOM\", e);\n}","typeGuard":null,"tryCatchPattern":"try {\n    context.getBean(MongoChatMemoryIndexCreatorAutoConfiguration.class);\n} catch (IllegalStateException e) {\n    logger.error(\"IndexOperations API mismatch: {}\", e.getCause());\n    throw e;\n}","preventionTips":["Import the Spring AI and Spring Boot BOMs together and avoid pinning spring-data-mongodb manually.","Run dependency:tree in CI to detect conflicting spring-data versions.","Upgrade Spring AI and Spring Data MongoDB in lockstep."],"tags":["mongodb","reflection","spring-data","dependency-version"],"backgroundTag":"incompatible-source-type","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}