Package com.tnsai.knowledge
Class Document
java.lang.Object
com.tnsai.knowledge.Document
Represents a document or document chunk in a knowledge base.
Documents are the fundamental unit of storage in RAG (Retrieval-Augmented Generation) systems. Each document contains:
- Content - the text content of the document
- Spec - arbitrary key-value pairs for filtering and context
- Embedding - optional vector representation for similarity search
Example usage:
// Simple document
Document doc = Document.of("This is the document content");
// Document with metadata
Document doc = Document.builder()
.content("Product documentation...")
.metadata("source", "docs/product.md")
.metadata("category", "documentation")
.build();
// Document with embedding
Document doc = Document.builder()
.content("Some text")
.embedding(embeddingVector)
.build();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creating Document instances. -
Method Summary
Modifier and TypeMethodDescriptionstatic Document.Builderbuilder()Creates a new document builder.booleanGets the document content.float[]Gets the document embedding (vector representation).intGets the embedding dimension.getId()Gets the unique document ID.getSpec()Gets the document metadata.Gets a specific metadata value.<T> TGets a metadata value with type casting.booleanChecks if the document has an embedding.inthashCode()static DocumentCreates a simple document with just content.static DocumentCreates a document with content and a single metadata entry.toString()withEmbedding(float[] embedding) Creates a copy of this document with a new embedding.
-
Method Details
-
of
Creates a simple document with just content.- Parameters:
content- the document content- Returns:
- a new document
-
of
Creates a document with content and a single metadata entry.- Parameters:
content- the document contentkey- metadata keyvalue- metadata value- Returns:
- a new document
-
builder
Creates a new document builder.- Returns:
- a new builder
-
getId
Gets the unique document ID.- Returns:
- the document ID
-
getContent
Gets the document content.- Returns:
- the content text
-
getSpec
Gets the document metadata.- Returns:
- unmodifiable metadata map
-
getSpec
Gets a specific metadata value.- Parameters:
key- the metadata key- Returns:
- the value, or null if not present
-
getSpec
Gets a metadata value with type casting.- Type Parameters:
T- the type- Parameters:
key- the metadata keytype- the expected type- Returns:
- the value, or null if not present or wrong type
-
hasEmbedding
public boolean hasEmbedding()Checks if the document has an embedding.- Returns:
- true if embedding is present
-
getEmbedding
public float[] getEmbedding()Gets the document embedding (vector representation).- Returns:
- copy of the embedding array, or null if not present
-
getEmbeddingDimension
public int getEmbeddingDimension()Gets the embedding dimension.- Returns:
- embedding dimension, or 0 if no embedding
-
withEmbedding
Creates a copy of this document with a new embedding.- Parameters:
embedding- the new embedding- Returns:
- a new document with the embedding
-
equals
-
hashCode
public int hashCode() -
toString
-