Full-text Lucene Search Initial Experience-Lucene demo

Source: Internet
Author: User

Recently, I have studied full-text search and found that the Lucene project is very good. So I used it to play. Today, I have successfully experienced the next search. Share it with you:

Lucene project Official Website: http://lucene.apache.org/

Lucene version: the latest version of lucene-3.0.3.zip has reached 3.5, but the latest version does not have our experience demo

Go to version 3.0.2 first. The version is not easy to find. It is an early code. Images for everyone: http://labs.renren.com/apache-mirror/lucene/java/3.0.3/

Download the second example e-3.0.3.zip.

After decompression, the following folder is displayed:




After obtaining the API, we can create a project. If you are not familiar with Eclipse, you can search for relevant information. There are a lot of information about eclipse on the Internet.

Create a Java Project

Create org. Apache. Lucene. Demo

Add lucene-3.0.3 \ SRC \ demo \ org \ apache \ Lucene \ demo

Add all files under the project package. Obtain the following Directory Project:



Run the indexfiles class. Select Run as config and enter the directory to be retrieved in the arguments parameter. Example: D: \ data

The running result is as follows. Search all files in the directory. Then you can try searching.

Select searchfiles to run. Enter the keyword to be retrieved. For example, 'tengzhou'

You can run the query results.

You can write a search by yourself.

Package COM. xinzhou. index; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. ioexception; import Java. util. date; import Org. apache. lucene. analysis. standard. standardanalyzer; import Org. apache. lucene. demo. filedocument; import Org. apache. lucene. index. indexwriter; import Org. apache. lucene. store. fsdirectory; import Org. apache. lucene. util. version; public class filesindex {static final file Index_dir = new file ("xinzhouindex");/** index all text files under a directory. */Public void indexdocfiles (string indexsourcepath) {string usage = "Enter the path of the file to be indexed"; if (indexsourcepath. length () = 0) {system. err. println ("Usage:" + usage); system. exit (1);} final file docdir = new file (indexsourcepath); If (! Docdir. exists () |! Docdir. canread () {system. out. println ("document directory" + docdir. getabsolutepath () + "'does not exist or is not readable, please check the path"); system. exit (1);} date start = new date (); try {indexwriter writer = new indexwriter (fsdirectory. open (index_dir), new standardanalyzer (version. lucene_current), true, indexwriter. maxfieldlength. limited); system. out. println ("indexing to directory" + index_dir + "'... "); Indexdocs (writer, docdir); system. out. println ("optimizing... "); writer. optimize (); writer. close (); date end = new date (); system. out. println (end. gettime ()-start. gettime () + "Total milliseconds");} catch (ioexception e) {system. out. println ("caught a" + E. getclass () + "\ n with message:" + E. getmessage () ;}} static void indexdocs (indexwriter writer, file) throws ioexception {// do not try To index files that cannot be readif (file. canread () {If (file. isdirectory () {string [] files = file. list (); // an IO error cocould occurif (files! = NULL) {for (INT I = 0; I <files. length; I ++) {indexdocs (writer, new file (file, files [I]) ;}} else {system. out. println ("adding" + file); try {writer. adddocument (filedocument. document (File);} // At least on windows, some temporary files raise this // exception with an "Access Denied" message // checking if the file can be read doesn't helpcatch (filenotfoundexception fnfe ){;}}}} // test public static void main (string [] ARGs) {filesindex fileindex = new filesindex (); fileindex. indexdocfiles ("D:/Data ");}}

You can also write a query program. In this way, you can write a full-text retrieval program. How is it? It's easy.

How good is Java ~~




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.