Class Document
A Document holds either text content or media content, but not both. Text
content is accessed via getText(), and media content via getMedia().
Use isText() to determine which type is present.
Documents are intended to carry data through Spring AI's ETL pipeline — from ingestion through transformation to storage in a vector store.
JSON serialization and deserialization
Document is fully Jackson-serializable. The serialized JSON uses the following
field names:
id— the unique document identifiertext— the text content (null for media documents)media— the media content (null for text documents)metadata— the metadata mapscore— the relevance score, may be null
Deserialization is handled via Document.Builder. JSON round-trips are fully supported: a
serialized Document can be deserialized back to an equal instance.
Example round-trip:
ObjectMapper mapper = JacksonUtils.getDefaultJsonMapper();
Document original = Document.builder()
.id("doc-1")
.text("hello world")
.metadata("source", "example")
.score(0.95)
.build();
String json = mapper.writeValueAsString(original);
// {"id":"doc-1","text":"hello world","media":null,"metadata":{"source":"example"},"score":0.95}
Document restored = mapper.readValue(json, Document.class);
// restored.equals(original) == true
Creating documents
Example of creating a text document:
// Using constructor
Document textDoc = new Document("Sample text content", Map.of("source", "user-input"));
// Using builder (preferred)
Document textDoc = Document.builder()
.text("Sample text content")
.metadata("source", "user-input")
.build();
Example of creating a media document:
// Using constructor
Media imageContent = new Media(MediaType.IMAGE_PNG, new byte[] {...});
Document mediaDoc = new Document(imageContent, Map.of("filename", "sample.png"));
// Using builder (preferred)
Document mediaDoc = Document.builder()
.media(new Media(MediaType.IMAGE_PNG, new byte[] {...}))
.metadata("filename", "sample.png")
.build();
Example of checking content type and accessing content:
if (document.isText()) {
String text = document.getText();
// Process text content
} else {
Media media = document.getMedia();
// Process media content
}
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a text document with a generated ID and empty metadata.Creates a text document with a generated ID and the given metadata.Creates a media document with a generated ID and the given metadata.Creates a text document with an explicit ID and the given metadata.Creates a media document with an explicit ID and the given metadata. -
Method Summary
Modifier and TypeMethodDescriptionstatic Document.Builderbuilder()booleanReturns the content formatter associated with this document.Returns the formatted content of this document usingMetadataMode.ALL, including both text and all metadata entries.getFormattedContent(ContentFormatter formatter, MetadataMode metadataMode) Helper content extractor that uses and externalContentFormatter.getFormattedContent(MetadataMode metadataMode) Returns the formatted content of this document using the givenMetadataMode.getId()Returns the unique identifier for this document.@Nullable MediagetMedia()Returns the document's media content, if any.Returns the metadata associated with this document.@Nullable DoublegetScore()Returns the relevance score associated with this document, if any.@Nullable StringgetText()Returns the document's text content, if any.inthashCode()booleanisText()Determines whether this document contains text or media content.mutate()Returns a newDocument.Builderpre-populated with all fields of this document, allowing selective modification without altering the original.voidsetContentFormatter(ContentFormatter contentFormatter) Replace the document'sContentFormatter.toString()
-
Field Details
-
DEFAULT_CONTENT_FORMATTER
-
-
Constructor Details
-
Document
Creates a text document with a generated ID and empty metadata.- Parameters:
text- the text content of the document
-
Document
Creates a text document with a generated ID and the given metadata.- Parameters:
text- the text content of the documentmetadata- the metadata map; must not be null and must not contain null keys or values
-
Document
Creates a text document with an explicit ID and the given metadata.- Parameters:
id- the unique document identifier; must not be null or emptytext- the text content of the documentmetadata- the metadata map; must not be null and must not contain null keys or values
-
Document
Creates a media document with a generated ID and the given metadata.- Parameters:
media- the media content of the documentmetadata- the metadata map; must not be null and must not contain null keys or values
-
Document
Creates a media document with an explicit ID and the given metadata.- Parameters:
id- the unique document identifier; must not be null or emptymedia- the media content of the documentmetadata- the metadata map; must not be null and must not contain null keys or values
-
-
Method Details
-
builder
-
getId
Returns the unique identifier for this document.This ID is either explicitly provided during document creation or generated using the configured
IdGenerator(defaults toRandomIdGenerator).- Returns:
- the unique identifier of this document
- See Also:
-
getText
Returns the document's text content, if any.- Returns:
- the text content if
isText()is true, null otherwise - See Also:
-
isText
public boolean isText()Determines whether this document contains text or media content.- Returns:
- true if this document contains text content (accessible via
getText()), false if it contains media content (accessible viagetMedia())
-
getMedia
Returns the document's media content, if any. -
getFormattedContent
Returns the formatted content of this document usingMetadataMode.ALL, including both text and all metadata entries.This method is excluded from JSON serialization as it is a computed, ephemeral representation.
- Returns:
- the formatted content string
- See Also:
-
getFormattedContent
Returns the formatted content of this document using the givenMetadataMode.- Parameters:
metadataMode- controls which metadata entries are included in the output; must not be null- Returns:
- the formatted content string
-
getFormattedContent
Helper content extractor that uses and externalContentFormatter. -
getMetadata
Returns the metadata associated with this document.The metadata values are restricted to simple types (string, int, float, boolean) for compatibility with Vector Databases.
- Returns:
- the metadata map
-
getScore
Returns the relevance score associated with this document, if any.The score is typically assigned during retrieval and is not set at document creation time. Use
Document.Builder.score(Double)ormutate()to produce a new document with a score.- Returns:
- the relevance score, or null if none has been assigned
-
getContentFormatter
Returns the content formatter associated with this document.- Returns:
- the current ContentFormatter instance used for formatting the document content.
-
setContentFormatter
Replace the document'sContentFormatter.- Parameters:
contentFormatter- new formatter to use.
-
mutate
Returns a newDocument.Builderpre-populated with all fields of this document, allowing selective modification without altering the original.- Returns:
- a builder initialised from this document's state
-
equals
-
hashCode
public int hashCode() -
toString
-