Lucene solution to full-text search word2003,word2007

Source: Internet
Author: User
Tags gettext

In the previous article, Lucene could only retrieve word2003 in full text and could not retrieve 2007. To resolve this problem, the following methods have been found

POI reads Word (Word 2003 and Word 2007)

Recently in the system to the customer, the user asked for the need to be able to import Word files, now Microsoft Word has several versions 97, 2003, 2007, the three versions of the format of data storage is quite different, and now 97 has basically exited the market, Few people use this version, so in our system only consider 2003 version and 2007 version, because we only need to be able to read word text content, where the text style, pictures and other information can be ignored, and do not directly manipulate word files, so we choose to use Apache POI for reading.

Word files that read version 2003 (. doc) are relatively simple and require only Poi-3.5-beta6-20090622.jar and Poi-scratchpad-3.5-beta6-20090622.jar two jar packages, while 2007 version (. docx) on the trouble, I said this trouble is not when we write code trouble, is to import the jar package compared more, there are the following 7:
1. Openxml4j-bin-beta.jar
2. Poi-3.5-beta6-20090622.jar
3. Poi-ooxml-3.5-beta6-20090622.jar
4. Dom4j-1.6.1.jar
5. Geronimo-stax-api_1.0_spec-1.0.jar
6. Ooxml-schemas-1.0.jar
7. Xmlbeans-2.3.0.jar
4-7 of these are the jar packages that Poi-ooxml-3.5-beta6-20090622.jar depends on (can be found in the Ooxml-lib directory in poi-bin-3.5-beta6-20090622.tar.gz).

    before we write the code, we need to download the required jar packages, we just download  poi-bin-3.5-beta6-20090622.tar.gz  and   openxml4j-bin-beta.jar , because the other jar packages you need can be found in the poi-bin-3.5-beta6-20090622.tar.gz, as follows:
Poi-bin-3.5-beta6-20090622.tar.gz:http://apache.etoak.com/poi/dev/bin/poi-bin-3.5-beta6-20090622.tar.gz
Openxml4j-bin-beta.jar:http://mirror.optus.net/sourceforge/o/op/openxml4j/openxml4j-bin-beta.jar
 
    below is the Java code that reads the word file, notably: POI does not read the picture information in Word files when it reads the word file, or for version 2007 of Word (. docx), if wor The D file has a table, and all the data in the table will be at the end of the read string.

Import Java.io.file;import java.io.fileinputstream;import Java.io.inputstream;import org.apache.poi.POIXMLDocument ; Import Org.apache.poi.poixmltextextractor;import Org.apache.poi.hwpf.extractor.wordextractor;import Org.apache.poi.openxml4j.opc.opcpackage;import org.apache.poi.xwpf.extractor.xwpfwordextractor;/** * POI Read word Test classes for text content in 2003 and Word 2007 <br/> * @createDate 2009-07-25 * @author Carl He */public class Test {public static V OID Main (string[] args) {try {//word 2003: Picture is not read InputStream is = new FileInputStream (n            EW File ("C://files//2003.doc"));            Wordextractor ex = new Wordextractor (IS);            String text2003 = Ex.gettext ();            System.out.println (text2003); The word 2007 picture is not read, the data in the table is placed at the end of the string opcpackage opcpackage = Poixmldocument.openpackage ("C://files//2007.docx"            );            Poixmltextextractor extractor = new Xwpfwordextractor (opcpackage); String text2007 = Extractor.getteXT ();        System.out.println (text2007);        } catch (Exception e) {e.printstacktrace (); }    }}

After we find the method, we make changes to the source code of the previous article Indexer.java, new function getDocument2007 ()

  public static Document getDocument2007 (file file) throws Exception {String DocPath = File.getabsolutepath ();    String title = File.getname ();  Arrival Riverbank Document Document = new document (); Opcpackage opcpackage = Poixmldocument.openpackage (DocPath); Poixmltextextractor extractor = new Xwpfwordextractor (opcpackage);  String cont = Extractor.gettext (); Document.add (New Field ("filename", title, Field.Store.YES, Field.Index.ANALYZED));//tokenized//document.add (new  Field ("Contents", contents));  Document.add (New Field ("Contents", cont,field.store.yes,field.index.analyzed));  Document.add (New Field ("path", DocPath, field.store.yes,field.index.analyzed)); Document.add (New Field ("Indexdate", Datetools.datetostring (New Date (), DateTools.Resolution.DAY), Field.Store.YES,  Field.Index.NOT_ANALYZED));  return document; public static Document getDocument2003 (file file) throws Exception {String DocPath = File.getabsolutepath (); String title = File.getname ();//arrival Riverbank Slow DocumenTdocument document = new document (); StringBuffer contents = new StringBuffer ("");//Ã Andychia.            Kernel attacker try {FileInputStream fs = new FileInputStream (DocPath);            Hwpfdocument doc = new hwpfdocument (FS);            Range range = Doc.getrange ();                int paragraphcount = range.numparagraphs ();//molestation tenant paragraph for (int i = 0; i < Paragraphcount; i++) {//閬 Indent Vol molestation tenant paragraph Xuan 彇 indent version credential                Paragraph pp = range.getparagraph (i);            Contents.append (Pp.text ()); }} catch (Exception e) {} String cont = Contents.tostring (). Trim ();d Ocument.add (New Field ("Filenam E ", title, field.store.yes,field.index.analyzed));//tokenized//document.add (New Field (" Contents ", contents)); Document.add (new Field ("Contents", cont,field.store.yes,field.index.analyzed)),//document.add (new Field ("Path", DocPath, field.store.yes,field.index.analyzed));d Ocument.add (New Field ("Indexdate", datetools.datetostring (new Date (), DateTools.Resolution.DAY), Field.store. yes,field.index.not_analyzed)); return document;}

Also modify the read file in the For loop

if (Files[i].getname (). EndsWith (". Doc")) {
doc = getDocument2003 (files[i]);
}else if (Files[i].getname (). EndsWith (". docx")) {
doc = getDocument2007 (files[i]);
}

Lucene solution to full-text search word2003,word2007

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.