Apache Tika and character limit when parsing documents
Asked Answered
F

1

18

Could please anybody help me to sort it out?

It can be done like this

   Tika tika = new Tika();
   tika.setMaxStringLength(10*1024*1024);

But if you don't use Tika directly, like this:

ContentHandler textHandler = new BodyContentHandler();
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();

ParseContext ps = new ParseContext();
for (InputStream is : getInputStreams()) {
    parser.parse(is, textHandler, metadata, ps);
    is.close();
    System.out.println("Title: " + metadata.get("title"));
    System.out.println("Author: " + metadata.get("Author"));
}

There is no way to set it up, because you don't interact with the WriteOutContentHandler. Btw it is set to -1 by default which means no restrictions. But the resulting restriction is 100000 characters.

/**
 * The maximum number of characters to write to the character stream.
 * Set to -1 for no limit.
 */
private final int writeLimit;

/**
 * Number of characters written so far.
 */
private int writeCount = 0;

private WriteOutContentHandler(Writer writer, int writeLimit) {
    this.writer = writer;
    this.writeLimit = writeLimit;
}

/**
 * Creates a content handler that writes character events to
 * the given writer.
 *
 * @param writer writer
 */
public WriteOutContentHandler(Writer writer) {
    this(writer, -1);
}
Fernando answered 26/5, 2011 at 20:33 Comment(0)
F
39

You must have overlooked that the content handler has constructor with writelimit.

ContentHandler textHandler = new BodyContentHandler(int writeLimit);
Fernando answered 26/5, 2011 at 20:37 Comment(2)
Notice that -1 is unlimited chars!Tubbs
Is it possible to set this property in tika XML configuration?Crinum

© 2022 - 2024 — McMap. All rights reserved.