First, reference Lucene in the project. net. dll and then create an index (index creation page. aspx <div>
View Code
Protected void btnNew_Click (object sender, EventArgs e)
{
IList <contentsmodel> contents = Contents. GetContentsByCategory (18); // The set of table data is obtained here.
String indexDir = ConfigurationManager. AppSettings ["indexDir"]; // The path where the index is stored in web. config.
Analyzer analyzer = new StandardAnalyzer (global: Lucene. Net. Util. Version. paie_29 );
IndexWriter writer = new IndexWriter (indexDir, analyzer, true );
Try
{
Foreach (var item in contents)
{
If (IsContentLegal (item ))
{
Writer. AddDocument (GetDocument (item ));
}
}
Writer. SetMergeFactor (30 );
Writer. SetMaxBufferedDocs (30 );
Writer. Optimize ();
}
Catch
{
}
Finally
{
Writer. Close ();
}
}
// Filter non-conforming data
View Code
private bool IsContentLegal(ContentsModel contentInfo)
{
return contentInfo.Title != null &&
contentInfo.ContentBody != null;
}
Write the index column to the View Code in the index file.
Private Document GetDocument (ContentsModel contentModel)
{
Document doc = new Document ();
// Storage but not indexed is mainly used to build pages
Doc. Add (new Field ("cId", contentModel. Id. ToString (), Field. Store. YES, Field. Index. NO ));
Doc. Add (new Field ("content_thumbnail", contentModel. Thumbnail, Field. Store. YES, Field. Index. NO ));
Doc. Add (new Field ("issue_datetime", contentModel. Issue. ToString (), Field. Store. YES, Field. Index. NO ));
Doc. Add (new Field ("category_id", contentModel. CategoryId. ToString (), Field. Store. YES, Field. Index. NO ));
// Both storage and Indexing
Doc. Add (new Field ("content_title", contentModel. Title. ToString (), Field. Store. YES, Field. Index. ANALYZED ));
Doc. Add (new Field ("content_body", contentModel. ContentBody. ToString (), Field. Store. YES, Field. Index. ANALYZED ));
Return doc;
}
Result. aspx page displayed in the search Result
View Code
<div>
<%for (int i = 0; i < hitsOfpage.Count; i++){ %>
<ul>
<li>
<span><%=hitsOfpage[i].Get("content_title")%></span>
</li>
</ul>
<%} %>
</div>
Finally, the Result is displayed on the search Result page. aspx. cs // There is a paging. however, my pager class is used. useful SQL storage. so we will not post the paging code. view Code
1 public partial class Result: System. Web. UI. Page
2 {
3 const int PAGE_SIZE = 7;
4 protected int totalPageCount;
5 protected int currentPageIndex;
6 protected string searchp;
7 protected static List <Document> hitsOfpage = new List <Document> ();
8 protected void Page_Load (object sender, EventArgs e)
9 {
10 searchp = Request ["q"];
11 string pagenum = Request. QueryString ["page"];
12 if (pagenum = null)
13 {
14 pagenum = "0 ";
15}
16 int pageIndex = 1;
17
18 int. TryParse (pagenum. Trim (), out pageIndex );
19
20 if (pageIndex <1)
21 {
22 pageIndex = 1;
23}
24
25 Hits hits = SearchQuery (searchp );
26 currentPageIndex = pageIndex;
27 int count = hits. Length ();
28 // pagination
29 int pageCount = count/PAGE_SIZE + (count % PAGE_SIZE> 0? 1: 0 );
30 currentPageIndex = Math. Min (currentPageIndex, pageCount );
31 int startPos = Math. Max (currentPageIndex-1) * PAGE_SIZE, 0 );
32 int endPos = Math. Min (currentPageIndex * PAGE_SIZE-1, count-1 );
33 hitsOfpage. Clear ();
34 for (int I = startPos; I <= endPos; I ++)
35 {
36 hitsOfpage. Add (hits. Doc (I ));
37}
38 if (count % PAGE_SIZE! = 0)
39 {
40 totalPageCount = count/PAGE_SIZE + 1;
41}
42 else
43 {
44 totalPageCount = count/PAGE_SIZE;
45}
46}
// Query the index file based on search conditions and return the hits set View Code
Private Hits SearchQuery (string word)
{
String indexDir = ConfigurationManager. AppSettings ["indexDir"]; // the location of the index file.
String [] field = new string [] {"content_title", "content_body"}; // Coupled field
IndexReader reader = IndexReader. Open (indexDir, true );
IndexSearcher searcher = new IndexSearcher (reader );
MultiFieldQueryParser queryParser = new MultiFieldQueryParser (Lucene. Net. Util. Version. e_e_29, field, new StandardAnalyzer ());
Query query = queryParser. Parse (word );
Return searcher. Search (query );
}
Add another item on. if an exception is reported when you enter the operator, BooleanQuery is used to find the cause.
Private Hits SearchQuery (string querystring)
{
String indexDir = ConfigurationManager. configurettings ["indexDir"];
Analyzer analyzer = new StandardAnalyzer ();
// Construct BooleanQuery
QueryParser parser = new QueryParser ("content_title", analyzer );
BooleanQuery bquery = new BooleanQuery ();
TokenStream ts = analyzer. TokenStream (null, new StringReader (querystring ));
Lucene. Net. Analysis. Token token;
While (token = ts. Next ())! = Null)
{
Query query = parser. Parse (token. TermText ());
Bquery. Add (query, BooleanClause. Occur. MUST );
}
// Structure completed
IndexReader reader = IndexReader. Open (Server. MapPath (indexDir), true );
IndexSearcher searcher = new IndexSearcher (reader );
// Query query = parser. Parse (querystring );
// Output the expression we want to view
Return searcher. Search (bquery );
}