Lucene Personal Insights (iii)

Source: Internet
Author: User
Tags createindex date1

In fact, the next step is to paste the code, familiar with Lucene normal workflow, or how to use this API, the deeper things this article will not talk about.

The previous article also said Maven configuration, as long as you can download the computer network. I'll post the code.

Package Com.muyi.lucene.mavenlucene.ltest;import Java.io.bufferedreader;import Java.io.file;import Java.io.fileinputstream;import Java.io.filereader;import Java.nio.file.filesystems;import java.util.ArrayList; Import Java.util.date;import Java.util.list;import Org.apache.lucene.analysis.analyzer;import Org.apache.lucene.analysis.standard.standardanalyzer;import Org.apache.lucene.document.document;import Org.apache.lucene.document.field.store;import Org.apache.lucene.document.textfield;import Org.apache.lucene.index.directoryreader;import Org.apache.lucene.index.indexwriter;import Org.apache.lucene.index.indexwriterconfig;import Org.apache.lucene.queryparser.classic.queryparser;import Org.apache.lucene.search.indexsearcher;import Org.apache.lucene.search.query;import Org.apache.lucene.search.scoredoc;import Org.apache.lucene.search.topdocs;import Org.apache.lucene.store.directory;import Org.apache.lucene.store.fsdirectory;import Org.apache.poi.hwpf.hwpfdocument;import ORG.APACHE.POI.HWPF. Usermodel. Range;import JXL. Cell;import JXL. Sheet;import JXL.    workbook;/** * @author XINGHL * */public class indexmanager2{private static Indexmanager Indexmanager;        private static String content= "";    private static String Index_dir = "D:\\luceneindex";    private static String Data_dir = "D:\\lucenedata";    private static Analyzer analyzer = NULL;    private static Directory directory = NULL;        private static IndexWriter indexwriter = null;            /** * CREATE INDEX Manager * @return Returns the index Manager Object */public Indexmanager GetManager () {if (Indexmanager = = null) {        This.indexmanager = new Indexmanager ();    } return Indexmanager; /** * CREATE index of current file directory * @param path Current file directory * @return succeeded */public static Boolean CreateIndex (String        Path) {Date date1 = new Date ();        list<file> fileList = getfilelist (path);            for (File file:filelist) {content = ""; Get file suffix String type = filE.getname (). substring (File.getname (). LastIndexOf (".")            +1);                if ("TXT". Equalsignorecase (Type)) {content + = txt2string (file);                        System.out.println ("File name:" +file.getpath () + "file contents" +content);                }else if ("Doc". Equalsignorecase (Type)) {content + = doc2string (file);                        System.out.println ("File name:" +file.getpath () + "file contents" +content);                }else if ("xls". Equalsignorecase (Type)) {content + = xls2string (file);                            System.out.println ("File name:" +file.getpath () + "file contents" +content);                } try{Analyzer = new StandardAnalyzer ();                    Directory = Fsdirectory.open (Filesystems.getdefault (). GetPath (Index_dir));                File Indexfile = new file (Index_dir);                if (!indexfile.exists ()) {indexfile.mkdirs (); } indexwriterconfigConfig = new Indexwriterconfig (analyzer);                IndexWriter = new IndexWriter (directory, config);                Indexwriter.deleteall ();//Clear Previous index Document document = new document ();                Document.add (New TextField ("FileName", File.getname (), store.yes));                Document.add (New TextField ("content", content, Store.yes));                Document.add (New TextField ("Path", File.getpath (), store.yes));                Indexwriter.adddocument (document);                Indexwriter.commit ();                                Closewriter ();            }catch (Exception e) {e.printstacktrace ();        } content = "";        } Date Date2 = new Date ();        SYSTEM.OUT.PRINTLN ("CREATE INDEX-----Time:" + (Date2.gettime ()-date1.gettime ()) + "ms\n");    return true; }/** * Reads the contents of the TXT file * @param file object to read * @return return file contents */public static String txt2string (F ile file) {String result = "";            try{FileReader filereader = new FileReader (file);            BufferedReader br = new BufferedReader (filereader);//Constructs a BufferedReader class to read the file String s = null;            while ((s = br.readline ())!=null) {//Use the ReadLine method, one-time read result = result + "\ n" +s;            } br.close ();        }catch (Exception e) {e.printstacktrace ();    } return result; /** * Read doc file contents * @param file object to read * @return return file contents */public static String doc2string (Fi        Le file) {String result = "";            try{FileInputStream fis = new FileInputStream (file);            Hwpfdocument doc = new hwpfdocument (FIS);            Range rang = Doc.getrange ();            Result + = Rang.text ();        Fis.close ();        }catch (Exception e) {e.printstacktrace ();    } return result; }/** * Read xls file contents * @param file object to read * @return return file contents */Public sTatic string xls2string (file file) {string result = "";               try{FileInputStream fis = new FileInputStream (file);               StringBuilder sb = new StringBuilder (); Jxl.               Workbook RWB = Workbook.getworkbook (FIS);               sheet[] Sheet = Rwb.getsheets ();                   for (int i = 0; i < sheet.length; i++) {Sheet rs = rwb.getsheet (i);                      for (int j = 0; J < Rs.getrows (); j + +) {cell[] cells = Rs.getrow (j);                   for (int k=0;k<cells.length;k++) sb.append (Cells[k].getcontents ());               }} fis.close ();        Result + = Sb.tostring ();        }catch (Exception e) {e.printstacktrace ();    } return result; }/** * Find index, return eligible file * @param text lookup String * @return eligible files list */public static void Searchindex (        String text) {Date date1 = new Date (); try{           Directory = Fsdirectory.open (Filesystems.getdefault (). GetPath ("D:\\luceneindex"));            Analyzer = new StandardAnalyzer ();            Directoryreader Ireader = directoryreader.open (directory);                Indexsearcher isearcher = new Indexsearcher (Ireader);            Queryparser parser = new Queryparser ("content", analyzer);                        Query query = parser.parse (text);            Topdocs Topdocs = isearcher.search (query, 1000);            System.out.println (topdocs.totalhits);            scoredoc[] Scoredocs = Topdocs.scoredocs;            System.out.println ("--------------------Find Results-----------------------");  for (Scoredoc Scoredoc:scoredocs) {///7, Get specific Document object document based on searcher and Scoredoc objects                  Document = Isearcher.doc (Scoredoc.doc); 8. Get the desired value according to the Document Object System.out.println (document.get ("filename") + document.get ("Conte NT ") +" "+ document.get (" path ");           } System.out.println ("--------------------Find Results-----------------------");            Ireader.close ();        Directory.close ();        }catch (Exception e) {e.printstacktrace ();        } Date Date2 = new Date ();    SYSTEM.OUT.PRINTLN ("View Index-----Time:" + (Date2.gettime ()-date1.gettime ()) + "ms\n"); }/** * Filter files in directory * @param Dirpath want to get directory of files * @return return file list */public static list<file> get        FileList (String dirpath) {file[] files = new File (Dirpath). Listfiles ();        list<file> fileList = new arraylist<file> ();            for (file file:files) {if (Istxtfile (File.getname ())) {Filelist.add (file);    }} return fileList;    }/** * Determines whether the TXT XLS DOC format is currently supported for the target file * @param fileName file name * @return Returns True if the file type satisfies the filter condition; otherwise false */ public static Boolean istxtfile (String fileName) {if (Filename.lastindexof (". txt") > 0) {           return true;        }else if (Filename.lastindexof (". xls") > 0) {return true;        }else if (Filename.lastindexof (". Doc") > 0) {return true;    } return false; } public static void Closewriter () throws Exception {if (IndexWriter! = null) {Indexwriter.clos        E ();     }}/** * Delete all files under File directory * @param file directory to delete * @return If successful, returns TRUE. */public static Boolean Deletedir (file file) {if (File.isdirectory ()) {file[] files = file.listfiles (            );            for (int i=0; i<files.length; i++) {deletedir (files[i]);        }} file.delete ();    return true;        } public static void Main (string[] args) {Date date1 = new Date ();        File FileIndex = new file (Index_dir);        if (Deletedir (FileIndex)) {Fileindex.mkdir ();        }else{Fileindex.mkdir ();        } createindex (Data_dir); SeArchindex ("Black Cave");        Date Date2 = new Date ();    System.out.println ("Execution Time:" + (Date2.gettime ()-date1.gettime ()) + "ms\n"); }}

In fact, these are the few that create a reader--index--Look up the index--get the result--output.

This is probably the process. Lucene ends here. I suddenly wanted to learn something else.

Lucene Personal Insights (iii)

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.