Class ApacheTikaDocumentParser.Builder

java.lang.Object
dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser.Builder
Enclosing class:
ApacheTikaDocumentParser

public static final class ApacheTikaDocumentParser.Builder extends Object
Builder for ApacheTikaDocumentParser. Options that are not set (or set to null) use their default values.

Each supplier is called once for every parsed document, so the content handler and metadata suppliers should return a new object on every call: these objects collect the text and the metadata of a single document.

Since:
1.25.0
  • Method Details

    • parserSupplier

      public ApacheTikaDocumentParser.Builder parserSupplier(@Nullable Supplier<org.apache.tika.parser.Parser> parserSupplier)
      Sets the supplier of the Tika Parser that reads the documents. The supplier is called once for every parsed document.
      Parameters:
      parserSupplier - the supplier of the Tika parser; null means the default, an AutoDetectParser
      Returns:
      this builder
      Since:
      1.25.0
    • contentHandlerSupplier

      public ApacheTikaDocumentParser.Builder contentHandlerSupplier(@Nullable Supplier<ContentHandler> contentHandlerSupplier)
      Sets the supplier of the Tika ContentHandler that collects the text of the documents. The supplier is called once for every parsed document, and the text of the document is the toString() of the handler after parsing.

      Your own content handler decides how much text is collected, so this option cannot be used together with maxCharacters(Integer). To limit the text, set the limit on your handler, for example new BodyContentHandler(limit).

      Parameters:
      contentHandlerSupplier - the supplier of the content handler; null means the default, a BodyContentHandler that extracts at most maxCharacters(Integer) characters
      Returns:
      this builder
      Since:
      1.25.0
    • metadataSupplier

      public ApacheTikaDocumentParser.Builder metadataSupplier(@Nullable Supplier<org.apache.tika.metadata.Metadata> metadataSupplier)
      Sets the supplier of the Tika Metadata that is passed to the Tika parser. The supplier is called once for every parsed document.
      Parameters:
      metadataSupplier - the supplier of the Tika metadata; null means the default, an empty Metadata
      Returns:
      this builder
      Since:
      1.25.0
    • parseContextSupplier

      public ApacheTikaDocumentParser.Builder parseContextSupplier(@Nullable Supplier<org.apache.tika.parser.ParseContext> parseContextSupplier)
      Sets the supplier of the Tika ParseContext that is passed to the Tika parser. The supplier is called once for every parsed document.
      Parameters:
      parseContextSupplier - the supplier of the Tika parse context; null means the default, an empty ParseContext
      Returns:
      this builder
      Since:
      1.25.0
    • includeMetadata

      public ApacheTikaDocumentParser.Builder includeMetadata(@Nullable Boolean includeMetadata)
      Sets whether the metadata that Tika extracts (for example the author or the content type) is added to the parsed Document.
      Parameters:
      includeMetadata - whether to include the metadata; null means the default, false
      Returns:
      this builder
      Since:
      1.25.0
    • maxCharacters

      public ApacheTikaDocumentParser.Builder maxCharacters(@Nullable Integer maxCharacters)
      Sets the maximum number of characters of text that the parser extracts from a document. The count includes the line breaks and other whitespace that Tika adds to the text, for example after each paragraph. If a document contains more text, ApacheTikaDocumentParser.parse(InputStream) throws a RuntimeException whose cause is Tika's WriteLimitReachedException.

      This option applies to the default content handler, so it cannot be used together with contentHandlerSupplier(Supplier).

      Parameters:
      maxCharacters - the maximum number of characters, a positive number, or -1 for no limit; null means the default, 10,000,000
      Returns:
      this builder
      Since:
      1.25.0
    • build

      public ApacheTikaDocumentParser build()
      Creates the parser.
      Returns:
      a new ApacheTikaDocumentParser
      Throws:
      IllegalArgumentException - if maxCharacters(Integer) is not a positive number or -1, or if both maxCharacters(Integer) and contentHandlerSupplier(Supplier) are set
      Since:
      1.25.0