萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

ASP/VBScript 實現站內搜索引擎:文件系統遍歷與全文匹配

方案架構

前端 HTML 表單提交關鍵詞 → 後端 searchfiles.asp 遍歷指定目錄下的 .html/.htm 文件 → 讀取文件內容全文匹配關鍵詞 → 提取 <title> 作為結果標題 → 生成結果列表頁面。

前端頁面 searchfiles.html

<html>
<head><title>站內搜索</title></head>
<body>
<form method="POST" action="cgi-bin/searchfiles.asp">
    <table border="0" cellpadding="5" bgcolor="#C2E7E3">
        <tr>
            <td align="center">
                請輸入關鍵詞 <input type="text" name="term" size="15">
                <input type="submit" value="搜索">
            </td>
        </tr>
    </table>
</form>
</body></html>

後端處理 searchfiles.asp

<%
SearchTerm = Request.Form("term")

' 簡單過濾:拒絕過於簡單的查詢
Novalid = " a b c d e f g h i j k l m n o p q r s t u v w x y z yes no and more "
If InStr(Novalid, " " & SearchTerm & " ") > 0 Then
    Response.Write "查詢詞過於簡單,請返回重試!"
    Response.End
End If

Response.Write "

搜索結果

" FolderName = "FolderName" ' 目標搜索目錄名 Call ShowList() Sub ShowList() FolderToCheck = Server.MapPath("") & "/" & FolderName & "/" Dim fs, f, f1, fc, s Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder(FolderToCheck) Set fc = f.Files Dim SearchResponse SearchResponse = "" For Each f1 In fc Wfile = f1.Name ' 僅處理 HTML 文件 If Right(Wfile, 5) = ".html" Or Right(Wfile, 4) = ".htm" Then Wfile2 = FolderToCheck & Wfile Set fs2 = CreateObject("Scripting.FileSystemObject") Set a = fs2.OpenTextFile(Wfile2, 1) ' ForReading ct = a.ReadAll a.Close ' 大小寫不敏感匹配 ct2 = LCase(ct) SearchTerm2 = LCase(SearchTerm) If InStr(ct2, SearchTerm2) > 0 Then ' 提取 標籤內容作為結果標題 Dim titleText, startPos, endPos titleText = Wfile ' 默認用文件名 If InStr(ct, "") > 0 Then endPos = InStr(ct, "") - 1 startPos = InStr(ct, "") If startPos > 0 Then titleText = Mid(ct, startPos + 7, endPos - startPos - 6) End If End If ' 構建結果行 SearchResponse = SearchResponse & _ "<tr><td>" & titleText & "</td>" & _ "<td align=center valign=middle> " & _ "<A HREF=/" & FolderName & "/" & Wfile & "> 詳細 </A></td></tr>" End If End If Next ' 輸出結果頁面 Response.Write "<html><head><title>搜索結果
" Response.Write "
搜索結果
" Response.Write "" If SearchResponse <> "" Then Response.Write SearchResponse Else Response.Write "" End If Response.Write "
對不起,沒有找到匹配的內容!
" Response.Write "
嘗試專業搜索引擎 <A HREF=https://www.yahoo.com.cn/>Yahoo</A>
" Response.Write "
" End Sub %>

關鍵技術點分析

  1. 文件系統遍歷:FileSystemObject.GetFolder.Files 獲取文件集合,For Each 迭代。
  2. 全文讀取:OpenTextFile.ReadAll 一次性讀入內存,適用於小文件;大文件應改用逐行讀取。
  3. 標題提取:簡單字符串查找 <title>/</title>,脆弱(不支持屬性、跨行、嵌套),健壯方案應用 HTML 解析器。
  4. 大小寫不敏感:LCase 統一轉小寫比較。
  5. 結果拼接:字符串拼接 HTML,易產生 XSS 風險(未對標題轉義),現代框架應用模板引擎自動轉義。

性能與擴展性瓶頸

瓶頸影響解決方案
全量遍歷讀取O(N) 線性掃描,文件多時極慢建立倒排索引 (Lucene/Elasticsearch/Whoosh)
ReadAll 佔用內存大文件導致內存飆升流式讀取、索引時增量處理
無分頁結果集大時頁面巨大分頁顯示、無限滾動加載
無相關性排序結果僅按文件系統順序TF-IDF/BM25 評分排序
單機文件系統無法橫向擴展分布式搜索引擎集群

現代化重構路徑

  1. 索引構建:定時任務或實時監聽文件變更,調用 Whoosh (Python) 或 Lucene.NET 建立索引。
  2. 搜索服務:獨立搜索 API (Elasticsearch/Meilisearch/Typesense),提供 REST/GraphQL 接口。
  3. 前端交互:AJAX/Fetch 調用搜索 API,JSON 渲染結果,支持高亮、分頁、篩選。
  4. 安全加固:輸入驗證、輸出編碼、速率限制、索引權限控制。

總結

該 ASP 方案展示了「掃描文件→全文匹配→提取標題→展示結果」的最基礎搜索引擎雛形。雖然功能極簡、性能差、安全隱患多,但理解其核心流程有助於掌握搜索引擎基本原理,並為遷移至專業搜索平台提供業務參考基準。

copyright © 萬盛學電腦網 all rights reserved