Elasticsearchは、Apache Luceneを基盤とする分散型全文検索エンジンで、高速なデータ探索・分析を可能にします。REST APIを通じて操作でき、JSON形式でのデータ管理が標準です。全文検索、構造化検索、リアルタイム分析など、多様な用途に対応しています。
代表的な利用シーン
- ECサイトの商品検索
- ユーザーレビューの収集・表示
- ログの集約・可視化
- 検索結果のハイライト表示
データ構造例(JSON)
{
"email": "sato@example.com",
"profile": {
"name": "佐藤太郎",
"age": 30,
"hobbies": ["読書", "登山"]
},
"signup_date": "2024-01-15"
}
インストール方法の比較
| 方式 | 利点 | 欠点 |
|---|---|---|
| Docker | 手軽に起動、環境分離 | 設定変更が面倒、永続化が必要 |
| RPM/DEB | 標準パス、起動スクリプト付き | アンインストールが不完全な場合あり |
| tar.gz | 柔軟な配置、侵襲性が低い | 手動でのサービス管理が必要 |
| Ansible | 大規模展開に最適、自動化可能 | 学習コスト高、設計が必要 |
RPMによるインストール手順
# Java 8のインストール
sudo yum install -y java-1.8.0-openjdk
# Elasticsearch RPMのダウンロード・インストール
mkdir -p /opt/es
cd /opt/es
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.rpm
sudo rpm -ivh elasticsearch-6.6.0.rpm
# サービス有効化・起動
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
主要な設定ファイル
/etc/elasticsearch/elasticsearch.yml— 基本設定/etc/elasticsearch/jvm.options— JVMメモリ設定/var/lib/elasticsearch— データ保存ディレクトリ/var/log/elasticsearch— ログ出力先
最小限のクラスタ設定例
cluster.name: my_cluster
node.name: server01
path.data: /data/es_data
path.logs: /var/log/elasticsearch
network.host: 192.168.1.10
http.port: 9200
discovery.zen.minimum_master_nodes: 1
bootstrap.memory_lock: true
メモリロックの設定
# /usr/lib/systemd/system/elasticsearch.service.d/override.conf
[Service]
LimitMEMLOCK=infinity
その後、systemctl daemon-reload && systemctl restart elasticsearch を実行。
重要な用語解説
- Cluster:複数ノードからなる論理グループ。名前で識別。
- Node:クラスタに参加する単一サーバー。データ保持・検索処理を担う。
- Index:類似ドキュメントの集合。RDBの「テーブル」に相当。
- Shard:インデックスを分割した物理単位。水平スケーリングの鍵。
- Replica:可用性・性能向上のためのシャードのコピー。
- Document:JSON形式で格納される1件のデータ。RDBの「レコード」。
- Mapping:フィールドの型や分析方法を定義。スキーマ定義に近い。
基本的なAPI操作
# インデックス作成
curl -X PUT "localhost:9200/userdb?pretty"
# ドキュメント登録
curl -X PUT "localhost:9200/userdb/_doc/1" \
-H 'Content-Type: application/json' \
-d '{ "name": "田中", "age": 28, "city": "東京" }'
# 検索クエリ
curl -X GET "localhost:9200/userdb/_search?q=city:東京&pretty"
# 条件付き検索(DSL)
curl -X POST "localhost:9200/userdb/_search" \
-H 'Content-Type: application/json' \
-d '{
"query": {
"bool": {
"must": { "match": { "city": "東京" } },
"filter": { "range": { "age": { "gte": 25 } } }
}
}
}'
クラスタ構築時の注意点
- 全ノードで
cluster.nameを統一 discovery.zen.ping.unicast.hostsに初期ノードを記載- マスター選出のため
minimum_master_nodes = (ノード数/2)+1を設定 - ノード障害時は、残存ノードの設定を見直して再起動
分片とレプリカの動的調整
# インデックス作成時に明示的に設定
curl -X PUT "localhost:9200/logs" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}'
# 後からレプリカ数のみ変更可能
curl -X PUT "localhost:9200/logs/_settings" -H 'Content-Type: application/json' -d'
{
"number_of_replicas": 2
}'
日本語形態素解析プラグイン(IK Analyzer)
# プラグインインストール
cd /usr/share/elasticsearch/bin
./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.2/elasticsearch-analysis-ik-6.4.2.zip
# 日本語対応マッピング定義
curl -X PUT "localhost:9200/jp_docs" -H 'Content-Type: application/json' -d'
{
"mappings": {
"properties": {
"body": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}'
監視機能(X-Pack)
バージョン6.3以降、基本的な監視機能は無料で利用可能。
# 監視を有効化
PUT _cluster/settings
{
"persistent": {
"xpack.monitoring.collection.enabled": true
}
}
# 設定ファイル末尾に追加
xpack.monitoring.exporters.my_local:
type: local
運用上の推奨設定
- JVMヒープサイズは物理メモリの50%以下(最大31GB)
- SWAPを無効化(
swapoff -a+/etc/fstab修正) vm.max_map_count = 262144を sysctl で設定- ファイルディスクリプタ上限を65536以上に引き上げ
- SSD推奨、回転ディスクは避ける