Advanced
Full-Text Search
Implement full-text search in your website using Nuxt Content
Content module exposes a handy utility queryCollectionSearchSections to break down content files into searchable sections. This is useful for implementing full-text search in your website. You can use the result of this utility in combination with Nuxt UI Content Search or other search libraries like Fuse.js, minisearch, etc.
Nuxt UI
Nuxt UI provides a ready to use component for full-text search. You can use it by passing the result of queryCollectionSearchSections to the files prop of the component.
Read more about Nuxt UI Content Search.
<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>
MiniSearch example
Read more about 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,
},
})
// Add data to the MiniSearch instance
miniSearch.addAll(toValue(data.value))
const result = computed(() => miniSearch.search(toValue(query)))
</script>
<template>
<UContainer class="p-4">
<UCard>
<UInput v-model="query" placeholder="Search..." />
<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 example
Read more about Fuse.js.
<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="Search..." 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 v3The powerful Git-based CMS designed specifically for Nuxt developers. Welcome to Nuxt Content v3, a ...
- What's New?...
- Content CollectionsCollections organize related items within your project, helping you manage large datasets more effic...
- Improved PerformanceA significant challenge in v2 was the large bundle size needed for storing files, particularly affec...
- TypeScript IntegrationThe new collections system provides automatic TypeScript types for all your data. Every utility and ...
- Nuxt Studio Integration SoonNuxt Studio and v3 are designed to complement each other perfectly. The studio module is creating an...
- Content V2 MigrationLearn how to migrate from Content v2 to v3 in the migration guide....
- InstallationGet started with Nuxt Content v3 in your Nuxt application....
- Install the PackageChoose your preferred package manager to install Nuxt Content v3: pnpm add @nuxt/content yarn add @n...
- Register the ModuleAdd the Nuxt Content module to your nuxt.config.ts: export default defineNuxtConfig({ modules: ['@...