[Translation] Use Lucene. Net for Recursive document Query

Source: Internet
Author: User
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

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.