Algolia Implementation with Claude Code
Learn about algolia implementation using Claude Code. Practical tips and code examples included.
Algolia全文検索をClaude Codeで構築する
Algoliaはミリ秒単位の応答速度を誇る検索エンジンSaaSです。タイプミス許容、ファセット検索、パーソナライゼーションなど高度な検索体験を簡単に実装できます。Claude Codeを使えば、インデックス設計から検索UIの構築まで効率的に進められます。
インデックスの設計とデータ投入
> Algoliaのインデックスを設計して、商品データを投入する
> スクリプトを作って。日本語検索にも対応して。
// src/scripts/index-products.ts
import algoliasearch from 'algoliasearch';
const client = algoliasearch(
process.env.ALGOLIA_APP_ID!,
process.env.ALGOLIA_ADMIN_KEY!
);
const index = client.initIndex('products');
// インデックス設定
await index.setSettings({
searchableAttributes: [
'name',
'description',
'category',
'brand',
'tags',
],
attributesForFaceting: [
'searchable(category)',
'searchable(brand)',
'price_range',
'filterOnly(in_stock)',
],
customRanking: ['desc(popularity)', 'asc(price)'],
queryLanguages: ['ja', 'en'],
indexLanguages: ['ja', 'en'],
// 日本語トークナイゼーション
ignorePlurals: true,
removeStopWords: true,
});
// データ投入
interface Product {
objectID: string;
name: string;
description: string;
category: string;
brand: string;
price: number;
price_range: string;
in_stock: boolean;
tags: string[];
popularity: number;
image_url: string;
}
const products: Product[] = await fetchProductsFromDB();
// バッチでインデックス投入(1000件ずつ)
const batchSize = 1000;
for (let i = 0; i < products.length; i += batchSize) {
const batch = products.slice(i, i + batchSize);
await index.saveObjects(batch);
console.log(`Indexed ${i + batch.length} / ${products.length}`);
}
React InstantSearchで検索UI構築
> React InstantSearchで検索ページを作って。
> ファセット、ハイライト、ページネーション付きで。
// src/components/SearchPage.tsx
import {
InstantSearch,
SearchBox,
Hits,
RefinementList,
Pagination,
Highlight,
Stats,
Configure,
} from 'react-instantsearch';
import algoliasearch from 'algoliasearch/lite';
const searchClient = algoliasearch(
process.env.NEXT_PUBLIC_ALGOLIA_APP_ID!,
process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_KEY!
);
function Hit({ hit }: { hit: any }) {
return (
<article className="p-4 border rounded-lg">
<img
src={hit.image_url}
alt={hit.name}
className="w-full h-48 object-cover rounded"
/>
<h3 className="text-lg font-bold mt-2">
<Highlight attribute="name" hit={hit} />
</h3>
<p className="text-gray-600 text-sm mt-1">
<Highlight attribute="description" hit={hit} />
</p>
<div className="flex justify-between items-center mt-2">
<span className="text-indigo-600 font-bold">
¥{hit.price.toLocaleString()}
</span>
<span className="text-sm text-gray-500">{hit.brand}</span>
</div>
</article>
);
}
export default function SearchPage() {
return (
<InstantSearch searchClient={searchClient} indexName="products">
<Configure hitsPerPage={12} />
<div className="max-w-6xl mx-auto p-6">
<SearchBox
placeholder="商品を検索..."
className="mb-6"
/>
<Stats />
<div className="grid grid-cols-4 gap-6 mt-4">
<aside>
<h4 className="font-bold mb-2">カテゴリ</h4>
<RefinementList attribute="category" />
<h4 className="font-bold mt-4 mb-2">ブランド</h4>
<RefinementList attribute="brand" searchable />
<h4 className="font-bold mt-4 mb-2">価格帯</h4>
<RefinementList attribute="price_range" />
</aside>
<main className="col-span-3">
<Hits hitComponent={Hit} className="grid grid-cols-3 gap-4" />
<Pagination className="mt-6" />
</main>
</div>
</div>
</InstantSearch>
);
}
インデックスの同期
データベースの変更をAlgoliaに同期する仕組みが必要です。
// src/services/algolia-sync.ts
import algoliasearch from 'algoliasearch';
const client = algoliasearch(
process.env.ALGOLIA_APP_ID!,
process.env.ALGOLIA_ADMIN_KEY!
);
const index = client.initIndex('products');
export async function onProductCreated(product: Product) {
await index.saveObject({ ...product, objectID: product.id });
}
export async function onProductUpdated(product: Product) {
await index.partialUpdateObject({
objectID: product.id,
...product,
});
}
export async function onProductDeleted(productId: string) {
await index.deleteObject(productId);
}
Summary
Algoliaの高速検索とClaude Codeを組み合わせれば、プロダクションレベルの検索機能を短期間で構築できます。検索機能の実装ガイドやパフォーマンス最適化も参考にしてください。
Algoliaの詳細はAlgolia公式ドキュメントを参照してください。
#Claude Code
#Algolia
#全文検索
#検索UI
#SaaS連携
Related Posts
Use Cases
Use Cases
How to Supercharge Your Side Projects with Claude Code [With Examples]
How to Supercharge Your Side Projects with Claude Code [With Examples]. A practical guide with code examples.
Use Cases
Use Cases
How to Automate Refactoring with Claude Code
Learn how to automate refactoring using Claude Code. Includes practical code examples and step-by-step guidance.
Use Cases
Use Cases
Complete CORS Configuration Guide with Claude Code
Learn about complete cors configuration guide using Claude Code. Practical tips and code examples included.