Lucene-2.0 learning document (I)

Source: Internet
Author: User
Tags date1

Lucene is an open-source project organized by Apache to implement full-text search engines using Java. Its function is very powerful, and its API is also very simple. In general, using Lucene to create and search databases is similar (a bit like). Document can be seen as a row of database records, and field can be seen as a database field. Using Lucene to implement a search engine is as simple as using JDBC to connect to a database.

Lucene2.0, Which is widely used and introduced in the pastLucene 1.4.3Incompatible.

Lucene2.0YesHttp://apache.justdn.org/lucene/java/

Let's take a look at an example to give us a rough idea of Lucene.

A JUnit test case: (To make the code clear and nice, we throw all exceptions)

A)This is an example of creating a file index.

Public VoidTestindexhello ()ThrowsIoexception

{

Date date1 =NewDate ();

// Create a new write Tool

// The first parameter is the directory in which the index is created.

// The second parameter is to create a text analyzer. You can write a text analyzer by yourself using the standard parameter.

// If the third parameter is true, the C: // index directory is cleared before the index is created.

Indexwriter writer =NewIndexwriter ("C: // Index ",NewStandardanalyzer (),True);

// This is the data source folder.

File file =NewFile ("C: // file ");

/**

* In this example, the file content in the C: // file directory is indexed and the file path is used as an attachment to the search content.

*/

If(File. isdirectory ())

{

String [] filelist = file. List ();

For(IntI = 0; I <filelist. length; I ++)

{

// Create a new document, which can be viewed as a row of database records

Document Doc =NewDocument ();

File F =NewFile (file,

Filelist [I]);

Reader reader =NewBufferedreader (NewFilereader (f ));

Doc. Add (NewField ("file", Reader); // Add field for doument

Doc. Add (NewField ("path", F. getabsolutepath (), field. Store.Yes, Field. index.No));

Writer. adddocument (DOC );

}

}

Writer. Close (); // this step is required. Only in this way will the data be written into the index directory.

Date date2 =NewDate ();

System.Out. Println ("time used" + (date2.gettime ()-date1.gettime () + "millisecond ");

}

Note: It is time-consuming to create an index. Therefore, the final output will take a long time.

B) An example of full-text search using Indexes

Public VoidHellosearch ()ThrowsIoexception, parseexception

{

Indexsearcher =NewIndexsearcher ("C: // Index"); // same as indexwriter above

Queryparser =NewQueryparser ("file", // This is a word Divider

NewStandardanalyzer ());

Bufferedreader BR =NewBufferedreader (NewInputstreamreader (system.In));

Query query = queryparser. parse (Br. Readline (); // query is an abstract class.

Hits hits = indexsearcher. Search (query );

Document Doc =Null;

System.Out. Print ("Searching ................");

For(IntI = 0; I

{

Doc = hits.doc (I );

System.Out. Println ("content:" + Doc. Get ("file"); // note what is output here

System.Out. Println ("file path:" + Doc. Get ("path "));

}

}

The two examples above show that Lucene is relatively simple.

Run the two examples above. You may say how Doc. Get ("file"); the returned result is null. We will talk about it now.

 

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.