Use Java Lucene ikanalyzer

Source: Internet
Author: User
Http://tonrenyuye.blog.163.com/blog/static/3001257620107238120526/
For details about how to create an index, refer to the ikanalyzer documentation snapshot * to change this template, choose tools | templates * and open the template in the editor. */package indexanly; import Java. io. file; import Java. io. ioexception; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. sqlexcep Tion; import Java. util. date; import Java. util. logging. level; import Java. util. logging. logger; import javax. naming. namingexception; import Org. apache. lucene. analysis. analyzer; import org.apache.e.doc ument. datemedils; import org.apache.w.e.doc ument. document; import org.apache.e.doc ument. field; import Org. apache. lucene. index. corruptindexexception; import Org. apache. lucene. index. indexwriter; import o RG. apache. lucene. store. directory; import Org. apache. lucene. store. simplefsdirectory; import Org. wltea. analyzer. lucene. ikanalyzer;/***** @ author ndljava */public class KBC {/***** @ Param ARGs the command line arguments */public static void main (string [] ARGs) throws corruptindexexception, ioexception, namingexception, sqlexception {// todo code application logic here string indexdir = "/var/www/ALU C/Index "; try {class. forname ("com. mySQL. JDBC. driver ");} catch (classnotfoundexception ex) {logger. getlogger (KBC. class. getname ()). log (level. severe, null, ex);} connection conn = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/newsstore", "root", "root "); resultset rs = conn.createstatement(cmd.exe cutequery ("select * From tbl_news limit 50000"); // create an index for the table Field Analyzer analyzer = new ikanalyzer (); Directory dir = new simplefsdirectory (new file (indexdir); indexwriter = new indexwriter (Dir, analyzer, true, indexwriter. maxfieldlength. unlimited); While (RS. next () {system. out. println (RS. getstring ("news_title"); document DOC = new document (); Doc. add (new field ("news_title", Rs. getstring ("news_title"), field. store. yes, field. index. analyzed); Doc. add (new field ("news_content", Rs. Getstring ("news_content"), field. store. yes, field. index. analyzed); Doc. add (new field ("indexdate", datemedils. datetostring (new date (), datetols. resolution. day), field. store. yes, field. index. not_analyzed); indexwriter. adddocument (DOC);} system. out. println ("numdocs" + indexwriter. numdocs (); indexwriter. close (); RS. close (); Conn. close () ;}} search code: ========================================================== ================== ========================================================== /** To change this template, choose tools | templates * and open the template in the editor. */package indexanly; import Java. io. file; import Java. io. ioexception; import Org. apache. lucene. analysis. simpleanalyzer; import org.apache.e.doc ument. document; import Org. apache. lucene. index. corruptindexexception; 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. searcher; import Org. apache. lucene. search. topdocs; import Org. apache. lucene. store. directory; import Org. apache. lucene. store. ramdirectory; import Org. apache. l Ucene. store. simplefsdirectory; import Org. apache. lucene. util. version; import Org. wltea. analyzer. lucene. ikanalyzer; import Org. wltea. analyzer. lucene. ikqueryparser; import Org. wltea. analyzer. lucene. iksimilarity;/***** @ author ndljava */public class searchanl {/*** @ Param ARGs the command line arguments */public static void main (string [] ARGs) throws corruptindexexception, ioexception, parseexception {// Todo code application logic here string indexdir = "/var/www/aluc/Index"; string content = "news_content "; directory indexfsdir = new simplefsdirectory (new file (indexdir); indexreader = indexreader. open (indexfsdir, true); // directory indexfsdir = new ramdirectory (); searcher = new indexsearcher (indexfsdir); searcher. setsimilarity (New iksimilarity (); // queryparser parser = New queryparser (version. inclue_current, content, new simpleanalyzer (); // No search results are found in Chinese. This sentence is obtained from the source file. It can be searched in English. I don't know where the problem is?

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.