Search
Built-in full-text search.
The recommended preset includes full-text search, with no setup. Readers open it with ⌘ K, Ctrl K or the search button.
Built-in Search
The build indexes each page's title, description, headings and text, in one index per language. The browser searches the index of the page's language, so search works on a static host.
Pass search options to the preset's search:
import { createSite, recommendedPreset } from "vuesho/config";
import { defineDocs } from "vuesho/content";
const docs = defineDocs();
export default createSite(docs.toSource()).plugins(
recommendedPreset({
search: { filter: ({ page }) => !page.url.startsWith("/changelog") },
}),
);Prop
Type
Leaving Pages Out
filter keeps a page in the index when it returns true. page.data has your frontmatter types, so a field from the frontmatter schema can leave pages out:
import { pageSchema } from "vuesho/schema";
import { z } from "zod";
const docs = defineDocs({
docs: { schema: pageSchema.extend({ unlisted: z.boolean().default(false) }) },
});
export default createSite(docs.toSource()).plugins(
recommendedPreset({ search: { filter: ({ page }) => !page.data.unlisted } }),
);The sitemap, the RSS feed and llms.txt each take their own filter, as SEO shows.
Customizing the Index
buildIndex returns a page's index. Start from the default index, which buildIndexDefault() returns:
import { buildIndexDefault } from "vuesho/search";
createSite(docs.toSource()).plugins(
recommendedPreset({
search: {
async buildIndex(page) {
const index = await buildIndexDefault.call(this, page);
// Shows "guide" and "cli" above `guide/cli/build.md`'s results.
return { ...index, breadcrumbs: page.path.split("/").slice(0, -1) };
},
},
}),
);Prop
Type
Some search services store the extra fields that buildIndex returns.
additionalIndexes adds indexes that aren't pages of a source, like a custom page or another site's page. It takes one list for all languages, or a function that receives { lang } and returns that language's list:
recommendedPreset({
search: {
additionalIndexes: [
{
id: "/showcase",
url: "/showcase",
title: "Showcase",
structuredData: { headings: [], contents: [{ content: "Sites built with Acme." }] },
},
],
},
});Tags
Tags filter the results, with a tab per tag in the dialog's footer. Return each page's tag from buildIndex, and list the tabs in tags:
import { buildIndexDefault } from "vuesho/search";
createSite(docs.toSource()).plugins(
recommendedPreset({
search: {
async buildIndex(page) {
const index = await buildIndexDefault.call(this, page);
return { ...index, tag: page.path.startsWith("api/") ? "api" : "guide" };
},
tags: [
{ name: "Guide", value: "guide" },
{ name: "API Reference", value: "api" },
],
// The tab selected when the dialog opens.
defaultTag: "guide",
// Lets the reader clear the tab, to search every page.
allowClear: true,
},
}),
);Searching on the Server
With render: "dynamic", the server runs each query, so the browser downloads no index. The site then needs a server:
createSite(docs.toSource()).plugins(
recommendedPreset({ search: { render: "dynamic" } }),
);render is "dynamic" by default when content renders on request, like a dynamic source. With render: "static" on such a site, the index holds only the pages known when the site builds.
Each language's search API is at /_vuesho/search/<lang>, under the base path, and returns the results as JSON:
# `query` is required. `tag` takes tags separated by commas, and `limit` caps the results.
curl "https://docs.acme.dev/_vuesho/search/en?query=deploy&tag=guide&limit=10"Search Engines
The built-in search runs on MiniSearch, and ZBSearch or FlexSearch can replace it:
| Engine | Matches | Index size |
|---|---|---|
| MiniSearch (default) | Prefixes, and words with a typo | The smallest |
| ZBSearch | Whole words and prefixes | About 1.2 times as large |
| FlexSearch | Any part of a word | About 3 times as large |
MiniSearch
MiniSearch splits text into words in every language, CJK included, and splits a name at its dots, so useFakeTimers finds vi.useFakeTimers().
A stemmer turns each word into its stem, so "deploying" matches "deploy". stemmer takes a module whose default export is the stemmer, by a path from the project root or a package export. @zbsearch/stemmers has stemmers for 32 languages:
npm i @zbsearch/stemmersexport { stemmer as default } from "@zbsearch/stemmers/english";createSite(docs.toSource()).plugins(
recommendedPreset({ search: { stemmer: "/src/search-stemmer.ts" } }),
);The stemmer receives each word in lowercase, and the language tag of its page's index. A site in several languages stems each language with its own stemmer:
import { stemmer as english } from "@zbsearch/stemmers/english";
import { stemmer as german } from "@zbsearch/stemmers/german";
export default (word: string, lang: string) =>
lang.startsWith("de") ? german(word) : english(word);Prop
Type
ZBSearch
ZBSearch splits text into words in every language, and takes its own tokenizers, components and plugins. Install it, turn off the preset's search, and add its plugin:
npm i zbsearchimport { zbsearchPlugin } from "vuesho/search/zbsearch";
createSite(docs.toSource()).plugins(
recommendedPreset(),
recommendedPreset({ search: false }),
zbsearchPlugin(),
);Stemming and typo tolerance are off by default. Turn them on with tokenizer and search:
export default { language: "english", stemming: true };zbsearchPlugin({
// A word of the query may differ from the text by one letter.
search: { tolerance: 1 },
// "deploying" matches "deploy".
tokenizer: "/src/search-tokenizer.ts",
});tokenizer, components and plugins take a module, by a path from the project root or a package export, whose default export is the value. The tokenizer applies to every language's index.
zbsearchPlugin() takes the built-in search's options, the dialog options, and these:
Prop
Type
FlexSearch
FlexSearch matches any part of a word. Install it, turn off the preset's search, and add its plugin:
npm i flexsearchimport { flexsearchPlugin } from "vuesho/search/flexsearch";
createSite(docs.toSource()).plugins(
recommendedPreset(),
recommendedPreset({ search: false }),
flexsearchPlugin({ languages: { ja: { encoder: "CJK" } } }),
);flexsearchPlugin() takes the built-in search's options, the dialog options, and these:
Prop
Type
A static index takes only tokenize, resolution and an encoder name, like "CJK". With render: "dynamic", document takes any FlexSearch index option, like a custom Encoder:
import { Charset, Encoder } from "flexsearch";
flexsearchPlugin({
render: "dynamic",
document: { encoder: new Encoder({ ...Charset.LatinBalance, filter: new Set(["the", "a"]) }) },
});Semantic Search
The built-in engines match words. To find pages by meaning, like matching "make it faster" to a page on caching, use Mixedbread, which searches on the server.
Search Dialog
Each search plugin fills the search-dialog slot with a dialog, configured with the plugin's dialog options.
Dialog Options
createSite(docs.toSource()).plugins(
recommendedPreset({
search: {
// The links the dialog shows before a query.
links: [["Getting Started", "/guide"]],
// Opens the dialog with ⌘ / or Ctrl /.
hotKey: [
{ key: ["Meta", "Control"], display: { mac: "⌘", other: "Ctrl" } },
{ key: "/", display: "/" },
],
},
}),
);Prop
Type
Adding a Footer
The search-footer theme slot renders in the footer of every built-in search dialog, after its tags. Fill it in your theme:
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import SearchFooter from "./SearchFooter.vue";
export default defineTheme({
extends: defaultTheme,
slots: { "search-footer": SearchFooter },
});Styling Highlights
The dialog highlights the query's words in the results with the CSS Custom Highlight API. Style them with ::highlight(fd-search):
::highlight(fd-search) {
color: var(--color-fd-primary-foreground);
background-color: var(--color-fd-primary);
}Custom Dialog
vuesho/ui/components/dialog/search exports the dialog's parts, and useIndexSearch() returns the results. This dialog searches only the pages tagged guide:
<script setup lang="ts">
import {
SearchDialog,
SearchDialogContent,
SearchDialogFooter,
SearchDialogHeader,
SearchDialogInput,
SearchDialogList,
SearchDialogOverlay,
} from "vuesho/ui/components/dialog/search";
import { useAddingSlot } from "vuesho/client";
import type { IndexValues } from "vuesho/search";
import { useIndexSearch, type SearchPluginDialogProps } from "vuesho/theme";
// The dialog options, and how the index is searched.
const props = defineProps<SearchPluginDialogProps & IndexValues>();
const emit = defineEmits<{ "update:open": [open: boolean] }>();
const { search, onSearchChange, isLoading, data, error } = useIndexSearch(
props,
"guide",
{ client: () => import("vuesho/search/minisearch/client") },
);
const Footer = useAddingSlot("search-footer");
</script>
<template>
<SearchDialog
:search="search"
:is-loading="isLoading"
:data="data"
:error="error"
:open="open"
@update:search="onSearchChange"
@update:open="emit('update:open', $event)"
>
<SearchDialogOverlay />
<SearchDialogContent>
<SearchDialogHeader>
<SearchDialogInput />
</SearchDialogHeader>
<SearchDialogList />
<SearchDialogFooter>
<Footer v-if="Footer" />
</SearchDialogFooter>
</SearchDialogContent>
</SearchDialog>
</template>Fill the search-dialog slot with it, loaded with defineAsyncComponent():
import { defineAsyncComponent } from "vue";
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
export default defineTheme({
extends: defaultTheme,
slots: { "search-dialog": defineAsyncComponent(() => import("./SearchDialog.vue")) },
});useIndexSearch() searches a static index or the search API, whichever the plugin serves. For ZBSearch or FlexSearch, pass { client: () => import("vuesho/search/zbsearch/client") } or vuesho/search/flexsearch/client. Each search service has its own composable:
// Algolia, with a client of `algoliasearch/lite`.
import { useAlgoliaSearch } from "@vuesho/algolia/ui";
// Orama Cloud, with a client of `@orama/core`.
import { useOramaCloudSearch } from "@vuesho/orama-cloud/ui";
// Meilisearch: useMeilisearch(props, tag).
import { useMeilisearch } from "@vuesho/meilisearch/theme";
// Typesense: useTypesense(props, tag).
import { useTypesense } from "@vuesho/typesense/theme";
// Mixedbread, which searches on the server: useIndexSearch(props, tag), without a client.
import { useIndexSearch } from "vuesho/theme";SearchDialogList has a slot for each kind of row:
item: each resulttable: each table of resultsempty: the text shown when there are no results
A custom component highlights the query with useHighlightQuery(), which skips the elements with data-highlight-ignore:
<script setup lang="ts">
import { useSearch } from "vuesho/ui/components/dialog/search";
import { useHighlightQuery } from "vuesho/ui/search";
defineProps<{ content: string }>();
const { query } = useSearch();
const highlight = useHighlightQuery(query);
</script>
<template>
<p :ref="highlight">{{ content }}</p>
</template>Searching an External Index
A dialog can search an index that the site doesn't build, like one that the Orama Cloud crawler fills:
- Install
@vuesho/orama-cloudfor its dialog. - Turn off the preset's search, and add the dialog's styles.
- Fill
search-dialogwith your dialog, as in Custom Dialog.
createSite(docs.toSource()).plugins(
recommendedPreset({ search: false }),
{ name: "orama-dialog", stylesheet: "@vuesho/orama-cloud/css/orama-cloud.css" },
);Disabling Search
search: false removes the index, the dialog and the search button:
createSite(docs.toSource()).plugins(
recommendedPreset({ search: false }),
);To write a search engine or connect another service, see Custom Search.