Class TikaDocumentReader
java.lang.Object
org.springframework.ai.reader.tika.TikaDocumentReader
- All Implemented Interfaces:
Supplier<List<Document>>,DocumentReader
A document reader that leverages Apache Tika to extract text from a variety of document
formats, such as PDF, DOC/DOCX, PPT/PPTX, and HTML. For a comprehensive list of
supported formats, refer to: https://tika.apache.org/docs/4.0.x/formats.html.
This reader directly provides the extracted text without any additional formatting. All
extracted texts are encapsulated within a
Document instance.
If you require more specialized handling for PDFs, consider using the
PagePdfDocumentReader or ParagraphPdfDocumentReader.
The extracted text is held in memory, so by default this reader extracts at most
DEFAULT_MAX_CHARACTERS characters per document and fails if a document exceeds
that limit. Use TikaDocumentReader(Resource, int, ExtractedTextFormatter) to
configure a different limit when reading documents from trusted sources.- Author:
- Christian Tzolov, ikaitist, Dimitar Proynov
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default maximum number of characters extracted from a single document.static final StringMetadata key representing the source of the document. -
Constructor Summary
ConstructorsConstructorDescriptionTikaDocumentReader(String resourceUrl) Constructor initializing the reader with a given resource URL.TikaDocumentReader(String resourceUrl, ExtractedTextFormatter textFormatter) Constructor initializing the reader with a given resource URL and a text formatter.TikaDocumentReader(org.springframework.core.io.Resource resource) Constructor initializing the reader with a resource.TikaDocumentReader(org.springframework.core.io.Resource resource, int maxCharacters, ExtractedTextFormatter textFormatter) Constructor initializing the reader with a resource, a maximum number of characters to extract, and a text formatter.TikaDocumentReader(org.springframework.core.io.Resource resource, ExtractedTextFormatter textFormatter) Constructor initializing the reader with a resource and a text formatter.TikaDocumentReader(org.springframework.core.io.Resource resource, ContentHandler contentHandler, ExtractedTextFormatter textFormatter) Constructor initializing the reader with a resource, content handler, and a text formatter. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.document.DocumentReader
read
-
Field Details
-
METADATA_SOURCE
Metadata key representing the source of the document.- See Also:
-
DEFAULT_MAX_CHARACTERS
public static final int DEFAULT_MAX_CHARACTERSThe default maximum number of characters extracted from a single document. Prevents a large or highly expanding document from exhausting the JVM heap. Roughly equivalent to a few thousand pages of text, which is far beyond the size of typical documents.- See Also:
-
-
Constructor Details
-
TikaDocumentReader
Constructor initializing the reader with a given resource URL.- Parameters:
resourceUrl- URL to the resource
-
TikaDocumentReader
Constructor initializing the reader with a given resource URL and a text formatter.- Parameters:
resourceUrl- URL to the resourcetextFormatter- Formatter for the extracted text
-
TikaDocumentReader
public TikaDocumentReader(org.springframework.core.io.Resource resource) Constructor initializing the reader with a resource.- Parameters:
resource- Resource pointing to the document
-
TikaDocumentReader
public TikaDocumentReader(org.springframework.core.io.Resource resource, ExtractedTextFormatter textFormatter) Constructor initializing the reader with a resource and a text formatter. Extracts at mostDEFAULT_MAX_CHARACTERScharacters.- Parameters:
resource- Resource pointing to the documenttextFormatter- Formatter for the extracted text
-
TikaDocumentReader
public TikaDocumentReader(org.springframework.core.io.Resource resource, int maxCharacters, ExtractedTextFormatter textFormatter) Constructor initializing the reader with a resource, a maximum number of characters to extract, and a text formatter.- Parameters:
resource- Resource pointing to the documentmaxCharacters- Maximum number of characters to extract, or-1to disable the limit. Because the extracted text is held in memory, disabling the limit is only safe for documents from trusted sources.textFormatter- Formatter for the extracted text- Throws:
IllegalArgumentException- ifmaxCharactersis lower than-1- Since:
- 2.0.2.1
-
TikaDocumentReader
public TikaDocumentReader(org.springframework.core.io.Resource resource, ContentHandler contentHandler, ExtractedTextFormatter textFormatter) Constructor initializing the reader with a resource, content handler, and a text formatter. The given handler is reused for every call toget(), so it collects the text of each call on top of the previous ones. Any limit on the extracted text is the responsibility of the handler.- Parameters:
resource- Resource pointing to the documentcontentHandler- Handler to manage content extractiontextFormatter- Formatter for the extracted text
-
-
Method Details