# Fumadocs (Framework Mode, Next.js): Algolia
URL: /zh-TW/docs/next/search/algolia
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/search/algolia.zh-TW.mdx

在 Fumadocs UI 中使用 Algolia。

## 概覽 [#overview]

設定指南請參閱[整合 Algolia 搜尋](/docs/headless/search/algolia)。

雖然我們一般建議您使用 Algolia 的用戶端 SDK 自行建構搜尋功能，
但您也可以直接套用內建的搜尋對話方塊介面。

## 設定 [#setup]

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

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

或手動設定：

建立搜尋對話方塊，並將 `appId`、`apiKey` 和 `indexName` 替換為您需要的值。

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

const appId = 'replace me';
const apiKey = 'replace me';
const algolia = liteClient(appId, apiKey);

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

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

<Callout title="注意">
`useAlgoliaSearch()` 並未使用 Algolia 官方的
JavaScript 用戶端 instant search。
</Callout>

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

請將您的搜尋對話方塊傳入 [`<RootProvider />`](/docs/ui/layouts/root-provider)。由於根版面配置是伺服器元件，請先用用戶端元件包裝 provider：

```tsx tab="components/provider.tsx"
'use client';
import { RootProvider } from 'fumadocs-ui/provider/next';
import SearchDialog from '@/components/search';
import type { ReactNode } from 'react';

export function Provider({ children }: { children: ReactNode }) {
  return <RootProvider search={{ SearchDialog }}>{children}</RootProvider>;
}
```

```tsx tab="app/layout.tsx"
// [!code ++]
import { Provider } from '@/components/provider';
import type { ReactNode } from 'react';

export default function Layout({ children }: { children: ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body className="flex flex-col min-h-screen">
        {/* [!code --] */}
        <RootProvider>{children}</RootProvider>
        {/* [!code ++] */}
        <Provider>{children}</Provider>
      </body>
    </html>
  );
}
```

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

您也可以視需要加入依標籤篩選結果的 UI。請在搜尋伺服器上設定[標籤篩選](/docs/headless/search/algolia#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 { useFetchSearch } from 'fumadocs-core/search/client';

export default function CustomSearchDialog(props: SharedProps) {
  // [!code ++]
  const [tag, setTag] = useState<string | undefined>();
  const search = useFetchSearch({
    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>
  );
}
```