Lucene + Sen combined to implement a simple query engine (Japanese)

Source: Internet
Author: User

Related materials: http://ultimania.org/sen/

Perl5.6 to reduce: http://www.activestate.com/store/download.aspx? Prdguid = 81fbce82-6bd5-49bc-a915-08d58c2648ca
Ant1.5 reduction: http://ant.apache.org/bindownload.cgi
Note) Ant-dperl. bin = D:/Java/perl/bin/perl.exe
: Https://sen.dev.java.net/servlets/ProjectDocumentList? Folderid = 755 & expandfolder = 755 & folderid = 0

The Code is as follows:

Package demo;

Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;

Import org. Apache. Lucene. analysis. analyzer;
Import org. Apache. Lucene. analysis. Ja. japaneseanalyzer;
Import org.apache.e.doc ument. Document;
Import org.apache.e.doc ument. field;
Import org. Apache. Lucene. Index. indexwriter;
Import org. Apache. Lucene. queryparser. parseexception;
Import org. Apache. Lucene. queryparser. queryparser;
Import org. Apache. Lucene. Search. Hits;
Import org. Apache. Lucene. Search. indexsearcher;
Import org. Apache. Lucene. Search. query;
Import org. Apache. Lucene. Store. Directory;
Import org. Apache. Lucene. Store. fsdirectory;
Import org. Apache. Lucene. Store. ramdirectory;

Public class hellolucene ...{

Private Static final string field_content = "content ";

Private Static final string sen_xml_path = "D:/Java/sen-1.2.2.1/CONF/SEN. xml ";

Private Static final directory = new ramdirectory ();

Private Static final analyzer = new japaneseanalyzer (sen_xml_path );

Private Static final queryparser QP = new queryparser (field_content, analyzer );



Private Static final string [] Contents = ...{

"カツオ ", "サザはワ", "ワ ", "ワ ",

"カオは ", "サエは ", "ワカ ",

"マスはサ ", "", "タち ",

"サザとマ ", "", "",

"マス ", "カオは ", ""

};



Public static void main (string [] ARGs) throws ioexception, parseexception ...{

Makeindex ();

Backupindex ();

Bufferedreader BR = new bufferedreader (New inputstreamreader (system. In ));

String q = NULL;

While (q = NULL |! Q. Equals ("Q "))...{

System. Out. Print ("Q already asked)> ");

System. Out. Flush ();

Q = Br. Readline ();

If (! Q. Equals ("Q "))

Searchindex (Q );

}

BR. Close ();

If (directory! = NULL)

Directory. Close ();

}



Private Static void makeindex () throws ioexception ...{

Indexwriter writer = new indexwriter (directory, analyzer, true );

For (INT I = 0; I <contents. length; I ++ )...{

Document Doc = new document ();

Doc. Add (new field (field_content, contents [I], field. Store. Yes, field. Index. tokenized ));

Writer. adddocument (DOC );

}

Writer. Close ();

}



Private Static void searchindex (final string q) throws ioexception, parseexception ...{

Indexsearcher searcher = new indexsearcher (directory );

Query query = QP. parse (Q );

Hits hits = searcher. Search (query );

Int length = hits. Length ();

System. Out. println (integer. tostring (length) +. ");

For (INT I = 0; I <length; I ++ )...{

Document Doc = hits.doc (I );

System. Out. println ("" + Doc. Get (field_content ));

}

Searcher. Close ();

}



Private Static void backupindex () throws ioexception ...{

Directory persistent = fsdirectory. getdirectory ("Index ");

Directory. Copy (directory, persistent, false );

Persistent. Close ();

}
}

References:

Http://lucene.jugem.jp /? Month = 200703

Http://www.geocities.jp/another4000yrs/lucene/

Http://repository.kulib.kyoto-u.ac.jp/dspace-memo/japanization.html

Http://lucene.jugem.jp /? Eid = 53

Http://lucene.jugem.jp /? Eid = 97

Http://www.getopt.org/luke/

Chinese:

Http://www.javaeye.com/topic/49441? Page = 6

Http://blog.cnblog.org/archives/2005/07/luceneaecee.html

Http://blog.csdn.net/ugg/archive/2007/03/06/1522624.aspx

Http://www.ideagrace.com/html/doc/2007/02/07/08630.html

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.