高级用法
全文搜索
使用 Nuxt Content 在你的网站中实现全文搜索。
内置 FTS5 搜索
Content 模块提供 useSearchCollection,这是一个由 SQLite FTS5 驱动的零依赖组合式函数。它会基于你的内容章节构建倒排索引,并提供带前缀匹配和摘要的即时排序搜索。
SearchExample.vue
<script setup lang="ts">
const { status, search } = useSearchCollection('docs')
const query = ref('')
const results = ref([])
watch(query, async (value) => {
results.value = value
? await search(value, { snippet: { columns: ['content'], around: 40 } })
: []
})
</script>
<template>
<UInput v-model="query" :disabled="status !== 'ready'" placeholder="搜索..." />
<ul>
<li v-for="result in results" :key="result.id">
<NuxtLink :to="result.id">{{ result.title }}</NuxtLink>
<p v-if="result.snippets?.content" v-html="result.snippets.content" />
</li>
</ul>
</template>
外部库
你也可以使用 queryCollectionSearchSections 来获取原始章节数据,并将其传递给像 Fuse.js 或 MiniSearch 这样的搜索库。这种方式可以容忍拼写错误并进行模糊匹配,但代价是需要将所有章节加载到内存中。
MiniSearch
MiniSearch 是一个轻量级的全文搜索库,支持前缀匹配、模糊搜索和字段权重提升。
<script setup lang="ts">
import MiniSearch from 'minisearch'
const query = ref('')
const { data } = await useAsyncData('search', () => queryCollectionSearchSections('docs'))
const miniSearch = new MiniSearch({
fields: ['title', 'content'],
storeFields: ['title', 'content'],
searchOptions: {
prefix: true,
fuzzy: 0.2,
},
})
// 将数据添加到 MiniSearch 实例中
miniSearch.addAll(toValue(data.value))
const result = computed(() => miniSearch.search(toValue(query)))
</script>
<template>
<UContainer class="p-4">
<UCard>
<UInput v-model="query" placeholder="搜索..." />
<ul>
<li v-for="link of result" :key="link.id" class="mt-2">
<NuxtLink :to="link.id">{{ link.title }}</NuxtLink>
<p class="text-gray-500 text-xs">{{ link.content }}</p>
</li>
</ul>
</UCard>
</UContainer>
</template>
Fuse.js
Fuse.js 是一个模糊搜索库,使用 Bitap 算法进行可容忍拼写错误的匹配,并支持可配置阈值。
<script setup lang="ts">
import Fuse from 'fuse.js'
const query = ref('')
const { data } = await useAsyncData('search-data', () => queryCollectionSearchSections('docs'))
const fuse = new Fuse(data.value, {
keys: ['title', 'description']
})
const result = computed(() => fuse.search(toValue(query)).slice(0, 10))
</script>
<template>
<UContainer class="p-4">
<UCard>
<UInput v-model="query" placeholder="搜索..." class="w-full" />
<ul>
<li v-for="link of result" :key="link.item.id" class="mt-2">
<UButton variant="ghost" class="w-full" :to="link.item.id">
{{ link.item.title }}
<span class="text-gray-500 text-xs">
{{ link.item.content?.slice(0, 100) }}...
</span>
</UButton>
</li>
</ul>
</UCard>
</UContainer>
</template>
- Nuxt Content v3专为 Nuxt 开发者设计的强大基于 Git 的 CMS。 欢迎使用 Nuxt Content v3,这是一次重大升级,为您的 Nuxt 项目带来更高性能和创新功能。我们基于 Git 的 CMS 的最...
- 新功能...
- 内容集合集合用于组织项目中的相关项,帮助您更高效地管理大型数据集。主要优势包括: 结构化数据:在 content.config.ts 中配置数据库架构并定义集合类型安全查询:所有工具均支持 TypeScrip...
- 性能提升v2 中的一个主要挑战是用于存储文件的大体积包,特别影响无服务器部署。 v3 通过在生产环境中切换到基于 SQL 的存储解决了这一问题。此转换无需任何配置,支持开发模式、静态生成、服务器托管、无服务器...
- TypeScript 集成新的集合系统为您的所有数据自动生成 TypeScript 类型。所有工具和 API 都基于您的集合定义强类型,确保开发过程中的类型安全。...
- Nuxt Studio 集成 即将推出Nuxt Studio 与 v3 设计上完美互补。studio 模块现已直接集成到 Nuxt Content 中,打造理想环境,开发者专注编码,团队成员则通过直观界面管理内容。 我们很高兴您能探索这些...
- Content V2 迁移了解如何从 Content v2 迁移到 v3,请参见迁移指南。...
- 安装在你的 Nuxt 应用中开始使用 Nuxt Content v3。...
- 安装包选择你喜欢的包管理器来安装 Nuxt Content v3: pnpm add @nuxt/content yarn add @nuxt/content npm install @nuxt/conte...
- 注册模块在你的 nuxt.config.ts 文件中添加 Nuxt Content 模块: export default defineNuxtConfig({ modules: ['@nuxt/conte...
Nuxt UI
Nuxt UI 提供了一个 ContentSearch 组件,它由 fuse.js 驱动,并可直接接收 queryCollectionSearchSections 的输出。
<script setup lang="ts">
const { data: navigation } = await useAsyncData('navigation', () => queryCollectionNavigation('docs'))
const { data: files } = await useAsyncData('search', () => queryCollectionSearchSections('docs'))
const searchTerm = ref('')
</script>
<template>
<UContentSearch
v-model:search-term="searchTerm"
:files="files"
:navigation="navigation"
:fuse="{ resultLimit: 42 }"
/>
</template>