A simple standard test of Lucene (the Lucene package is based on version 3.5), javase3.5

Source: Internet
Author: User

A simple standard test of Lucene (the Lucene package is based on version 3.5), javase3.5

Lucene programming is generally divided into: Index, word segmentation, search

Index source code:

A standard test of package lucene; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStreamReader; import java. util. date; import org. apache. lucene. analysis. analyzer; import org. apache. lucene. analysis. standard. standardAnalyzer; import org.apache.e.doc ument. document; import org.apache.e.doc ument. field; import org. apache. lucene. index. IndexWriter; import org. apache. lucene. index. indexWriterConfig; import org. apache. lucene. index. indexWriterConfig. openMode; import org. apache. lucene. store. directory; import org. apache. lucene. store. FSDirectory; import org. apache. lucene. util. version; public class TextFileIndexer {public static void main (String [] args) throws Exception {// the location of the source File to be indexed File fileDir = new File ("E: \ Lucene \ source "); // file location for storing Indexes File indexDir = new File ("E: \ Lucene \ index"); // create the index in the hard disk Directory dir = FSDirectory. open (indexDir); // create a standard Analyzer luceneAnalyzer = new StandardAnalyzer (Version. required e_35); // IndexWriterConfig iwc = new IndexWriterConfig (Version. LUCENE_35, luceneAnalyzer); // each execution creates a new index instead of an additional index. setOpenMode (OpenMode. CREATE); // CREATE an IndexWriter indexWriter = new IndexWriter (dir, iwc); // save multiple files in the source File to the array File [] File [] textFiles = fileDir. listFiles (); // index start time long startTime = new Date (). getTime (); // for Loop the objects in the source directory for (int I = 0; I <textFiles. length; I ++) {if (textFiles [I]. isFile () & textFiles [I]. getName (). endsWith (". txt ") {// obtain the relative path ??? System. out. println ("file" + textFiles [I]. getCanonicalPath () + "being indexed... "); // call the custom method to read the file content FileReaderAll () String temp = FileReaderAll (textFiles [I]. getCanonicalPath (), "GBK"); // print the read content System. out. println (temp); // creates a document Object Document document = new Document () for the index information of each file; // file path index: only stores indexes without path creation, because we do not need to query the path Field fieldPath = new Field ("path", textFiles [I]. getPath (), Field. store. YES, Field. index. NO );// File Content Index: Field fieldBody = new Field ("body", temp, Field. store. YES, Field. index. ANALYZED, Field. termVector. WITH_POSITIONS_OFFSETS); // Add each domain to the document Object of the document. add (fieldPath); document. add (fieldBody); // write the document to the index indexWriter. addDocument (document) ;}// disable indexWriter. close (); // test the index time long endTime = new Date (). getTime (); // For the differences between getPath, getAbsolutePath, and getCanonicalPath, refer to the reprinted article System. out. println ("time used:" + (endTi Me-startTime) + "add the document to the index in milliseconds," + fileDir. getPath (); System. out. println ("time used:" + (endTime-startTime) + "millisecond to add the document to the index," + fileDir. getAbsolutePath (); System. out. println ("time used:" + (endTime-startTime) + "millisecond to add the document to the index," + fileDir. getCanonicalPath ();} // custom method for reading file content FileReaderAll () public static String FileReaderAll (String FileName, String charset) throws IOException {BufferedReader reader = new Buffe RedReader (new InputStreamReader (new FileInputStream (FileName), charset); // specify the file reading method charset: GBKString line = new String (); String temp = new String (); while (line = reader. readLine ())! = Null) {temp + = line;} reader. close (); return temp ;}}

Search source code:

A standard test of package lucene; import java. io. file; import java. io. IOException; import org. apache. lucene. analysis. analyzer; import org. apache. lucene. analysis. standard. standardAnalyzer; import org. apache. lucene. index. indexReader; import org. apache. lucene. queryParser. parseException; import org. apache. lucene. queryParser. queryParser; import org. apache. lucene. search. indexSearcher; import org. apache. lucene. search. query; I Mport org. apache. lucene. search. scoreDoc; import org. apache. lucene. search. topDocs; import org. apache. lucene. store. FSDirectory; import org. apache. lucene. util. version; public class TestQuery {public static void main (String [] args) throws IOException, ParseException {// search index path String index = "E: \ index "; indexReader reader = IndexReader. open (FSDirectory. open (new File (index); // defines the searcherIndexSearch to be queried in the index Library Er searcher = new IndexSearcher (reader); ScoreDoc [] hits = null; // query String queryString = ""; // declare the Query object query = null; // Analyzer analyzer = new StandardAnalyzer (Version. required e_35); try {QueryParser qp = new QueryParser (Version. required e_35, "body", analyzer); query = qp. parse (queryString);} catch (ParseException e) {e. printStackTrace ();} if (searcher! = Null) {TopDocs results = searcher. search (query, 10); hits = results. scoreDocs; if (hits. length> 0) {System. out. println ("find hits. length = "+ hits. length + "Result \ n" + "find results. totalHits = "+ results. totalHits);} searcher. close ();}}}


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.