Bitzsoft.Integrations.Search 1.0.0-alpha.10

This is a prerelease version of Bitzsoft.Integrations.Search.
dotnet add package Bitzsoft.Integrations.Search --version 1.0.0-alpha.10
                    
NuGet\Install-Package Bitzsoft.Integrations.Search -Version 1.0.0-alpha.10
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bitzsoft.Integrations.Search" Version="1.0.0-alpha.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.Search" Version="1.0.0-alpha.10" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.Search" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitzsoft.Integrations.Search --version 1.0.0-alpha.10
                    
#r "nuget: Bitzsoft.Integrations.Search, 1.0.0-alpha.10"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bitzsoft.Integrations.Search@1.0.0-alpha.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitzsoft.Integrations.Search&version=1.0.0-alpha.10&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.Search&version=1.0.0-alpha.10&prerelease
                    
Install as a Cake Tool

Bitzsoft.Integrations.Search

全文检索引擎抽象层 — 统一接口定义、三段式检索上下文、业务规则矩阵与弱词过滤抽象。

功能特性

  • 引擎核心抽象ISearchEngine 覆盖索引全生命周期(实时 Upsert/Delete、keyset 分页全量 Rebuild、三段式 Search、GetStatistics)
  • 全量重建数据源ISearchIndexDataSource 采用 keyset(游标式)分页迭代,避免深分页 O(n) 性能退化
  • 三段式检索上下文SearchPerformInput 将 Query / Filter / Rule 职责分离,一套模型支持利冲审查、列表过滤、关键字检索等多场景
  • 业务规则矩阵BusinessRuleScopeBuilder 按「场景 × 关键词角色」生成二维约束(关键字允许命中的利冲方类型 / 允许搜索的字段),内置 6 种 SearchScenario
  • 分级关键词匹配KeywordMatchStrategy 按关键词数量自动切换算法(SmallSet / MediumSet / LargeSet / CachedTrie),>=500 使用 AhoCorasick 双数组 Trie + SHA256 缓存
  • 结果聚合管道SearchPipeline 基于 Channel + ConcurrentDictionary 按 (BusinessId, BusinessType) 聚合命中项,背压有界(容量 10000)
  • 命中等级计算HitLevelCalculator 按我方/对方/第三方角色分组计算红(3)/黄(2)/绿(1)/无(0) 四级冲突
  • 弱词过滤抽象ISegmentWordStore(LegalSuffix 实体法定后缀 / OrgWeak 弱组织词 / GeoWeak 弱地理词)、IStopWordProviderIExtensionWordProvider
  • 索引目录存储抽象ISearchIndexCatalogStore 管理多场景索引元数据(DirectoryName / 状态 / 版本 / 文档数),连接器包零 DB 依赖由宿主实现持久化

安装

.NET CLI

dotnet add package Bitzsoft.Integrations.Search

PackageReference

<PackageReference Include="Bitzsoft.Integrations.Search" Version="1.0.0" />

配置

本包为纯抽象层,不读取任何配置节点。宿主项目通过实现下述抽象接口提供运行时数据:

抽象接口 职责 是否必填
ISearchIndexCatalogStore 索引目录元数据持久化(IndexKey / DirectoryName / 状态 / 版本 / 文档数) 必填
ISearchIndexDataSource 全量重建数据源(keyset 分页迭代) 执行 Rebuild 时必填
ISegmentWordStore 弱词存储(LegalSuffix / OrgWeak / GeoWeak),建议 24h TTL 缓存 可选
IStopWordProvider 停用词提供者 可选
IExtensionWordProvider 扩展词提供者 可选

注册服务

本包仅定义抽象契约,不提供 DI 注册扩展方法。宿主项目需配合具体实现包(如 Bitzsoft.Integrations.Search.Lucene)注册引擎,并自行注册上述存储抽象的实现:

// 1. 注册 Lucene 引擎实现
services.AddBitzsoftLuceneSearch(opt => opt.BasePath = "/data/lucene");

// 2. 注册宿主实现的存储抽象
services.AddScoped<ISearchIndexCatalogStore, MyCatalogStore>();
services.AddScoped<ISegmentWordStore, MySegmentWordStore>();
services.AddScoped<IStopWordProvider, MyStopWordProvider>();

使用示例

实现全量重建数据源

using Bitzsoft.Integrations.Search;

public sealed class CaseConflictDataSource : ISearchIndexDataSource
{
    public async Task<IReadOnlyList<IndexDocumentBatch>> GetBatchAsync(
        string tenantId, string? lastCursor, int pageSize,
        CancellationToken cancellationToken = default)
    {
        // keyset 分页:按 Id 游标推进,避免深分页性能退化
        var rows = await _db.Cases
            .Where(c => c.TenantId == tenantId && c.Id.CompareTo(lastCursor) > 0)
            .OrderBy(c => c.Id)
            .Take(pageSize)
            .ToListAsync(cancellationToken);

        return rows.Select(c => new IndexDocumentBatch(
            c.Id.ToString(),
            new Dictionary<string, string?>
            {
                ["Name"] = c.Name,
                ["ForeignName"] = c.ForeignName,
                ["FormerName"] = c.FormerName,
                ["ConflictKeyword"] = c.Keyword
            },
            c.Id.ToString())).ToList();
    }
}

调用引擎写入与检索

using Bitzsoft.Integrations.Search;

public sealed class SearchService(ISearchEngine engine)
{
    // 实时增量写入
    public Task UpsertAsync(string caseId, string name, string tenantId, CancellationToken ct)
        => engine.UpsertAsync(
            indexKey: "conflict",
            tenantId: tenantId,
            documentId: caseId,
            fields: new Dictionary<string, string?>
            {
                ["Name"] = name,
                ["EntityType"] = "CaseMatter"
            },
            cancellationToken: ct);

