public boolean findInFile(String query, Path path) {
        InterruptingContentHandler handler = new InterruptingContentHandler(query);

        Metadata metadata = new Metadata();
        ParseContext context = new ParseContext();
        context.set(Parser.class, tika.getParser());

        try (InputStream is = new BufferedInputStream(Files.newInputStream(path))) {
            tika
                    .getParser()
                    .parse(is, handler, metadata, context);
        } catch (QueryMatchedException e) {
            return true;
        } catch (SAXException | TikaException | IOException e) {
            // something went wrong with parsing...
            e.printStackTrace();
        }
        return false;
    }
This example demonstrates how to interrupt document parsing if
some condition is met.
<p>
{@link InterruptableParsingExample.InterruptingContentHandler} throws special exception as soon as
find {@code query} string in parsed file.
<p>
See also http://stackoverflow.com/questions/31939851
tika-example/src/main/java/org/apache/tika/example/InterruptableParsingExample.java