Milvus embedding store integration for LangChain4j
—
Direct collection management operations.
void dropCollection(String collectionName);Permanently deletes a Milvus collection and all its data.
Parameters:
collectionName: Name of collection to dropWarning: Permanent deletion, cannot be undone
store.dropCollection("old_collection");String testCollection = "test_" + System.currentTimeMillis();
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName(testCollection)
.dimension(384)
.build();
try {
// Run tests
runTests(store);
} finally {
// Cleanup
store.dropCollection(testCollection);
}try {
store.dropCollection("my_collection");
} catch (Exception e) {
// Collection may not exist, ignore
}
// Create with new configuration
MilvusEmbeddingStore newStore = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName("my_collection")
.dimension(768) // New dimension
.indexType(IndexType.HNSW) // New index
.build();List<String> collectionsToClean = Arrays.asList(
"test_collection_1",
"test_collection_2",
"test_collection_temp"
);
for (String collection : collectionsToClean) {
try {
store.dropCollection(collection);
System.out.println("Dropped: " + collection);
} catch (Exception e) {
System.err.println("Failed to drop " + collection + ": " + e.getMessage());
}
}public void cleanupEnvironment(String environment) {
MilvusEmbeddingStore store = createStore(environment);
// Drop environment-specific collections
// Note: Must track collection names separately
List<String> collections = getCollectionsForEnvironment(environment);
for (String collection : collections) {
if (collection.startsWith(environment + "_")) {
store.dropCollection(collection);
}
}
}String oldCollection = "embeddings_v1";
String newCollection = "embeddings_v2";
// Create new collection
MilvusEmbeddingStore newStore = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName(newCollection)
.dimension(768)
.build();
// Migrate data
migrateData(oldCollection, newCollection);
// Verify and drop old
if (verifyMigration(newCollection)) {
newStore.dropCollection(oldCollection);
}Dropping permanently deletes all data. No undo.
Always:
// Safe pattern with confirmation
public void safeDropCollection(String name, boolean confirmed) {
if (!confirmed) {
throw new IllegalStateException("Drop must be confirmed");
}
System.out.println("Dropping collection: " + name);
store.dropCollection(name);
}public class ProductionSafeManager {
private final String environment;
public void dropCollection(String name) {
if ("production".equals(environment)) {
throw new UnsupportedOperationException(
"Cannot drop collections in production"
);
}
store.dropCollection(name);
}
}Can drop any collection through any store instance:
// Store configured for collection "A"
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName("collectionA")
.dimension(384)
.build();
// Can drop collection "B"
store.dropCollection("collectionB"); // Valid
// Can drop its own collection
store.dropCollection("collectionA"); // Valid but be carefulpublic void dropCollectionSafely(String name) {
try {
store.dropCollection(name);
System.out.println("Dropped: " + name);
} catch (Exception e) {
if (e.getMessage().contains("not found")) {
System.out.println("Collection doesn't exist: " + name);
} else {
System.err.println("Error: " + e.getMessage());
throw e;
}
}
}Collections are auto-created when building store:
// Drop collection
store.dropCollection("my_collection");
// Create new store - collection auto-created
MilvusEmbeddingStore newStore = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName("my_collection")
.dimension(384)
.build();// Good: Descriptive with purpose and environment
String name = "prod_docs_embeddings_v2";
// Avoid: Generic
String name = "collection1";public String buildCollectionName(String env, String purpose, String version) {
return String.format("%s_%s_%s", env, purpose, version);
}
// Usage
String name = buildCollectionName("dev", "customer_docs", "v1");
// Result: "dev_customer_docs_v1"String baseCollection = "embeddings";
int currentVersion = 1;
String oldCollection = baseCollection + "_v" + currentVersion;
String newCollection = baseCollection + "_v" + (currentVersion + 1);
// Create new version
MilvusEmbeddingStore newStore = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName(newCollection)
.dimension(768)
.build();
// Migrate and drop old
migrateData(oldCollection, newCollection);
newStore.dropCollection(oldCollection);
currentVersion++;public void dropWithBackup(String name) {
// Backup first
backupCollection(name);
// Then drop
store.dropCollection(name);
System.out.println("Dropped with backup available");
}
private void backupCollection(String name) {
// Export vectors and metadata to external storage
}import org.junit.jupiter.api.*;
public class CollectionLifecycleTest {
private MilvusEmbeddingStore store;
private String testCollection;
@BeforeEach
public void setup() {
testCollection = "test_" + System.currentTimeMillis();
store = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName(testCollection)
.dimension(384)
.build();
}
@Test
public void testOperations() {
// Test with isolated collection
}
@AfterEach
public void cleanup() {
if (store != null && testCollection != null) {
try {
store.dropCollection(testCollection);
} catch (Exception e) {
System.err.println("Cleanup failed: " + e.getMessage());
}
}
}
}Map<String, MilvusEmbeddingStore> stores = new HashMap<>();
public void createStore(String name, int dimension) {
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName(name)
.dimension(dimension)
.build();
stores.put(name, store);
}
public void dropStore(String name) {
MilvusEmbeddingStore store = stores.get(name);
if (store != null) {
store.dropCollection(name);
stores.remove(name);
}
}
public void dropAllStores() {
for (Map.Entry<String, MilvusEmbeddingStore> entry : stores.entrySet()) {
entry.getValue().dropCollection(entry.getKey());
}
stores.clear();
}public void rotateCollection(String baseName) {
String currentDate = LocalDate.now().toString();
String newCollection = baseName + "_" + currentDate;
// Create new collection
MilvusEmbeddingStore newStore = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName(newCollection)
.dimension(384)
.build();
// Drop old collections (keep last 7 days)
LocalDate cutoff = LocalDate.now().minusDays(7);
for (LocalDate date = cutoff.minusDays(30); date.isBefore(cutoff); date = date.plusDays(1)) {
String oldCollection = baseName + "_" + date.toString();
try {
newStore.dropCollection(oldCollection);
} catch (Exception e) {
// Collection may not exist
}
}
}The dropCollection method provides essential collection lifecycle management:
For advanced collection operations (listing, renaming, etc.), use Milvus SDK directly.
Install with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-milvus