    // 全量重建(原子替换,不中断检索服务)
    public Task<RebuildResult> RebuildAsync(ISearchIndexDataSource dataSource, string tenantId,
        CancellationToken ct) => engine.RebuildAsync("conflict", tenantId, dataSource, ct);

    // 三段式检索(利冲审查)
    public async Task<IReadOnlyList<SearchHitItem>> SearchConflictsAsync(
        string tenantId, string keyword, CancellationToken ct)
    {
        var input = new SearchPerformInput
        {
            IndexKey = "conflict",
            TenantId = tenantId,
            BusinessName = "ConflictCheck",
            Query = new SearchQueryContext
            {
                SearchFieldWordsDict = new()
                {
                    ["Name"] = new() { keyword },
                    ["FormerName"] = new() { keyword }
                },
                Mode = SearchMode.Fuzzy,
                MultiFieldCombineMode = FieldCombineMode.Should
            },
            Filter = new SearchFilterContext { ExcludeSourceBusinessId = true },
            Rule = new SearchRuleContext
            {
                Scenario = SearchScenario.ProcessRoleOpposing,
                Categories = new PartyCategories
                {
                    OurPartyCategories = { "Client" },
                    OppositePartyCategories = { "Opponent" },
                    ThirdPartyCategories = { "ThirdParty" }
                }
            }
        };

        var output = await engine.SearchAsync(input, ct);
        return output.Hits; // 已按 HitLevel 降序、按 BusinessId 聚合
    }
}

构建业务规则约束矩阵

// 根据场景 × 角色,生成每个关键字允许命中的利冲方类型与字段
var scope = BusinessRuleScopeBuilder.Build(
    scenario: SearchScenario.PreProcessRoleOpposing,
    categoryKeywordMap: new() { ["Client"] = new() { "张三" } },
    categories: new PartyCategories
    {
        OurPartyCategories = { "Client" },
        OppositePartyCategories = { "Opponent" }
    });

// scope.KeywordCategoryScopeMap:关键字 → 允许命中的利冲方类型
// scope.KeywordFieldScopeMap:关键字 → 允许搜索的字段

关键词匹配策略(离线/预检场景)

// 按关键词数量自动选择 SmallSet / MediumSet / LargeSet / CachedTrie
var strategy = KeywordMatchStrategy.Create(
    searchFieldWordsDict: new() { ["Name"] = new() { "张三", "李四" } },
    mode: SearchMode.Fuzzy);

var hits = strategy.Match(new() { ["Name"] = "张三的合同" });
// hits: (Keyword=张三, Field=Name) → "张三的合同"

核心类型一览

类型 说明
ISearchEngine 引擎核心契约(Upsert / Delete / Rebuild / Search / GetStatistics)
ISearchIndexDataSource 全量重建数据源(keyset 分页迭代)
ISearchIndexCatalogStore 索引目录元数据存储(宿主实现)
ISegmentWordStore 弱词存储抽象(LegalSuffix / OrgWeak / GeoWeak)
IStopWordProvider 停用词提供者抽象
IExtensionWordProvider 扩展词提供者抽象
SearchPerformInput 三段式检索输入(Query / Filter / Rule)
SearchQueryContext 查询上下文(字段→关键字映射 / 搜索模式 / 分页)
SearchFilterContext 过滤上下文(来源排除 / 利冲方类型白名单/黑名单)
SearchRuleContext 规则上下文(角色分组 / 场景 / 关键字类型映射)
SearchHitItem 聚合后的命中业务实体(含 HitLevel / MatchKeywords / StoredFields)
SearchOutput 检索输出(命中项列表 + 总数)
SearchMode 搜索模式枚举(Exact / Fuzzy / Wildcard / Regex / CustomEquals)
FieldCombineMode 多字段组合模式(Should = OR / Must = AND)
SearchScenario 检索场景枚举(6 种利冲/关键字场景,驱动约束矩阵)
PartyCategories 角色分组(我方 / 对方 / 第三方)
SegmentType 弱词类型枚举(LegalSuffix / OrgWeak / GeoWeak)
SegmentTypeBuckets 弱词类型分组集合
IndexCatalogEntry / IndexStatus 索引目录元数据 / 索引状态
IndexStats 索引统计信息(文档数 / 版本)
IndexDocumentBatch 全量重建批次项(DocumentId / Fields / Cursor)
RebuildResult 全量重建结果(已处理数 / 总数 / 是否完成)
BusinessRuleScopeBuilder 利冲检索业务约束矩阵构建器
KeywordMatchStrategy 分级关键词匹配策略(4 级 AC Trie)
SearchPipeline 检索结果聚合管道(Channel 背压)
HitLevelCalculator 命中等级计算器(红/黄/绿/无)

依赖

说明
Bitzsoft.Integrations.Compatibility 基础工具库(多目标框架 BCL 兼容封装)
NReco.Text.AhoCorasickDoubleArrayTrie 双数组 AhoCorasick Trie(KeywordMatchStrategy 大词集匹配)

相关包

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Bitzsoft.Integrations.Search:

Package Downloads
Bitzsoft.Integrations.Search.Lucene

Lucene.NET 全文检索引擎实现 — 拼音分词、NGram、索引生命周期、三段式检索编排

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.10 28 7/26/2026
1.0.0-alpha.9 62 7/12/2026
1.0.0-alpha.8 308 7/1/2026