ASP.NET网站全文检索(代码部分)

 

前文 ASP.NET网站全文检索  主要是收集整理的一些资料,本文是使用Microsoft Indexing Service进行网站全文检索的代码部分,配置部分见这里。 

ASPX页面代码如下:

< tr >
    
< td  class ="record"  height ="20" > 关 键 词:
    
< asp:textbox  id ="txtQuery"  accessKey ="Q"  runat ="server"  Width ="250px"  MaxLength ="50" ></ asp:textbox > &nbsp;
    
< asp:button  id ="btnSearch"  runat ="server"  Text ="搜索"  EnableViewState ="False" ></ asp:button ></ td >
</ tr >
< tr >
    
< td  height ="10" > 查询方式:
    
< asp:dropdownlist  id ="cboQueryType"  accessKey ="T"  runat ="server"  Width ="250px"  EnableViewState ="False" >
    
< asp:ListItem  Value ="All"  Selected ="True" > 包含全部的字词 </ asp:ListItem >
    
< asp:ListItem  Value ="Any" > 包含任何一个字词 </ asp:ListItem >
    
< asp:ListItem  Value ="Boolean" > 布尔表达式查询 </ asp:ListItem >
    
< asp:ListItem  Value ="Exact" > 全字匹配 </ asp:ListItem >
    
< asp:ListItem  Value ="Natural" > 自然语言查询 </ asp:ListItem >
    
</ asp:dropdownlist >
    
</ td >
</ tr >
< tr >
    
< td  class ="record"  height ="20" > 查询范围:
    
< asp:dropdownlist  id ="cboDirectory"  accessKey ="D"  runat ="server"  Width ="250px"  EnableViewState ="False" >
    
< asp:ListItem  Value ="/"  Selected ="True" > 整个网站 </ asp:ListItem >
    
</ asp:dropdownlist >
    
</ td >
</ tr >
< tr >
    
< td  class ="record"  height ="20" >< asp:label  id ="lblResultCount"  runat ="server"  Font-Italic ="True"  visible ="False"  EnableViewState ="False" ></ asp:label ></ td >
</ tr >
< tr >
    
< td  vAlign ="top"  height ="210" >
    
< asp:datagrid  id ="dgResultsGrid"  runat ="server"  PageSize ="15"  AllowPaging ="True"  AutoGenerateColumns ="False"
        Visible
="False"  GridLines ="None"  EnableViewState ="False" >
        
< ItemStyle  HorizontalAlign ="Left"  VerticalAlign ="Top" ></ ItemStyle >
        
< HeaderStyle  Font-Bold ="True" ></ HeaderStyle >
        
< Columns >
            
< asp:TemplateColumn  HeaderText ="排名" >
                
< HeaderStyle  Width ="40px" ></ HeaderStyle >
                
< ItemTemplate >
                    
<% # (cint(DataBinder.Eval(Container, "DataSetIndex"))) + 1  %>
                    
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                    
< asp:TemplateColumn  HeaderText ="文档信息" >
                    
< ItemStyle  HorizontalAlign ="Left"  VerticalAlign ="Top" ></ ItemStyle >
                    
< ItemTemplate >
                    
< p >
                    
< href ='<%#  DataBinder.Eval(Container.DataItem, "VPath")% > ' target="_blank">
                    
<% # GetTitle(Container.DataItem) %>
                    
</ a >
                    
< br >
                    
<% # GetCharacterization(Container.DataItem) %>
                    
< br >
                    
< i >< href ='<%#  DataBinder.Eval(Container.DataItem, "VPath")% > ' target="_blank">http:// <% # Request.ServerVariables("SERVER_NAME") %><% # DataBinder.Eval(Container.DataItem, "VPath") %> </ a >
                    -
                    
<% # GetFileSize(Container.DataItem) %>
                    k 
</ i >
                    
</ p >
                
</ ItemTemplate >
            
</ asp:TemplateColumn >
            
</ Columns >
        
< PagerStyle  Visible ="False" ></ PagerStyle >
    
</ asp:datagrid >
    
</ td >
</ tr >

后台代码,使用VB.NET书写:

 

  '  绑定查询结果
         Private   Sub BindSearch()

            
Dim dbAdapter As OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
            
Dim oleDbSelectCommand1 As OleDbCommand = New System.Data.OleDb.OleDbCommand
            
Dim dbConnection As OleDbConnection = New System.Data.OleDb.OleDbConnection
            
Dim ds As DataSet = New DataSet("Results")
            
Dim rows As Integer = 0

            dbAdapter.SelectCommand 
= oleDbSelectCommand1
            oleDbSelectCommand1.Connection 
