CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-dev-langchain4j--langchain4j-milvus

Milvus embedding store integration for LangChain4j

Pending
Overview
Eval results
Files

collection-management.mddocs/operations/

Collection Management

Direct collection management operations.

Method

Drop Collection

void dropCollection(String collectionName);

Permanently deletes a Milvus collection and all its data.

Parameters:

  • collectionName: Name of collection to drop

Warning: Permanent deletion, cannot be undone

store.dropCollection("old_collection");

Use Cases

Cleanup After Testing

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);
}

Collection Recreation

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

Multiple Collection Cleanup

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

Environment-Based Cleanup

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);
        }
    }
}

Migration Pattern

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);
}

Important Considerations

1. Data Loss Warning

Dropping permanently deletes all data. No undo.

Always:

  • Verify collection name
  • Back up important data
  • Use caution in production
// 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);
}

2. Production Safety

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);
    }
}

3. Connection Independence

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 careful

4. Error Handling

public 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;
        }
    }
}

5. Automatic Recreation

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

Best Practices

1. Descriptive Names

// Good: Descriptive with purpose and environment
String name = "prod_docs_embeddings_v2";

// Avoid: Generic
String name = "collection1";

2. Naming Conventions

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"

3. Versioning Strategy

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++;

4. Backup Before Dropping

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
}

5. Testing Pattern

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

Advanced Patterns

Multi-Store Management

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

Collection Rotation

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
        }
    }
}

Summary

The dropCollection method provides essential collection lifecycle management:

  • Purpose: Permanently delete collections and data
  • Use Cases: Testing cleanup, migration, environment management
  • Warnings: Data loss is permanent
  • Best Practices: Safeguards, descriptive names, backups

For advanced collection operations (listing, renaming, etc.), use Milvus SDK directly.

Related

  • Adding Embeddings
  • Removing Embeddings
  • Configuration

Install with Tessl CLI

npx tessl i tessl/maven-dev-langchain4j--langchain4j-milvus

docs

operations

adding.md

collection-management.md

removing.md

searching.md

advanced.md

api-reference.md

configuration.md

index.md

patterns.md

quickstart.md

troubleshooting.md

tile.json