Use e.net to search and highlight search keywords

Source: Internet
Author: User
Tags createindex

Lucene. Net is a full-text search framework, not an application product.

For the two DLL files used, you can download them at http://d.download.csdn.net/down/2352061/taomanfor free.

Directly paste the Code:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "xxjs. aspx. cs" inherits = "xxjs_xxjs" %>

<% @ Register Assembly = "aspnetpager" namespace = "Wuqi. webdiyer" tagprefix = "webdiyer" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Div style = "height: 40px">
<Asp: button id = "btncreateindex" text = "create Index" runat = "server" onclick = "btncreateindex_click"/>
<Asp: Label id = "lbstatus" runat = "server"> </ASP: Label>
</Div>
<Table>
<Tr>
<TD> <asp: textbox id = "tbsearchcontent" runat = "server" Height = "20px" width = "155px"> </ASP: textbox> </TD>
<TD> <asp: button id = "btnsearch" runat = "server" text = "Search" onclick = "btnsearch_click"/> </TD>
</Tr>
</Table>
<Div id = "MSG" runat = "server" bordercolorlight = "#99cc99"
Bordercolordark = "#669933" bgcolor = "# dde3ce" style = "background-color: # c0c0c0"> </div>
<Div>
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false"
Width = "100%" gridlines = "NONE" allowpaging = "true"
Onpageindexchanging = "gridview1_pageindexchanging">
<Columns>
<Asp: templatefield>
<Itemtemplate>
<Table width = "100%" border = "0" align = "center" cellpadding = "5" cellspacing = "1">
<Tr>
<TD bgcolor = "# ffffff">
<Font size = "2"> <asp: hyperlink id = "hyperlink1" runat = "server" text = '<% # eval ("title ") %> 'navigateurl = '<% # eval ("link address") %>'> </ASP: hyperlink> </font>
</TD>
</Tr>
<Tr>
<TD>
<Font size = "2"> <% # This. getsubstring (eval ("content"). tostring () %> </font>
</TD>
</Tr>
</Table>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
</Div>
</Div>
</Form>
</Body>
</Html>
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

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. Data. sqlclient;
Using system. text;
Using system. IO;
Using system. collections;
Using USTC;

Using Lucene. net. documents;
Using Lucene. net. index;
Using Lucene. net. search;
Using Lucene. net. queryparsers;
Using Lucene. net. analysis. Standard;
Using Lucene. net. store;
Using Lucene. net. Highlight;
Using Lucene. net. analysis;

Public partial class xxjs_xxjs: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
// Obtain information only when the screen is read for the first time
Vsdatatable = new datatable ();
}
Pagedatabind (); // bind data
}

Protected void btncreateindex_click (Object sender, eventargs E)
{
Createindex ();
}

