《游戏编辑器技术原理与架构》10.2 搜索与过滤

资产一多浏览器就要能搜。本节实现 AssetSearch 轻量索引——名称包含(大小写不敏感)+ 类型过滤 + 多标签 AND 过滤;6 条用例本机真跑,钉住「名称包含、大小写不敏感、类型过滤、多标签 AND、空查询全量」五条搜索语义。

10.2 搜索与过滤

资产一多,浏览器就要能搜。本节的 AssetSearch 维护一个轻量索引(id → 名称/类型/标签),search 做三件事的叠加:名称包含(大小写不敏感)、类型过滤、多标签 AND 过滤。

  • AssetSearch.add(entry) / remove(id):维护索引。
  • search(query?, type?, tags?):按三条件过滤,返回快照数组。

本章与站内既有内容的分工

主题站内既有内容本节做什么
资产浏览器helio 11.3 资产浏览器与关卡编辑浏览器给搜索框;本节提供索引与查询
虚拟文件系统10.1 虚拟文件系统与浏览VFS 管路径树;搜索是路径之外的另一条访问路径
标签10.3(下一节)search 的 tags 过滤消费 TagStore 的标签

搜索是索引问题,不是扫描问题

朴素搜索 = 每次键入都扫全量资产比对。资产上千后,每个按键都扫一遍开始卡。AssetSearch 的思路:搜索发生在内存索引上,不重扫磁盘。索引由 add 增量维护(资产导入时顺手入索引),search 只在索引上过滤。

真正的工业级搜索还会加倒排索引(关键词 → 资产列表)和模糊匹配,但过滤语义与索引形式解耦——先有 search(query, type, tags) 这个纯过滤函数,后续换用倒排索引只是替换内部结构。这是本节的边界:先把搜索语义钉对,性能优化是后面的档。

对标名称、类型和标签查询

Unity 的 AssetDatabase.FindAssets 支持名称查询、t: 类型和 l: 标签过滤,也可限定搜索目录。Unreal Content Browser 可配置筛选是否递归应用。helio 将名称、类型和标签作为独立输入,再按本节约定合并条件。相同筛选维度在不同产品中的组合规则应分别核对。 Unity FindAssets · Unreal Content Browser Settings

可运行产物:6 条用例

下面 src/asset-search.ts 层级 L1,完整可编译,命令 node --test test/10-2-asset-search.test.ts:

// file: src/asset-search.ts
// tier: L1
// verify: node --test test/10-2-asset-search.test.ts
//
// 资产管线(第十章):内容浏览器深化——搜索与过滤。
// 资产一多,浏览器就要能搜:按名称、按类型、按标签。AssetSearch 维护一个
// 轻量索引(id → 名称/类型/标签),search 做名称包含(大小写不敏感)+
// 类型过滤 + 多标签 AND 过滤。搜索结果是索引快照,不重扫磁盘。

export interface AssetIndexEntry {
  id: string;
  name: string;
  type: string;
  tags: string[];
}

export class AssetSearch {
  readonly #entries = new Map<string, AssetIndexEntry>();

  add(entry: AssetIndexEntry): void {
    this.#entries.set(entry.id, entry);
  }

  remove(id: string): void {
    this.#entries.delete(id);
  }

  count(): number {
    return this.#entries.size;
  }

  /**
   * 搜索:名称包含 query(大小写不敏感),并按 type 与 tags 过滤。
   * 多个标签为 AND 语义——条目必须全含。
   */
  search(query?: string, type?: string, tags?: string[]): AssetIndexEntry[] {
    const q = query?.trim().toLowerCase() ?? '';
    const needTags = tags?.length ? tags : undefined;
    const out: AssetIndexEntry[] = [];
    for (const entry of this.#entries.values()) {
      if (q && !entry.name.toLowerCase().includes(q)) continue;
      if (type && entry.type !== type) continue;
      if (needTags && !needTags.every((t) => entry.tags.includes(t))) continue;
      out.push(entry);
    }
    return out;
  }
}

过滤的顺序:名称 → 类型 → 标签。这个顺序不改变结果(都是 AND),但让最便宜的名称过滤先跑——早筛早走,后两个条件只在必要时评估。query 空字符串时 q 为 falsy,名称过滤跳过(测试第五条钉住「空查询返回全部」)。

可运行产物:6 条用例

下面是 test/10-2-asset-search.test.ts 层级 L1,命令 node --test test/10-2-asset-search.test.ts:

// file: test/10-2-asset-search.test.ts
// tier: L1
// verify: node --test test/10-2-asset-search.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { AssetSearch, type AssetIndexEntry } from '../src/asset-search.ts';

function hero(): AssetIndexEntry {
  return { id: 'hero', name: 'Hero', type: 'model', tags: ['character', 'player'] };
}

function heroTex(): AssetIndexEntry {
  return { id: 'hero-tex', name: 'hero_tex', type: 'texture', tags: ['character'] };
}

test('add 与 search 名称包含', () => {
  const s = new AssetSearch();
  s.add(hero());
  s.add(heroTex());
  assert.deepEqual(s.search('hero').map((e) => e.id).sort(), ['hero', 'hero-tex']);
});

test('搜索大小写不敏感', () => {
  const s = new AssetSearch();
  s.add(hero());
  assert.equal(s.search('HERO').length, 1);
  assert.equal(s.search('hero').length, 1);
});

test('类型过滤', () => {
  const s = new AssetSearch();
  s.add(hero());
  s.add(heroTex());
  const models = s.search('hero', 'model');
  assert.equal(models.length, 1);
  assert.equal(models[0].id, 'hero');
});

test('多标签 AND 过滤', () => {
  const s = new AssetSearch();
  s.add(hero());
  s.add(heroTex());
  assert.equal(s.search(undefined, undefined, ['character', 'player']).length, 1);
  assert.equal(s.search(undefined, undefined, ['character']).length, 2);
});

test('query 为空返回全部(按加入序)', () => {
  const s = new AssetSearch();
  s.add(hero());
  s.add(heroTex());
  assert.equal(s.search().length, 2);
});

test('remove 后消失;count 反映索引大小', () => {
  const s = new AssetSearch();
  s.add(hero());
  s.add(heroTex());
  s.remove('hero');
  assert.equal(s.count(), 1);
  assert.equal(s.search('hero').length, 1);
});

本机实测(Node v26.5.0,node --test test/10-2-asset-search.test.ts):tests 6、pass 6、fail 0,耗时约 50 ms。

第一条「名称包含」同时命中两个 hero;第四条把 AND 语义钉死——['character','player'] 只剩 hero(hero-tex 缺 player),['character'] 命中两个。第三条类型过滤与第六条移除后消失各守一边。

小结

  • 搜索是索引问题,不是扫描问题——增量索引,纯过滤查询。
  • search(query, type, tags):名称包含 + 类型 + 多标签 AND。
  • 名称 → 类型 → 标签的过滤顺序,便宜的先跑。
  • 先钉搜索语义,倒排索引/模糊匹配是后档。
  • 下一节给资产上标签与收藏:标签归集与收藏集。

阅读导航:上一节:10.1 虚拟文件系统与浏览 · 下一节:10.3 标签与收藏 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析·向量数据库与 AI 检索内核》 · 目录与学习路径(45 篇)
  2. 《大规模分布式计算深入解析》15.3 自研向量引擎:可执行骨架与工程缺口
  3. 《大规模分布式计算深入解析》14.1 Milvus:组件架构、段管理与检索链路