How to analyze and create indexes in a project
1. Add references to e.net DLL and namespace
Using Lucene. net. analysis. Standard;
Using Lucene. net. documents;
Using Lucene. net. index;
Using Lucene. net. queryparsers;
Using Lucene. net. search;
Using classlibrary1.icons;
Using classlibrary1.parsing;
2. Declare the path for storing the index file in the page load event. The program will remember the file path during the page lifecycle, and then call the indexbuilt and search functions for query.
This. pathindex = path. Combine (environment. getfolderpath (environment. specialfolder. localapplicationdata), "test ");
Indexbuilt ();
Search ();
3. The following is the content of the indexbuilt function. It recursively adds folders and then indexes the directory information. Private void indexbuilt ()
{
// The following Code creates a new index on the disk.
Indexwriter = new indexwriter (this. pathindex, new standardanalyzer (), true );
Bytestotal = 0;
Counttotal = 0;
Countskipped = 0;
Directoryinfo di = new directoryinfo (@ "C: \ Inetpub \ wwwroot \ test \ resumes ");
Datetime start = datetime. now;
Addfolder (DI );
String summary = string. Format ("done. Indexed {0} files ({1} bytes). Skipped {2} Files.", counttotal, bytestotal, countskipped );
Summary + = string. Format ("took {0}", (datetime. Now-Start ));
Indexwriter. Optimize ();
Indexwriter. Close ();
}
4. search functions
Private void search ()
{
Datetime start = datetime. now;
Try
{
Searcher = new indexsearcher (this. pathindex );
}
Catch (ioexception ex)
{
MessageBox. Show ("the index doesn't exist or is damaged. Please rebuild the index. \ r \ n \ r \ ndetails: \ r \ n" + ex. Message );
Return;
}
Query query = queryparser. parse ("test", "text", new standardanalyzer ());
Hits hits = searcher. Search (query );
For (INT I = 0; I {
// Obtain the document from the index
Document Doc = hits. DOC (I );
// Create a new row in the result set
String filename = Doc. Get ("title ");
String Path = Doc. Get ("path ");
String folder = path. getdirectoryname (PATH );
Directoryinfo di = new directoryinfo (folder );
STR + = filename + ","
}
Searcher. Close ();
Response. Write (STR );
String searchreport = string. Format ("Search took {0}. Found {1} items.", (datetime. Now-Start), hits. Length ());
}
Http://www.dotlucene.net/Created by jecray