A small example of Lucene index using the ikanalyzer3.2.5 Chinese Word Divider

Source: Internet
Author: User

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();

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.