// Create an index
Public indexwriter createindex ()
{
String index_store_path = server. mappath ("Index"); // index_store_path is the index storage directory.
// Indexreader reader = indexreader. Open (index_store_path );
// Term term = new term (field, key );
// Reader. deletedocuments (TERM );
// Reader. Close ();
Indexwriter writer = NULL;
Try
{
Writer = new indexwriter (index_store_path, new standardanalyzer (), true );
DM dm = new DM ();
String strsql = "select c_title, c_text, c_url from v_searches ";
Dataset DS = DM. getsql (strsql );
// Create an index Field
Foreach (datarowview DRV in DS. Tables [0]. defaultview)
{
Document Doc = new document ();
Doc. Add (new field ("title", DRV ["c_title"]. tostring (), field. Store. Yes, field. Index. un_tokenized ));
Doc. Add (new field ("content", DRV ["c_text"]. tostring (), field. Store. Yes, field. Index. un_tokenized ));
Doc. Add (new field ("link address", DRV ["c_url"]. tostring (), field. Store. Yes, field. Index. un_tokenized ));
Doc. add (new field ("title and content", DRV ["c_title"]. tostring () + DRV ["c_text"]. tostring (), field. store. yes, field. index. tokenized); // No storage, index, title, and content index.
Writer. adddocument (DOC );
}
Writer. Optimize ();
Writer. Close ();

This. lbstatus. Text = "Index created successfully! ";
}
Catch
{
This. lbstatus. Text = "the index has been created. You do not need to create a new one! ";
}
Return writer;
}

// Information Retrieval
Protected void btnsearch_click (Object sender, eventargs E)
{
Bindlucenedata ();
}

Public void bind1_edata ()
{
String index_store_path = server. mappath ("Index"); // index_store_path is the index storage directory.
String keyword = tbsearchcontent. Text; // search for keywords
If (keyword! = "")
{
Hits myhit = NULL;
Indexsearcher mysea = new indexsearcher (index_store_path );
Analyzer analyzer = new standardanalyzer (); // defines the word Divider
Queryparser q = new queryparser ("title and content", analyzer );
Query query1 = Q. parse (keyword );

// Highlight
Booleanquery bquery = new booleanquery ();
Highlighter = new highlighter (New simplehtmlformatter ("<font color =/" Red/">", "</font>"), new queryscorer (query1 )); // use the highlighted keyword to display alternative keywords
Highlighter. settextfragmenter (New simplefragmenter (100 ));

Datetime start = datetime. now;
Myhit = mysea. Search (query1 );
Datetime end = datetime. now;
Long time = end. millisecond-start. millisecond;
This. MSG. innerhtml = "<font size = 2> about <B> <font color = blue>" + keyword + "</font> <B/> found <B> <font color = Red> "+ myhit. length () + "</font> </B>. search Duration: <font color = Red> "+ time +" </font> millisecond. </font> </BR> ";

If (myhit! = NULL)
{
Datarow myrow;
Datatable mytab = new datatable ();
Mytab. Columns. Add ("title ");
Mytab. Columns. Add ("content ");
Mytab. Columns. Add ("link address ");
Mytab. Clear ();
For (INT I = 0; I <myhit. Length (); I ++)
{
Document Doc = myhit. DOC (I );
Myrow = mytab. newrow ();
Myrow [0] = Doc. Get ("title"). tostring ();
Myrow [1] = Doc. Get ("content"). tostring ();
Myrow [2] = Doc. Get ("link address"). tostring ();

Lucene. net. analysis. tokenstream titlestream = analyzer. tokenstream ("title", new system. Io. stringreader (Doc. Get ("title"). tostring ()));
String titleresult = highlighter. getbestfragments (titlestream, Doc. Get ("title"). tostring (), 0 ,"...");
Lucene. net. analysis. tokenstream contentstream = analyzer. tokenstream ("content", new system. Io. stringreader (Doc. Get ("content"). tostring ()));
String contentresult = highlighter. getbestfragments (contentstream, Doc. Get ("content"). tostring (), 0 ,"...");
If (! String. isnullorempty (titleresult ))
{
Myrow [0] = titleresult;
}
If (! String. isnullorempty (contentresult ))
{
Myrow [1] = contentresult;
}
Mytab. Rows. Add (myrow );
Myrow. acceptchanges ();
}

Gridview1.datasource = mytab;
Gridview1.databind ();
Viewstate ["mydatatable"] = mytab;
}
Else
{
Response. Write ("hits is empty ");
}
Mysea. Close ();
}
Else
{
Return;
}
}

// Paging events
Protected void gridview1_pageindexchanging (Object sender, gridviewpageeventargs E)
{
Gridview1.datasource = vsdatatable;
Gridview1.pageindex = E. newpageindex;
Gridview1.databind ();
}

/// <Summary>
/// The datatable stored in viewstate
/// </Summary>
Private datatable vsdatatable
{
Get {return viewstate ["mydatatable"] As datatable ;}
Set {viewstate ["mydatatable"] = value ;}
}

/// <Summary>
/// Common binding materials
/// </Summary>
Private void pagedatabind ()
{
Gridview1.datasource = vsdatatable;
Gridview1.databind ();
}

/// <Summary>
/// Add a page for binding materials
/// </Summary>
/// <Param name = "getpageindex"> new page index </param>
Private void pagedatabind (int32 getpageindex)
{
Gridview1.datasource = vsdatatable;
Gridview1.pageindex = getpageindex;
Gridview1.databind ();
}

Public String getsubstring (string Str)
{
If (Str. length> 200)
STR = Str. substring (0,197) + "...";
Return STR;
}

}

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.