asp简单的搜索引擎代码

2013 年 8 月 11 日4270

  

  下面是库中URLINDEX表:URL和Keywords字段分别添加了索引.

  URL 文本 (索引:有(无重复))Title 文本Description文本Summary 文本Keywords 文本(索引:有(无重复))

  doquery.ASP

  HTMLHEADTITLE简单搜索引擎/TITLE/HEADBODY BGCOLOR=#ffffff MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN=0 TOPMARGIN=0

  FORM METHOD="post" ACTION="doquery.asp?act=search"Query: INPUT TYPE="Text"BRINPUT TYPE="Submit" VALUE="Submit"/FORM/CENTER

  %dim actact=request("act")if(act="search") thenQueryString = Request.form( "QueryString" )QueryWords = Split( QueryString )strIndent = "nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;"' 如果搜索为空则返回If QueryString = "" ThenResponse.Redirect( "default.asp" )End IfSession.timeout = 2If IsObject(Session("sitesearch_conn")) Then Set conn = Session("sitesearch_conn")Else Set conn = Server.CreateObject("ADODB.Connection") conn.open "driver={Microsoft Access Driver (*.mdb)};dbq=" Server.MapPath("database/SiteSearch.mdb"),"","" Set Session("sitesearch_conn") = connEnd If

  ' 查询语句sql = "SELECT * FROM [URLIndex] WHERE"

  '搜索Description字段sql = sql " ( [Description] LIKE '%" QueryWords( 0 ) "%'"' FirstFor i = LBound( QueryWords ) + 1 to UBound( QueryWords )If QueryWords( i ) "" and UCase( QueryWords(i) ) "OR" and UCase( QueryWords(i) ) "AND" ThenIf uCase( QueryWords( i-1 ) ) = "OR" Thensql = sql " OR [Description] LIKE '%" QueryWords( i ) "%'"Elsesql = sql " AND [Description] LIKE '%" QueryWords( i ) "%'"End IfEnd IfNext

  ' 搜索Keywords字段 sql = sql " ) OR ( [Keywords] LIKE '%" QueryWords( 0 ) "%'"For i = LBound( QueryWords ) + 1 to UBound( QueryWords )If QueryWords( i ) "" and UCase( QueryWords(i) ) "OR" and UCase( QueryWords(i) ) "AND" ThenIf uCase( QueryWords( i-1 ) ) = "OR" Thensql = sql " OR [Keywords] LIKE '%" QueryWords( i ) "%'"Elsesql = sql " AND [Keywords] LIKE '%" QueryWords( i ) "%'"End IfEnd IfNext

  ' 搜索Title字段 sql = sql " ) OR ( [Title] LIKE '%" QueryWords( 0 ) "%'"For i = LBound( QueryWords ) + 1 to UBound( QueryWords )If QueryWords( i ) "" and UCase( QueryWords(i) ) "OR" and UCase( QueryWords(i) ) "AND" ThenIf uCase( QueryWords( i-1 ) ) = "OR" Thensql = sql " OR [Title] LIKE '%" QueryWords( i ) "%'"Elsesql = sql " AND [Title] LIKE '%" QueryWords( i ) "%'"End IfEnd IfNext

  ' 搜索Summary字段 sql = sql " ) OR ( [Summary] LIKE '%" QueryWords( 0 ) "%'"For i = LBound( QueryWords ) + 1 to UBound( QueryWords )If QueryWords( i ) "" and UCase( QueryWords(i) ) "OR" and UCase( QueryWords(i) ) "AND" ThenIf uCase( QueryWords( i-1 ) ) = "OR" Thensql = sql " OR [Summary] LIKE '%" QueryWords( i ) "%'"Elsesql = sql " AND [Summary] LIKE '%" QueryWords( i ) "%'"End IfEnd IfNext

  sql = sql " )"

  ' Set rs = Server.CreateObject("ADODB.Recordset") rs.Open sql, conn, 3, 3 Response.Write "BRB 你搜索的是: /B " QueryString Response.Write "BRB 搜索的关键字: /B "For i = LBound( QueryWords ) to UBound( QueryWords )Response.Write "BR" strIndent i ": " QueryWords( i )Next

  ' Print the SQL String Response.Write "BRB sql 语句 : /B " sql' Print the Results Response.Write "BRB 结果nbsp;nbsp;nbsp;nbsp;: /B UL"On Error Resume Nextrs.MoveFirstDo While Not rs.eofResponse.Write "BR" "A HREF='OpenPage.asp?IndexURL=" rs.Fields("URL").Value "'" rs.Fields("Title") "/A - " Response.Write rs.Fields("Description") "BR"Response.Write "nbsp;nbsp;nbsp;nbsp;nbsp;FONT SIZE=2URL: " rs.Fields("URL") "/FONT"Response.Write "HR SIZE=1 WIDTH=200 ALIGN=LEFT"rs.MoveNextLoopResponse.Write "/UL"end if %

  /BODY/HTML

0 0