|
| 1 | +--- |
| 2 | +title: Full-Text Search |
| 3 | +description: Implement full-text search in your website using Nuxt Content |
| 4 | +navigation: |
| 5 | + icon: i-heroicons-document-magnifying-glass |
| 6 | +--- |
| 7 | + |
| 8 | +Content module expose a handy utility [`queryCollectionSearchSections`{lang="ts-type"}][1] to break down content files into searchable sections. This is useful for implementing full-text search in your website. |
| 9 | +You can use the result of this utility in combination with [Nuxt UI Content Search][nuxt-ui-content-search] or other search libraries like [Fuse.js](https://fusejs.io/), [minisearch][mini-search], etc. |
| 10 | + |
| 11 | + |
| 12 | +## Nuxt UI Pro |
| 13 | + |
| 14 | +Nuxt UI Pro provide 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. |
| 15 | + |
| 16 | +Read more about [Nuxt UI Content Search][nuxt-ui-content-search]. |
| 17 | + |
| 18 | +::code-group |
| 19 | + |
| 20 | +```vue [UContentSearchExample.vue] |
| 21 | +<script setup lang="ts"> |
| 22 | +const { data: navigation } = await useAsyncData('navigation', () => queryCollectionNavigation('docs')) |
| 23 | +const { data: files } = await useAsyncData('search', () => queryCollectionSearchSections('docs')) |
| 24 | +
|
| 25 | +const searchTerm = ref('') |
| 26 | +</script> |
| 27 | +
|
| 28 | +<template> |
| 29 | + <UContentSearch |
| 30 | + v-model:search-term="searchTerm" |
| 31 | + :files="files" |
| 32 | + :navigation="navigation" |
| 33 | + :fuse="{ resultLimit: 42 }" |
| 34 | + /> |
| 35 | +</template> |
| 36 | +``` |
| 37 | + |
| 38 | +:::preview-card{label="Live Preview"} |
| 39 | + :ExampleFulltextContentSearch |
| 40 | +::: |
| 41 | + |
| 42 | +:: |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +## MiniSearch example |
| 47 | + |
| 48 | +Read more about [minisearch][mini-search]. |
| 49 | + |
| 50 | +::code-group |
| 51 | + |
| 52 | +```vue [MiniSearchExample.vue] |
| 53 | +<script setup lang="ts"> |
| 54 | +import MiniSearch from 'minisearch' |
| 55 | +
|
| 56 | +const query = ref('') |
| 57 | +const { data } = await useAsyncData('search', () => queryCollectionSearchSections('docs')) |
| 58 | +
|
| 59 | +const miniSearch = new MiniSearch({ |
| 60 | + fields: ['title', 'content'], |
| 61 | + storeFields: ['title', 'content'], |
| 62 | + searchOptions: { |
| 63 | + prefix: true, |
| 64 | + fuzzy: 0.2, |
| 65 | + }, |
| 66 | +}) |
| 67 | +
|
| 68 | +// Add data to the MiniSearch instance |
| 69 | +miniSearch.addAll(toValue(data.value)) |
| 70 | +const result = computed(() => miniSearch.search(toValue(query))) |
| 71 | +</script> |
| 72 | +
|
| 73 | +<template> |
| 74 | + <UContainer class="p-4"> |
| 75 | + <UCard> |
| 76 | + <UInput v-model="query" placeholder="Search..." /> |
| 77 | + <ul> |
| 78 | + <li v-for="link of result" :key="link.id" class="mt-2"> |
| 79 | + <NuxtLink :to="link.id">{{ link.title }}</NuxtLink> |
| 80 | + <p class="text-gray-500 text-xs">{{ link.content }}</p> |
| 81 | + </li> |
| 82 | + </ul> |
| 83 | + </UCard> |
| 84 | + </UContainer> |
| 85 | +</template> |
| 86 | +``` |
| 87 | + |
| 88 | +:::preview-card{label="Live Preview"} |
| 89 | + :example-fulltext-mini-search |
| 90 | +::: |
| 91 | + |
| 92 | +:: |
| 93 | + |
| 94 | + |
| 95 | +## Fuse.js example |
| 96 | + |
| 97 | +Read more about [Fuse.js][fusejs]. |
| 98 | + |
| 99 | +::code-group |
| 100 | + |
| 101 | +```vue [FusejsExample.vue] |
| 102 | +<script setup lang="ts"> |
| 103 | +import Fuse from 'fuse.js' |
| 104 | +
|
| 105 | +const query = ref('') |
| 106 | +const { data } = await useAsyncData('search-data', () => queryCollectionSearchSections('docs')) |
| 107 | +
|
| 108 | +const fuse = new Fuse(data.value, { |
| 109 | + keys: ['title', 'description'] |
| 110 | +}) |
| 111 | +
|
| 112 | +const result = computed(() => fuse.search(toValue(query)).slice(0, 10)) |
| 113 | +</script> |
| 114 | +
|
| 115 | +<template> |
| 116 | + <UContainer class="p-4"> |
| 117 | + <UCard> |
| 118 | + <UInput v-model="query" placeholder="Search..." class="w-full" /> |
| 119 | + <ul> |
| 120 | + <li v-for="link of result" :key="link.item.id" class="mt-2"> |
| 121 | + <UButton variant="ghost" class="w-full" :to="link.item.id"> |
| 122 | + {{ link.item.title }} |
| 123 | + <span class="text-gray-500 text-xs"> |
| 124 | + {{ link.item.content?.slice(0, 100) }}... |
| 125 | + </span> |
| 126 | + </UButton> |
| 127 | + </li> |
| 128 | + </ul> |
| 129 | + </UCard> |
| 130 | + </UContainer> |
| 131 | +</template> |
| 132 | +``` |
| 133 | + |
| 134 | +:::preview-card{label="Live Preview"} |
| 135 | + :example-fulltext-fusejs |
| 136 | +::: |
| 137 | + |
| 138 | +:: |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +[1]: /api/query-collection-search-sections |
| 144 | +[fusejs]: https://fusejs.io |
| 145 | +[mini-search]: https://lucaong.github.io/minisearch |
| 146 | +[nuxt-ui-content-search]: https://ui.nuxt.com/pro/components/content-search |
0 commit comments