Helloworld for Lucene Learning (simple example)

Source: Internet
Author: User

introduction
what's Lucene

Lucene is a function library for information retrieval (Library ), you can add the index and search functions for your application .

LuceneUsers do not need to learn more about full-text search.,Just learn to use a class in the library,You can implement full-text search for your application..

But never think Lucene Is an image Google Such a search engine , Lucene Not even an applicationProgram , It is just a tool , One Library. You can also think of it as an index , A good set of simple and easy-to-use search functions API. Use this set API You can do a lot of searching. , And convenient .

What can Lucene do

Lucene You can index and search any data. . Lucene Regardless of the data source format , As long as it can be converted into text form , You can Lucene Analyzed and utilized . That is to say, whether it is MS word, HTML, PDF Or other forms of files, as long as you can extract text content from them Lucene Used . You can use Lucene Index and search for them .

The above detailed introduction has detailed descriptions in Lucene China. I have also uploaded a copy here. You can also click to download
Paste a simple example belowCode:

Using System;
Using System. configuration;
Using System. Data;
Using System. LINQ;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. xml. LINQ;
Using System. text;
Using System. IO;

UsingLucene. net. documents;
UsingLucene. net. index;
UsingLucene. net. search;
UsingLucene. net. queryparsers;
UsingLucene. net. analysis. Standard;

Public partial class _ default: system. web. UI. page
{

Protected VoidPage_load (ObjectSender, eventargs E)
{
If(!Ispostback)
Textbox3.text=Server. mappath ("Doc");
}

# Region Create an index
Protected   Void Button2_click ( Object Sender, eventargs E)
{
String Index_store_path = Server. mappath ( " Index " ); // Index_store_path is the index storage directory.
String Index_path = Textbox3.text; // Index_path is the search directory

Indexwriter writer =   Null ;

Writer= NewIndexwriter (index_store_path,NewStandardanalyzer (),True);

Indexdirectory (writer,NewFileinfo (index_path ));
Writer. Optimize ();
Writer. Close ();

Textbox1.text= "Tip: Index completed \ n";
}

Public   Void Indexdirectory (indexwriter writer, fileinfo file)
{
If (Directory. exists (file. fullname ))
{
String [] files = Directory. getfilesystementries (file. fullname );
If (Files ! =   Null )
{
For ( Int I =   0 ; I < Files. length; I ++ )
{
Indexdirectory (writer, New Fileinfo (files [I]); // Here is a recursion
}
}
}
Else   If (File. Extension =   " . Txt " )
{
Indexfile (file, writer );
}
}
Private   Void Indexfile (fileinfo file, indexwriter writer)
{
Document Doc =   New Document ();

Doc. Add (NewField ("Filename", File. fullname, field. Store. Yes, field. Index. un_tokenized ));

Doc. Add (NewField ("Contents",NewStreamreader (file. fullname, system. Text. encoding. Default )));

Writer. adddocument (DOC );
}

# Endregion

# Region Search
Protected   Void Button#click ( Object Sender, eventargs E)
{
String Index_store_path = Server. mappath ( " Index " ); // Index_store_path is the index storage directory.
String Keyword = Textbox2.text; // Search keywords
Indexsearcher searcher;

Searcher= NewIndexsearcher (index_store_path );

Queryparser Q= NewQueryparser ("Contents",NewStandardanalyzer ());

Query=Q. parse (keyword );

Hits hits=Searcher. Search (query );

Textbox1.text= "The search result is" +Hits. Length ()+ "\ N";

If (Hits ! =   Null )
{
For ( Int I =   0 ; I < Hits. Length (); I ++ )
{
Document Doc = Hits. DOC (I );
Textbox1.text = Textbox1.text +   " The "   + (I +   1 ) +   " Search results, file path: "   + Doc. Get ( " Filename " ) +   " \ N " ;
}
}

Searcher. Close ();
}

# Endregion


}

Download the sample code: Click Here (vs. net2008)
Lucene.net version 2.0.0.4, which can be downloaded on Lucene's website: http://incubator.apache.org/lucene.net/
As of today, Lucene's Java version is 2.3 and. Net version is 2.0.

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.