本記事では、全文検索エンジン「Elasticsearch(以下 ES)」を Python から使い始めるための基礎を、初心者向けにやさしく解説します。
- 対象読者:Python に慣れ始めた初心者
- 前提知識:Python の基本的な文法、pip によるパッケージインストール
- 到達目標:
- Elasticsearch 環境の構築
- Python クライアント elasticsearch-py の利用
- データの登録(インデックス)、検索、更新、削除
- 実践的な演習問題で学んだ知識を定着
Elasticsearch とは?
Elasticsearch は、分散型の RESTful な全文検索エンジンです。
- Lucene をベースに開発
- ドキュメント指向で JSON を扱う
- 水平スケーリングや高可用性が特徴
主な用途
- Web サイトの検索機能
- ログデータの可視化・分析
- ビッグデータのリアルタイム処理
環境構築
Elasticsearch のインストール
はじめに、公式サイトからダウンロードします。
解凍後、bin/elasticsearch を実行します。
ブラウザで http://localhost:9200/ にアクセスし、以下が返れば正常起動です。
{
"name" : "node-1",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "...",
"version" : {
"number" : "8.x.x",
...
},
"tagline" : "You Know, for Search"
}
Python クライアントのインストール
pip install elasticsearch
Python からの基本操作
以下、Python スクリプト例を交えながら説明します。
from elasticsearch import Elasticsearch
# クライアントを生成
es = Elasticsearch("http://localhost:9200")
インデックスの作成
index_name = "books"
mapping = {
"mappings": {
"properties": {
"title": {"type": "text"},
"author": {"type": "keyword"},
"content": {"type": "text"},
"year": {"type": "integer"}
}
}
}
es.indices.create(index=index_name, body=mapping)
ドキュメントの登録(Index API)
doc = {
"title": "Python入門",
"author": "山田太郎",
"content": "Pythonの基本文法を学ぶ...",
"year": 2025
}
res = es.index(index="books", id=1, body=doc)
print(res["result"]) # created
ドキュメントの取得(Get API)
res = es.get(index="books", id=1)
print(res["_source"])
検索(Search API)
query = {
"query": {
"match": {"content": "基本文法"}
}
}
res = es.search(index="books", body=query)
for hit in res["hits"]["hits"]:
print(f'ID={hit["_id"]}, score={hit["_score"]}')
更新(Update API)
update_body = {
"doc": {"year": 2026}
}
es.update(index="books", id=1, body=update_body)
削除(Delete API & Index 削除)
es.delete(index="books", id=1)
es.indices.delete(index="books")
応用テクニック
- バルク登録(Bulk API)
大量データを一度に登録して高速化 - アナライザー設定
日本語形態素解析プラグイン(kuromoji)導入で日本語検索を強化 - フェイルオーバー
複数ノードをクラスタ構成にして高可用性を実現
演習問題
学んだ内容を確認するため、以下の演習に取り組んでみましょう。
問題1:インデックス作成
「movies」という名前で、以下プロパティを持つインデックスを作成してください。
- title:text
- director:keyword
- year:integer
- summary:text
問題2:複数ドキュメントの登録
問題1で作成した「movies」インデックスに、次の映画データをバルク登録してください。
- タイトル: 君の名は。, 監督: 新海誠, 年: 2016
- タイトル: 千と千尋の神隠し, 監督: 宮崎駿, 年: 2001
問題3:検索クエリ
「summary」フィールド内に「異世界」というワードを含むドキュメントを検索するクエリを作成し、実行結果を表示してください。
問題4:ドキュメント更新
IDが2のドキュメントの year を 2002 に更新するコードを書いてください。
問題5:インデックス削除
「movies」インデックスを削除するコードを書いてください。
解答例
解答1:インデックス作成
mapping = {
"mappings": {
"properties": {
"title": {"type": "text"},
"director":{"type": "keyword"},
"year": {"type": "integer"},
"summary": {"type": "text"}
}
}
}
es.indices.create(index="movies", body=mapping)
解答2:複数ドキュメントのバルク登録
from elasticsearch import helpers
docs = [
{"_index": "movies", "_id": 1, "_source": {"title": "君の名は。", "director": "新海誠", "year": 2016}},
{"_index": "movies", "_id": 2, "_source": {"title": "千と千尋の神隠し", "director": "宮崎駿", "year": 2001}}
]
helpers.bulk(es, docs)
解答3:検索クエリ
query = {
"query": {
"match": {"summary": "異世界"}
}
}
res = es.search(index="movies", body=query)
for hit in res["hits"]["hits"]:
print(hit["_source"])
解答4:ドキュメント更新
update_body = {"doc": {"year": 2002}}
es.update(index="movies", id=2, body=update_body)
解答5:インデックス削除
es.indices.delete(index="movies")
おわりに
本記事では、Python から Elasticsearch を使うための基礎を一通り解説しました。
- 環境構築から CRUD 操作
- バルク登録や応用プラグインの紹介
- 演習問題で理解を深める
このチュートリアルをもとに、実際のアプリケーションやログ分析、検索機能の実装へと応用してみてください。Elasticsearch の奥深い機能に触れ、より高度な設定やチューニングにもぜひ挑戦してみましょう!