Milvus embedding store integration for LangChain4j
—
Complete configuration options for MilvusEmbeddingStore.
MilvusEmbeddingStore.builder()
.host(String host) // default: "localhost"
.port(Integer port) // default: 19530
.username(String username) // optional
.password(String password) // optional
.databaseName(String database) // optional, default: null
.collectionName(String name) // default: "default"
.dimension(Integer dim) // required for new collections
.build();Example:
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
.host("192.168.1.100")
.port(19530)
.username("admin")
.password("password")
.collectionName("embeddings")
.dimension(384)
.build();MilvusEmbeddingStore.builder()
.uri(String uri) // Zilliz Cloud endpoint
.token(String token) // API key
.collectionName(String name)
.dimension(Integer dim)
.build();Example:
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
.uri("https://xxx.api.gcp-us-west1.zillizcloud.com")
.token(System.getenv("ZILLIZ_API_KEY"))
.collectionName("prod_embeddings")
.dimension(1536)
.build();import io.milvus.client.MilvusServiceClient;
import io.milvus.param.ConnectParam;
ConnectParam connectParam = ConnectParam.newBuilder()
.withHost("localhost")
.withPort(19530)
.withAuthorization("", "")
.build();
MilvusServiceClient client = new MilvusServiceClient(connectParam);
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
.milvusClient(client)
.collectionName("my_collection")
.dimension(384)
.build();.collectionName(String name) // Collection name, default: "default"
.dimension(Integer dimension) // Vector dimension, REQUIRED for new collectionsNote: Collection is auto-created if it doesn't exist.
import io.milvus.param.IndexType;
.indexType(IndexType.FLAT) // Exact search (brute force), default
.indexType(IndexType.IVF_FLAT) // Inverted file, balanced
.indexType(IndexType.IVF_SQ8) // IVF with scalar quantization
.indexType(IndexType.IVF_PQ) // IVF with product quantization
.indexType(IndexType.HNSW) // Hierarchical NSW graph, fast
.indexType(IndexType.DISKANN) // Disk-based, very large datasetsSelection Guide:
import io.milvus.param.MetricType;
.metricType(MetricType.COSINE) // Cosine similarity [0,1], default
.metricType(MetricType.L2) // Euclidean distance, lower=similar
.metricType(MetricType.IP) // Inner product, higher=similarSelection Guide:
import io.milvus.common.clientenum.ConsistencyLevelEnum;
.consistencyLevel(ConsistencyLevelEnum.EVENTUALLY) // default, fastest
.consistencyLevel(ConsistencyLevelEnum.BOUNDED) // bounded staleness
.consistencyLevel(ConsistencyLevelEnum.SESSION) // per-session guarantees
.consistencyLevel(ConsistencyLevelEnum.STRONG) // immediate visibilityTrade-offs:
Requirements:
import java.util.Map;
.extraParameters(Map<String, Object> params)HNSW Parameters:
.extraParameters(Map.of(
"efConstruction", 200, // Build-time effort (64-512)
"m", 16 // Connections per node (4-64)
))IVF Parameters:
.extraParameters(Map.of(
"nlist", 1024 // Number of clusters (1-65536)
))IVF_PQ Parameters:
.extraParameters(Map.of(
"m", 8, // Subquantizers (1-65536)
"nlist", 1024 // Number of clusters
))Customize schema field names:
.idFieldName(String name) // default: "id"
.textFieldName(String name) // default: "text"
.metadataFieldName(String name) // default: "metadata"
.vectorFieldName(String name) // default: "vector"Example:
MilvusEmbeddingStore store = MilvusEmbeddingStore.builder()
.host("localhost")
.collectionName("custom_schema")
.dimension(384)
.idFieldName("doc_id")
.textFieldName("content")
.metadataFieldName("properties")
.vectorFieldName("embedding")
.build();.autoFlushOnInsert(Boolean enabled) // default: falseWhen to enable: Single-insert workflows requiring immediate durability
.retrieveEmbeddingsOnSearch(Boolean enabled) // default: falseWhen to enable: Need embedding vectors for post-processing
MilvusEmbeddingStore.builder()
.host("localhost")
.port(19530)
.collectionName("dev_embeddings")
.dimension(384)
.build();import io.milvus.param.IndexType;
import io.milvus.param.MetricType;
import io.milvus.common.clientenum.ConsistencyLevelEnum;
MilvusEmbeddingStore.builder()
.uri(System.getenv("MILVUS_URI"))
.token(System.getenv("MILVUS_TOKEN"))
.collectionName("prod_fast")
.dimension(768)
.indexType(IndexType.HNSW)
.metricType(MetricType.COSINE)
.consistencyLevel(ConsistencyLevelEnum.EVENTUALLY)
.extraParameters(Map.of("efConstruction", 512, "m", 32))
.autoFlushOnInsert(false)
.retrieveEmbeddingsOnSearch(false)
.build();MilvusEmbeddingStore.builder()
.uri(System.getenv("MILVUS_URI"))
.token(System.getenv("MILVUS_TOKEN"))
.collectionName("prod_realtime")
.dimension(1536)
.indexType(IndexType.IVF_FLAT)
.consistencyLevel(ConsistencyLevelEnum.STRONG)
.build();MilvusEmbeddingStore.builder()
.host("milvus-cluster")
.collectionName("large_scale")
.dimension(768)
.indexType(IndexType.IVF_PQ)
.metricType(MetricType.COSINE)
.extraParameters(Map.of("m", 8, "nlist", 2048))
.build();MilvusEmbeddingStore.builder()
.host("localhost")
.databaseName("tenant_db_123")
.collectionName("embeddings")
.dimension(384)
.build();Required Parameters:
dimension - MUST be set when creating new collectionOptional Parameters: All other settings have defaults
Validation at build():
MilvusEmbeddingStore createStore(String env) {
MilvusEmbeddingStore.Builder builder = MilvusEmbeddingStore.builder()
.collectionName("embeddings_" + env)
.dimension(384);
if ("production".equals(env)) {
return builder
.uri(System.getenv("PROD_MILVUS_URI"))
.token(System.getenv("PROD_MILVUS_TOKEN"))
.indexType(IndexType.HNSW)
.consistencyLevel(ConsistencyLevelEnum.STRONG)
.build();
} else {
return builder
.host("localhost")
.port(19530)
.build();
}
}Common Issues:
Missing dimension:
Exception: dimension must be set for new collectionFix: Add .dimension(384) or appropriate value
Connection failed:
Exception: Failed to connect to MilvusFix: Verify host/port or uri/token
Dimension mismatch:
Exception: Embedding dimension doesn't match collectionFix: Ensure embeddings match configured dimension
Invalid index parameters:
Exception: Invalid index parameterFix: Check extraParameters values for chosen index type
Install with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-milvus