concept Tika in category lucene

This is an excerpt from Manning's book Lucene in Action, Second Edition.
Solr (http://lucene.apache.org/solr), a sister project under the Apache Lucene umbrella, has support for natively ingesting relational databases and XML feeds, as well as handling rich documents through Tika integration. (We cover Tika in chapter 7.)
Tika has a simple-to-use API for providing a document source and then retrieving the text filtered from it. In this chapter we’ll start with an overview of Tika, then delve into its logical design, API, and tools. After showing you how to install Tika, we’ll discuss useful tools that let you filter a document without writing any Java code. Next we’ll explore a class that extracts text programmatically and produces a corresponding Lucene document. After that, we’ll examine two approaches for extracting fields from XML content, and then we’ll wrap up by considering some of Tika’s limitations and visiting a few alternative document filtering options.
Tika was added to the Lucene umbrella in October 2008, after graduating from the Apache incubator, which is the process newly created projects go through to become an Apache project. The most recent release as of this writing is 0.6. Development continues at a rapid pace, and it’s expected there will be non-backward-compatible changes in the march to the 1.0 release, so be sure to check Tika’s website at http://lucene.apache.org/tika for the latest documentation.
Tika is a framework that hosts plug-in parsers for each supported document type. The framework presents the same standard API to the application for extracting text and metadata from a document, and under the hood the plug-in parser interacts with the external library using the custom API exposed by that library. This lets your application use the same uniform API regardless of document type. When you need to extract text from a document, Tika finds the right parser for the document (details on this shortly).
As a framework, Tika doesn’t do any of the document filtering itself. Rather, it relies on external open source projects and libraries to do the heavy lifting. Table 7.1 lists the formats supported as of the 0.6 release, along with which project or library the document parser is based on. There’s support for many common document formats and new formats are added frequently, so check online for the latest list.
Table 7.1. Supported document formats and the library used to parse them
Format
Library
Microsoft’s OLE2 Compound Document Format (Excel, Word, PowerPoint, Visio, Outlook) Apache POI Microsoft Office 2007 OOXML Apache POI Adobe Portable Document Format (PDF) PDFBox Rich Text Format (RTF)—currently body text only (no metadata) Java Swing API (RTFEditorKit) Plain text character set detection ICU4J library HTML CyberNeko library XML Java’s javax.xml classes ZIP Archives Java’s built-in zip classes, Apache Commons Compress TAR Archives Apache Ant, Apache Commons Compress AR Archives Apache Commons Compress CPIO Archives Apache Commons Compress GZIP compression Java’s built-in support (GZIPInputStream), Apache Commons Compress BZIP2 compression Apache Ant, Apache Commons Compress Image formats (metadata only) Java’s javax.imageio classes Java class files ASM library (JCR-1522) Java JAR files Java’s built-in zip classes and ASM library, Apache Commons Compress MP3 audio (ID3v1 tags) Implemented directly Other audio formats (wav, aiff, au) Java’s built-in support (javax.sound.*) OpenDocument Parses XML directly Adobe Flash Parses metadata from FLV files directly MIDI files (embedded text, eg song lyrics) Java’s built-in support (javax.audio.midi.*) WAVE Audio (sampling metadata) Java’s built-in support (javax.audio.sampled.*) In addition to extracting the body text for a document, Tika extracts metadata values for most document types. Tika represents metadata as a single String <-> String map, with constants exposed for the common metadata keys, listed in table 7.2. These constants are defined in the Metadata class in the org.apache.tika.metadata package. But not all parsers can extract metadata, and when they do, they may extract to different metadata keys than you expect. In general the area of metadata extraction is still in flux in Tika, so it’s best to test parsing some samples of your documents to understand what metadata is exposed.
Table 7.2. Metadata keys that Tika extracts
Metadata Constant
Description
RESOURCE_KEY_NAME The name of the file or resource that contains the document. A client application can set this property to allow the parser to use filename heuristics to determine the format of the document. The parser implementation may set this property if the file format contains the canonical name of the file (for example, the GZIP format has a slot for the filename). CONTENT_TYPE The declared content type of the document. A client application can set this property based on an HTTP Content-Type header, for example. The declared content type may help the parser to correctly interpret the document. The parser implementation sets this property to the content type according to which document was parsed. CONTENT_ENCODING The declared content encoding of the document. A client application can set this property based on an HTTP Content-Type header, for example. The declared content type may help the parser to correctly interpret the document. The parser implementation sets this property to the content type according to which document was parsed. TITLE The title of the document. The parser implementation sets this property if the document format contains an explicit title field. AUTHOR The name of the author of the document. The parser implementation sets this property if the document format contains an explicit author field. MSOffice.* Defines additional metadata from Microsoft Office: APPLICATION_NAME, CHARACTER_COUNT, COMMENTS, KEYWORDS, LAST_AUTHOR, LAST_PRINTED, LAST_SAVED, PAGE_COUNT, REVISION_NUMBER, TEMPLATE, WORD_COUNT. Let’s drill down to learn how Tika models a document’s logical structure and what concrete API is used to expose this.