# Fumadocs (Framework Mode, React Router): Meilisearch
URL: /zh-TW/docs/react-router/search/meilisearch
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/search/meilisearch.zh-TW.mdx

搭配 Fumadocs UI 使用 Meilisearch。

## 設定 [#setup]

Fumadocs CLI 可以幫您完成設定，包括建立搜尋對話方塊、`static.json` 路由，以及在每次建置後同步搜尋索引的指令碼：

```package-install
npx @fumadocs/cli feature search --provider meilisearch
```

或手動設定：

1. 整合 [Meilisearch](/docs/headless/search/meilisearch)。

2. 建立搜尋對話方塊，並將 `host` 和 `apiKey` 替換為您的主機 URL 與搜尋 API 金鑰。

   ```tsx title="components/search.tsx"
   'use client';
   import { Meilisearch } from 'meilisearch';
   import {
     SearchDialog,
     SearchDialogClose,
     SearchDialogContent,
     SearchDialogFooter,
     SearchDialogHeader,
     SearchDialogIcon,
     SearchDialogInput,
     SearchDialogList,
     SearchDialogOverlay,
     type SharedProps,
   } from 'fumadocs-ui/components/dialog/search';
   import { useMeilisearch } from 'fumadocs-core/search/client';
   import { useI18n } from 'fumadocs-ui/contexts/i18n';

   const client = new Meilisearch({
     host: 'replace me',
     apiKey: 'replace me',
   });

   export default function CustomSearchDialog(props: SharedProps) {
     const { locale } = useI18n(); // (optional) for i18n
     const search = useMeilisearch({
       client,
       indexName: 'docs',
       locale,
     });

     return (
       <SearchDialog {...search} {...props}>
         <SearchDialogOverlay />
         <SearchDialogContent>
           <SearchDialogHeader>
             <SearchDialogIcon />
             <SearchDialogInput />
             <SearchDialogClose />
           </SearchDialogHeader>
           <SearchDialogList />
           <SearchDialogFooter>
             <a
               href="https://www.meilisearch.com"
               rel="noreferrer noopener"
               className="ms-auto text-xs text-fd-muted-foreground"
             >
               Search powered by Meilisearch
             </a>
           </SearchDialogFooter>
         </SearchDialogContent>
       </SearchDialog>
     );
   }
   ```

### 替換搜尋對話方塊 [#replace-search-dialog]

請在根版面配置中，將您的搜尋對話方塊傳入 [`<RootProvider />`](/docs/ui/layouts/root-provider)：

```tsx title="app/root.tsx"
import { Links, Meta, Scripts, ScrollRestoration } from 'react-router';
import { RootProvider } from 'fumadocs-ui/provider/react-router';
// [!code ++]
import SearchDialog from '@/components/search';

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
      </head>
      <body className="flex flex-col min-h-screen">
        {/* [!code highlight] */}
        <RootProvider search={{ SearchDialog }}>{children}</RootProvider>
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}
```

### 標籤篩選 [#tag-filter]

您也可以視需要加入依標籤篩選結果的 UI。請先在文件上設定[標籤篩選](/docs/headless/search/meilisearch#tag-filter)，再加入以下內容：

```tsx
'use client';

import {
  SearchDialog,
  SearchDialogContent,
  SearchDialogFooter,
  SearchDialogOverlay,
  type SharedProps,
  TagsList,
  TagsListItem,
} from 'fumadocs-ui/components/dialog/search';
import { useState } from 'react';
import { useMeilisearch } from 'fumadocs-core/search/client';

export default function CustomSearchDialog(props: SharedProps) {
  // [!code ++]
  const [tag, setTag] = useState<string | undefined>();
  const search = useMeilisearch({
    tag, // [!code ++]
    // 其他選項……
  });

  return (
    <SearchDialog {...search} {...props}>
      <SearchDialogOverlay />
      <SearchDialogContent>
        ...
        <SearchDialogFooter className="flex flex-row">
          {/* [!code ++:3] */}
          <TagsList tag={tag} onTagChange={setTag}>
            <TagsListItem value="my-value">My Value</TagsListItem>
          </TagsList>
        </SearchDialogFooter>
      </SearchDialogContent>
    </SearchDialog>
  );
}
```