Class Document

java.lang.Object
com.tnsai.knowledge.Document

public final class Document extends Object
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:
  • Method Details

    • of

      public static Document of(String content)
      Creates a simple document with just content.
      Parameters:
      content - the document content
      Returns:
      a new document
    • of

      public static Document of(String content, String key, Object value)
      Creates a document with content and a single metadata entry.
      Parameters:
      content - the document content
      key - metadata key
      value - metadata value
      Returns:
      a new document
    • builder

      public static Document.Builder builder()
      Creates a new document builder.
      Returns:
      a new builder
    • getId

      public String getId()
      Gets the unique document ID.
      Returns:
      the document ID
    • getContent

      public String getContent()
      Gets the document content.
      Returns:
      the content text
    • getSpec

      public Map<String,Object> getSpec()
      Gets the document metadata.
      Returns:
      unmodifiable metadata map
    • getSpec

      public Object getSpec(String key)
      Gets a specific metadata value.
      Parameters:
      key - the metadata key
      Returns:
      the value, or null if not present
    • getSpec

      public <T> T getSpec(String key, Class<T> type)
      Gets a metadata value with type casting.
      Type Parameters:
      T - the type
      Parameters:
      key - the metadata key
      type - 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

      public Document withEmbedding(float[] embedding)
      Creates a copy of this document with a new embedding.
      Parameters:
      embedding - the new embedding
      Returns:
      a new document with the embedding
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object