« 搜索引擎的缓存机制 | Main | ASP生成静态页面的方法 »

用ASP做全文检索

摘要:通过词句查找相关的全文是浏览器端常用的方法,ASP类似于CGI,也可以进行简单的文本方式全文检索

  在 Internet/Intranet上对某类信息做详细浏览和全文检索也是常碰到的情况。ASP是微软公司开发的用于制作动态Web页的一种服务器端脚本型式(或曰编写环境)。它使用VBScript或JavaScript等简易的脚本语言,集成在HTML码中,在服务器端响应浏览器端请求而执行数据访问和过程(或对象)调用并产生浏览器端执行的脚本语言。本文介绍用ASP编写全文检索代码的方法。

方法介绍:
  确定被检索的文件。将它们保存在同一文件夹中。文件的类型可以是任何文本类型(包括.htm网页文件类型)
  建立被检索文件的信息库。其中主要包含文件名,文件标题等。
  用ADO方式访问数据库。建立记录集对象。
  逐个打开记录集中当前记录所指向的文件。用查找的字串对文件的全文本进行匹配比较。
  将符合条件的文件标题输出到客户端Web页,并加入指向该文件的超链,以供用户选择浏览。
  技术要点:
  数据源。要在创建脚本之前,利用Windows95控制面板中的ODBC指定要访问的数据库及其驱动程序,以数据源名(DSN)标识之。
  匹配字串的长度。由于浏览器端的数据经过编码传到服务器端,ASP通过其内建对象Request取得的数据也并非完全是用户输入的有用信息
,所以要加以处理。可以在输入字串后附加一标志字符(如#或chr(7))。本文则采取另一种方法,即计算有效信息的长度。从而截取所需的
字串。
  创建脚本的文件系统对象Scripting.FileSystemObject,从而打开文本流以读入。
  特点:
  直接对.htm文件进行全文检索,省却许多转换过程。.htm文件又用于网页的显示,一举两得。
  应用ASP,简单明了,易于实现。
  本例ASP代码:

引用自
〈%@LANGUAGE="VBSCRIPT"%〉
  〈html〉
  〈head〉
  〈metaNAME="GENERATOR"Content="MicrosoftFrontPage3.0"〉
  〈metaHTTP-EQUIV="Content-Type"content="text/html;charset=gb_2312-80"〉
  〈title〉DocumentTitle〈/title〉
  〈metaname="MicrosoftBorder"content="tl,default"〉〈/head〉
  〈bodyBACKGROUND="/ie/images/watermrk.gif"BGPROPERTIES="FIXED"BGCOLOR="#00FFFF"〉
  〈%
  mousepointer=13
  SetConn1=Server.CreateObject("ADODB.Connection")
  conn1.open"DSN=cznj"
  Setrcst1=conn1.execute("SELECT*FROMpages_catalogue")
  sch_str=request.form("text1")
  doubbytes=len(sch_str)\2
  sch_str=left(sch_str,doubbytes)
  Response.Write"〈strong〉〈CENTER〉〈H2〉查找结果〈/H2〉〈HR〉〈Br〉〈/strong〉"
  Dimresult
  result=false
  Response.Write"〈UL〉"
  dowhilenotrcst1.eof
  tit=rcst1.fields("file_title")
  fn=rcst1.fields("file_name")
  file_name=Server.MapPath("/")&"\njweb\private\"&fn
  to_find=text_match(file_name,sch_str)
  ifto_findthen
  url="private/"&fn
  Response.Write"〈LEFT〉〈LI〉〈H4〉〈AHREF="&url&"〉"&tit
  endif
  rcst1.movenext
  result=resultorto_find
  loop
  Response.Write"〈/UL〉"
  ifnotresultthen
  Response.Write"〈H3〉〈I〉未找到!〈/I〉〈/H3〉"
  endif
  mousepointer=0
  %〉
  〈scriptRUNAT="Server"LANGUAGE="VBScript"〉
  functiontext_match(filename,search_string)
  dimretstring
  dimfind_pos
  Dimfso,a
  dimdone
  text_match=false
  Setfso=Server.CreateObject("Scripting.FileSystemObject")
  Seta=fso.OpenTextFile(filename,1,FALSE)"ForReading--〉1
  done=a.AtEndOfStreamortext_match
  DoWhilenotdone
  retstring=a.ReadLine
  find_pos=instr(retstring,search_string)
  iffind_pos〉0thentext_match=true
  done=a.AtEndOfStreamortext_match Loop
  a.Close
  endfunction
  〈/script〉
  〈/body〉
  〈/html〉

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on October 31, 2005 6:11 AM.

The previous post in this blog was 搜索引擎的缓存机制.

The next post in this blog is ASP生成静态页面的方法.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.