koide 5438f2fdef
All checks were successful
Deploy Docusaurus Site / deploy (push) Successful in 25s
Add: SearXNGワンライナースクリプト
2026-02-22 23:56:52 +00:00

260 lines
5.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
sidebar_position: 10
title: SearXNGでローカル検索APIを構築する
description: プライバシー重視のメタ検索エンジンSearXNGをセルフホストして、無料で検索APIを手に入れる方法
hide_table_of_contents: false
displayed_sidebar: null
---
# SearXNGでローカル検索APIを構築する
ローカルでClaude CodeやLLMエージェントを動かすとき、Web検索機能が欲しくなることがある。Brave APIやGoogle Custom Search APIは有料だったり、レート制限があったりする。
**SearXNG**をセルフホストすれば、**完全無料**で検索APIが手に入る。
## SearXNGとは
SearXNGは**プライバシー重視のメタ検索エンジン**(オープンソース)。
```
ユーザー → SearXNG → Google/Bing/DuckDuckGo等
結果を集約してJSON返却
```
複数の検索エンジンに代理でクエリを投げて、結果をまとめて返す。
### 特徴
| ポイント | 説明 |
|----------|------|
| **APIキー不要** | スクレイピング方式なので無料 |
| **JSON API** | `format=json` で検索結果をJSON取得 |
| **複数エンジン集約** | Google, Bing, DuckDuckGoなどを同時検索 |
| **セルフホスト** | 自分のサーバーで完全コントロール |
## セットアップ
### 前提条件
- Docker環境LXC、VM、物理サーバー等
- ポート8080が空いている
### ワンライナーでセットアップ
```bash
curl -sL https://docs.techswan.online/scripts/searxng-setup.sh | sudo bash
```
これだけでDocker + SearXNGが起動する。
### 手動セットアップ
手動で構築したい場合は以下:
```bash
# 必要なパッケージ
sudo apt-get update
sudo apt-get install -y docker.io curl
# ディレクトリ作成
sudo mkdir -p /opt/searxng/searxng
cd /opt/searxng
# docker-compose.yml作成
sudo tee docker-compose.yml << 'EOF'
version: '3.8'
services:
searxng:
image: searxng/searxng:latest
container_name: searxng
restart: unless-stopped
ports:
- "8080:8080"
volumes:
- ./searxng:/etc/searxng:rw
environment:
- SEARXNG_BASE_URL=http://localhost:8080
EOF
# 設定ファイル作成
sudo tee searxng/settings.yml << 'EOF'
use_default_settings: true
general:
instance_name: "SearXNG Local"
search:
safe_search: 0
autocomplete: "duckduckgo"
default_lang: "ja-JP"
formats:
- html
- json
server:
limiter: false
image_proxy: true
ui:
default_theme: simple
default_locale: "ja"
EOF
# 起動
sudo docker compose up -d
```
## 使い方
### Web UI
ブラウザで `http://localhost:8080` にアクセス。
### JSON API
```bash
# 基本的な検索
curl "http://localhost:8080/search?q=検索ワード&format=json"
# 言語指定
curl "http://localhost:8080/search?q=test&format=json&language=ja-JP"
# カテゴリ指定
curl "http://localhost:8080/search?q=test&format=json&categories=general"
```
### レスポンス例
```json
{
"results": [
{
"title": "Example Title",
"url": "https://example.com",
"content": "Description...",
"engine": "google",
"engines": ["google", "bing"],
"score": 4.0
}
]
}
```
## Claude Code連携
ローカルのClaude CodeからSearXNGを検索ツールとして使う。
### Pythonで使う
```python
import requests
def search(query: str, num_results: int = 5) -> list:
"""SearXNGで検索"""
response = requests.get(
"http://localhost:8080/search",
params={"q": query, "format": json}
)
return response.json()["results"][:num_results]
# 使用例
results = search("Python async await")
for r in results:
print(f"- {r['title']}: {r['url']}")
```
### MCP Toolとして
```json
{
"tools": [
{
"name": "web_search",
"description": "Search the web using SearXNG",
"inputSchema": {
"type": "object",
"properties": {
"query": {"type": "string"}
},
"required": ["query"]
}
}
]
}
```
## 有効な検索エンジン
デフォルトで以下が有効:
- **Google** - Web検索
- **Bing** - Web検索
- **DuckDuckGo** - プライバシー重視
- **Brave** - 広告なし
- **Wikipedia** - 百科事典
- **GitHub** - コード検索
`settings.yml` でエンジンの有効/無効を切り替え可能。
## トラブルシューティング
### 検索結果が少ない
一部のエンジンがIPブロックされている可能性。`settings.yml` で別のエンジンを有効化。
### 起動しない
```bash
# ログ確認
docker logs searxng
# 再起動
cd /opt/searxng && docker compose restart
```
### アップデート
```bash
cd /opt/searxng
docker compose pull
docker compose up -d
```
## Brave API との比較
| | SearXNG | Brave API |
|---|---------|-----------|
| 料金 | 無料 | 有料 |
| セットアップ | 必要 | 不要 |
| レート制限 | なし | あり |
| 安定性 | 自己責任 | 高い |
| エンジン | 複数集約 | Braveのみ |
**おすすめ構成:**
- 通常 → Brave API安定
- フォールバック → SearXNG
- ローカル開発 → SearXNG一本
## まとめ
SearXNGをセルフホストすれば
- ✅ 完全無料で検索API
- ✅ レート制限なし
- ✅ 複数エンジンの結果を集約
- ✅ ローカルLLM/エージェントと連携
ローカルでClaude Codeを動かすなら、一つ立てておくと便利
## 参考リンク
- [SearXNG公式ドキュメント](https://docs.searxng.org/)
- [SearXNG GitHub](https://github.com/searxng/searxng)
- [Docker Hub - searxng/searxng](https://hub.docker.com/r/searxng/searxng)
---
*この記事は2026年2月時点の情報です。*