Lucene index non-TXT document (PDF Word rtf html xml)

Source: Internet
Author: User
Index is required first for search. Index TXT files are the easiest way to search. Here we will introduce the indexes of documents in other formats, such as MS Word, PDF, and RTF.
The indexing method is to convert all kinds of documents into plain text before indexing, so the key lies in the conversion. Fortunately, there are too many open source projects in the Java World, and many of them can be used directly. The following sections describe:
Before writing it all: All the is parameters described below are inputstream, which is the file to be indexed.
Word documents:
Open-source projects that convert Word documents into plain text can be used: poi or textmining.
Poi usage: Worddocument WD = New Worddocument (is );
Stringwriter doctextwriter = New Stringwriter ();
WD. writealltext ( New Printwriter (doctextwriter ));
Doctextwriter. Close ();
Bodytext = Doctextwriter. tostring ();

Textmining is easier to use:Bodytext= NewWordextractor (). extracttext (is );

PDF:
The class library that can be used to convert PDF files is product_box. Cosdocument cosdoc = NULL;
Partition parser = new partition Parser (is );
Parser. parse ();
Cosdoc = Parser. getdocument ()
If (Cosdoc. isencrypted ()){
Decryptdocument decryptor = New decryptdocument (cosdoc );
Decryptor. decryptdocument (password );
}
Pdftextstripper Stripper = New vertex textstripper ();
String doctext = Stripper. gettext (New pddocument (cosdoc ));

RTF document:
The conversion of RTF is available in javax.Defaultstyleddocument styleddoc= NewDefaultstyleddocument ();
NewRtfeditorkit (). Read (is, styleddoc,0);
String bodytext=Styleddoc. gettext (0, Styleddoc. getlength ());

In this way, you can index texts in various formats.

The Processing Method for HTML and XML is the same.
The difference is that the available HTML class libraries are: jtidy
The available XML class libraries are Sax and digester.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.