This article uses a small example to help you learn the indexing functions of ikanalyzer3.2.5 and Lucene. The following two jar packages are required for the preparation environment.
Lucene 3.5.0.jar and ikanalyzer3.2.5 packages
The Code is as follows:
Import Java. io. file; import Java. io. ioexception; import Org. apache. lucene. analysis. analyzer; import org.apache.e.doc ument. document; import org.apache.e.doc ument. field; // import Org. apache. lucene. search. indexsearcher; import Org. apache. lucene. index. corruptindexexception; import Org. apache. lucene. index. indexwriter; import Org. apache. lucene. index. indexwriterconfig; import Org. apache. lucene. index. inde Xreader; import Org. apache. lucene. search. query; import Org. apache. lucene. search. topdocs; import Org. apache. lucene. search. indexsearcher; import Org. apache. lucene. store. directory; import Org. apache. lucene. store. fsdirectory; import Org. apache. lucene. store. lockobtainfailedexception; import Org. apache. lucene. store. ramdirectory; import Org. apache. lucene. util. version; import Org. wltea. analyzer. lucene. ikanalyzer; I Mport Org. wltea. analyzer. lucene. ikqueryparser; import Org. wltea. analyzer. lucene. iksimilarity; public class test {// Private Static final directory indexreader = NULL; public static void main (string ARGs []) throws exception {ramdirectory directory = new ramdirectory (); // file index_dir = new file ("E: \ temp \ Index"); analyzer = new ikanalyzer (); indexwriterconfig IWC = new indexwriterconfig (versio N. e_e_35, analyzer); indexwriter = new indexwriter (directory, IWC); string STR = "Hello Jun Jie, this is the start of success! Come on! "; Document DOC = new document (); Doc. add (new field ("contents", STR, field. store. yes, field. index. analyzed); indexwriter. adddocument (DOC); STR = "if you want to implement this project, you can! "; Doc = new document (); Doc. add (new field ("contents", STR, field. store. yes, field. index. analyzed); indexwriter. adddocument (DOC); indexwriter. close (); // indexreader reader = new indexreader (); indexreader reader = indexreader. open (directory); indexsearcher searcher = new indexsearcher (Reader); searcher. setsimilarity (New iksimilarity (); string KEYWORDS = "Junjie"; query = ikqueryparser. parse ("contents", keywords); topdocs = searcher. search (query, integer. max_value); system. out. println (topdocs. totalhits );}}
We also want to share with you a problem that occurs during programming. As follows:
Exception in thread "Main" org. apache. lucene. index. indexnotfoundexception: No segments * file found in org. apache. lucene. store. ramdirectory @ 1938039 lockfactory = org. apache. lucene. store. singleinstancelockfactory @ c743eb:
Files: [_ 0.fdx, _ 0.fdt]
At org. Apache. Lucene. Index. segmentinfos $ findsegmentsfile. Run (segmentinfos. Java: 712)
At org. Apache. Lucene. Index. directoryreader. Open (directoryreader. Java: 75)
At org. Apache. Lucene. Index. indexreader. open( indexreader. Java: 462)
At org. Apache. Lucene. Index. indexreader. open( indexreader. Java: 308)
At test. Main (test. Java: 51)
When the no segments * file found problem occurs, the following code line is missing.
indexWriter.close();