本文為技術概念驗證(PoC),**非生產級方案**。生產環境請使用 SQL Server 內建全文檢索、Elasticsearch、Solr 等成熟搜尋引擎。
.0、.1...)。字串: "山寨索引" (Unicode bytes: 113 92 232 91 34 125 21 95)
對應 4-gram 目錄與檔案:
../113/92/232/91/pk001.0 -- 「山寨」位置 0
../232/91/34/125/pk001.1 -- 「寨索」位置 1
../34/125/21/95/pk001.2 -- 「索引」位置 2
../232/91/34/125/ 下所有檔名(主鍵+.1)。../34/125/21/95/ 是否存在同主鍵 .2 檔案(位置連續性驗證)。public partial class UserDefinedFunctions
{
static string root = @"d:/index";
[SqlFunction]
public static SqlBoolean SetRoot(SqlString path)
{
if (path.IsNull) return false;
return Directory.Exists(path.Value) ? (root = path.Value, true) : false;
}
[SqlFunction]
public static SqlInt32 SetIndex(SqlString key, SqlString indexName, SqlString content)
{
if (key.IsNull || content.IsNull || indexName.IsNull) return 0;
return _setIndex(key.Value, indexName.Value, content.Value);
}
[SqlFunction(TableDefinition = "pk nvarchar(900)", FillRowMethodName = "FillRow")]
public static IEnumerable GetIndex(SqlString word, SqlString indexName)
{
if (word.IsNull || indexName.IsNull) return new List();
return _getIndex2(word.Value, indexName.Value); // 分詞、多級目錄交集
}
public static void FillRow(object obj, out SqlString pk)
=> pk = obj.ToString();
// 內部實作:Unicode 取 4 bytes 建目錄、寫 0-byte 檔、查詢時交集過濾
// 完整代碼見原文...
}
MyFullIndex.dll。sp_configure 'clr enabled', 1; RECONFIGURE;CREATE ASSEMBLY MyFullIndex FROM 'd:\MyFullIndex.dll' WITH PERMISSION_SET = EXTERNAL_ACCESS;CREATE FUNCTION xfn_SetMyIndexFileRoot(@path nvarchar(4000)) RETURNS bit AS EXTERNAL NAME MyFullIndex.UserDefinedFunctions.SetRoot; 等。