= dbConnection
            dbConnection.ConnectionString 
= "Provider=MSIDXS.1;Integrated Security .='';Data Source=Web"

            Try
                dbAdapter.SelectCommand.CommandText 
= Command
                
If CStr(ViewState("KEY")) <> "" Then
                    dbAdapter.Fill(ds)
                    rows 
= ds.Tables(0).Rows.Count
                
Else
                    lblResultCount.Text 
= "请输入关键词进行查询!"
                End If

                
If Not ds Is Nothing AndAlso rows > 0 Then
                    
' 自定义分页,与查询逻辑无关
                    ViewState("TOTALROWS"= rows
                    CalculatePage()

                    dgResultsGrid.AllowPaging 
= True
                    dgResultsGrid.PageSize 
= CInt(ViewState("PAGESIZE"))
                    dgResultsGrid.CurrentPageIndex 
= CInt(ViewState("PAGEINDEX")) - 1

                    lblResultCount.ForeColor 
= Color.Black
                    lblResultCount.Text 
= String.Format("找到 {0} 个相关网页", rows)

                    dgResultsGrid.DataSource 
= ds
                    dgResultsGrid.DataBind()
                    dgResultsGrid.Visible 
= (rows > 0)
                
Else
                    ViewState(
"TOTALROWS"= 0
                    CalculatePage()
                
End If

            
Catch ex As Exception
                lblResultCount.ForeColor 
= Color.Red
                lblResultCount.Text 
= String.Format("无法执行特定的查询: {0}", ex.Message)
                dgResultsGrid.Visible 
= False
            
Finally
                lblResultCount.Visible 
= True
            
End Try
            ' 自定义分页
            BindNavigate()

        
End Sub


 

Private   ReadOnly   Property Command() As String
            
Get
                
Dim query As String
                query 
= String.Format("SELECT Rank, VPath, DocTitle, Filename, Size, Characterization, Write FROM SCOPE('DEEP TRAVERSAL OF ""{0}""') WHERE  Not CONTAINS(FileName,'""*.txt"" OR ""*.js"" OR ""*.css"" OR ""*.config"" OR ""*.xml""')"CStr(ViewState("DIR")))

                
Dim type As String = CStr(ViewState("TYPE")).ToLower()
                
Dim fmt As String = " AND (CONTAINS('{0}') OR CONTAINS(DocTitle, '{0}'))"
                Dim text As String = CStr(ViewState("KEY")).Replace(";""").Trim

                
If type = "all" Or type = "any" Or type = "boolean" Then
                    
Dim words() As String = Split(text, " ")
                    
Dim len As Integer = words.Length
                    
Dim i As Integer

                    
For i = 0 To len - 1 Step i + 1
                        
Dim word As String = words(i)
                        
If type = "boolean" AndAlso (String.Compare(word, "and"True= 0 OrElse String.Compare(word, "or"True= 0 OrElse String.Compare(word, "not"True= 0 OrElse String.Compare(word, "near"True= 0Then
                        
ElseIf word <> "" Then
                            words(i) 
= String.Format("""{0}""", word)

                            
If i < len - 1 Then
                                
If type = "all" Then
                                    words(i) 
+= " AND"
                                ElseIf type = "any" Then
                                    words(i) 
+= " OR"
                                End If
                            
End If
                        
End If
                    
Next

                    query 
+= String.Format(fmt, String.Join(" ", words))

                
ElseIf type = "exact" Then
                    query 
+= String.Format(fmt, text)
                
ElseIf type = "natural" Then
                    query 
+= String.Format(" AND FREETEXT('{0}')", text)
                
End If

                query 
+= " ORDER BY Rank DESC"

                Return query
            
End Get
        
End Property

 

         '  如果网页没有Title,那么使用文件名
         Protected   Function GetTitle(ByVal value As ObjectAs Object
            
Dim title As String = Convert.ToString(DataBinder.Eval(value, "DocTitle"))

            
If Not (title Is NothingAnd title.Length > 0 Then
                
Return title '
            Else
                
Return DataBinder.Eval(value, "Filename")
            
End If
        
End Function
  ' GetTitle

        
'  取摘要
         Protected   Function GetCharacterization(ByVal value As ObjectAs String
            
Return Server.HtmlEncode(Convert.ToString(DataBinder.Eval(value, "Characterization")))
        
End Function


        
'  取文件尺寸,单位KB
         Protected   Function GetFileSize(ByVal value As ObjectAs String
            
Return Convert.ToString(CInt(Convert.ToInt32(DataBinder.Eval(value, "Size")) / 1000))
        
End Function


界面外观如下图:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值