Class TikaDocumentReader

java.lang.Object
org.springframework.ai.reader.tika.TikaDocumentReader
All Implemented Interfaces:
Supplier<List<Document>>, DocumentReader

public class TikaDocumentReader extends Object implements 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 Details

    • METADATA_SOURCE

      public static final String METADATA_SOURCE
      Metadata key representing the source of the document.
      See Also:
    • DEFAULT_MAX_CHARACTERS

      public static final int DEFAULT_MAX_CHARACTERS
      The 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

      public TikaDocumentReader(String resourceUrl)
      Constructor initializing the reader with a given resource URL.
      Parameters:
      resourceUrl - URL to the resource
    • TikaDocumentReader

      public TikaDocumentReader(String resourceUrl, ExtractedTextFormatter textFormatter)
      Constructor initializing the reader with a given resource URL and a text formatter.
      Parameters:
      resourceUrl - URL to the resource
      textFormatter - 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 most DEFAULT_MAX_CHARACTERS characters.
      Parameters:
      resource - Resource pointing to the document
      textFormatter - 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 document
      maxCharacters - Maximum number of characters to extract, or -1 to 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 - if maxCharacters is 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 to get(), 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 document
      contentHandler - Handler to manage content extraction
      textFormatter - Formatter for the extracted text
  • Method Details