A simple standard test for Lucene (Lucene package based on 3.5 version)

Source: Internet
Author: User

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

Index Source code:

A standard test of the 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.lucene.document.document;import Org.apache.lucene.document.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 {//source file location required for index, file Filedir = new file ("E:\\lucene\\source");//location of the file where the index is stored f Ile Indexdir = new File ("E:\\lucene\\index");//Set the index on the hard disk directory dir = fsdirectory.open (Indexdir);// Establish a standard word breaker analyzer Luceneanalyzer = new StandardAnalyzer (version.lucene_35);//indexwriterconfigIWC = new Indexwriterconfig (version.lucene_35,luceneanalyzer);//Each execution creates a new index instead of an append index Iwc.setopenmode ( openmode.create);//Create an indexer indexwriter indexwriter = new IndexWriter (dir, IWC);//Save multiple files from the source file to the array file[] file[ = Filedir.listfiles ();//The start time of the index is long startTime = new Date (). GetTime ();//For looping through files in source directory for (int i = 0; i < Textfiles.le Ngth; i++) {if (Textfiles[i].isfile () && textfiles[i].getname (). EndsWith (". txt")) {//Get relative path??? System.out.println ("File" + textfiles[i].getcanonicalpath () + "indexing ...");//Call Custom Read file contents Method Filereaderall () String temp = Filereaderall (Textfiles[i].getcanonicalpath (), "GBK");//Print the contents of the Read System.out.println (temp);// Establish a Document object for each file's index information document document = new document ();//file path index: Simply store index without path, 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 field to the Document object DocumeNt.add (Fieldpath);d ocument.add (fieldbody);//write the document to the index indexwriter.adddocument (document);}} Close Indexwriter.close ();//test the index time long endTime = new Date (). GetTime ();//getpath,getabsolutepath, Getcanonicalpath's differences refer to the reproduced article System.out.println ("spents:" + (Endtime-starttime) + "milliseconds to add the document to the index," +filedir.getpath ()); System.out.println ("spents:" + (Endtime-starttime) + "milliseconds to add the document to the index," +filedir.getabsolutepath ()); System.out.println ("spents:" + (Endtime-starttime) + "milliseconds to add the document to the index," +filedir.getcanonicalpath ());} Custom method for reading the contents of a file Filereaderall () public static string Filereaderall (String FileName, String charset) throws IOException { BufferedReader reader = new BufferedReader (new InputStreamReader (New FileInputStream (FileName), CharSet));// Read charset:gbkstring line = new String (); String temp = new String (), while (line = Reader.readline ()) = null) {temp + = line;} Reader.close (); return temp;}}

Search Source code:

One standard test of the 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;import 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 (index));//define Searcherindexsearcher for queries in the index library searcher = new Indexsearcher (reader); Scoredoc[] hits = null;//search term string querystring= "absolute Autumn Fragrance";//Declaring Query object query query = null;//Word breaker Analyzer Analyzer = new Standard Analyzer (version.lucene_35); try {QueryParser QP = new Queryparser (version.lucene_35, "body", analyzer); query = Qp.parse (queryString);} catch (ParseException e) {e.printstacktrace ();} if (searcher!=null) {Topdocs results = searcher.search (query, ten); hits = Results.scoredocs;if (hits.length>0) { System.out.println ("Find hits.length=" +hits.length+ "results \ n" + "find results.totalhits=" +results.totalhits);} Searcher.close ();}}}


A simple standard test for Lucene (Lucene package based on 3.5 version)

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.