Fixed issue with setting of maxStringLength applying globally to the tika instance.
I have extended the Tika class to allow for setting of how much text to extract from a document to be on a per call basis.
This commit is contained in:
parent
6a08ca673a
commit
9a26458862
|
@ -278,10 +278,8 @@ public class AttachmentMapper implements Mapper {
|
|||
|
||||
String parsedContent;
|
||||
try {
|
||||
// Set the maximum length of strings returned by the parseToString method, -1 sets no limit
|
||||
tika().setMaxStringLength(contentLength);
|
||||
|
||||
parsedContent = tika().parseToString(new FastByteArrayInputStream(content), metadata);
|
||||
// Set the maximum length of strings returned by the parseToString method, -1 sets no limit
|
||||
parsedContent = tika().parseToString(new FastByteArrayInputStream(content), metadata, contentLength);
|
||||
} catch (TikaException e) {
|
||||
throw new MapperParsingException("Failed to extract [" + contentLength + "] characters of text for [" + name + "]", e);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Licensed to ElasticSearch and Shay Banon under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. ElasticSearch licenses this
|
||||
* file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.plugin.mapper.attachments.tika;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.tika.Tika;
|
||||
import org.apache.tika.exception.TikaException;
|
||||
import org.apache.tika.metadata.Metadata;
|
||||
import org.apache.tika.parser.ParseContext;
|
||||
import org.apache.tika.parser.Parser;
|
||||
import org.apache.tika.sax.BodyContentHandler;
|
||||
import org.apache.tika.sax.WriteOutContentHandler;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
|
||||
/**
|
||||
* Extends the Tika class, so as to provide a way for setting the maximumStringLength on a per parse document basis.
|
||||
*/
|
||||
public class TikaExtended extends Tika {
|
||||
|
||||
public String parseToString(InputStream stream, Metadata metadata, int maxExtractedStringLength) throws IOException, TikaException {
|
||||
|
||||
// setup
|
||||
WriteOutContentHandler writeHandler = new WriteOutContentHandler(maxExtractedStringLength);
|
||||
BodyContentHandler contentHandler = new BodyContentHandler(writeHandler);
|
||||
Parser parser = getParser();
|
||||
ParseContext context = new ParseContext();
|
||||
context.set(Parser.class, parser);
|
||||
|
||||
try {
|
||||
parser.parse(stream, contentHandler, metadata, context);
|
||||
} catch (SAXException e) {
|
||||
if (!writeHandler.isWriteLimitReached(e)) {
|
||||
throw new TikaException("Unexpected SAX processing failure", e);
|
||||
}
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
return writeHandler.toString();
|
||||
}
|
||||
}
|
|
@ -19,16 +19,15 @@
|
|||
|
||||
package org.elasticsearch.plugin.mapper.attachments.tika;
|
||||
|
||||
import org.apache.tika.Tika;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class TikaInstance {
|
||||
|
||||
private static final Tika tika = new Tika();
|
||||
private static final TikaExtended tika = new TikaExtended();
|
||||
|
||||
public static Tika tika() {
|
||||
public static TikaExtended tika() {
|
||||
return tika;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue