今日のひとこと Challange IT For Future

elasticsearch-pyを試す

elasticsearchPython 読了 2分

この記事は最終更新から 11 年以上たっています。内容が現在の状況と異なる可能性があります。現在の情報は最新の記事、Fess の記事をご覧ください。

ElasticsearchのPython用クライアントであるelasticsearch-pyを試してみる。
pipが利用できる環境であれば、

$ sudo pip install elasticsearch

とすれば利用できるようになる。
あとは、以下のような感じでes.pyとか適当に作って、

# -*- coding: utf-8 -*-
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch("localhost:9200")
# データの登録
res1 = es.index(index="sample", doc_type="data",
                  body={"msg":"Hello", "timestamp": datetime.now()}, id="1")
print(res1)
# 検索
res2 = es.search(index="sample", doc_type="data",
                 body={"query":{"match_all":{}}})
print(res2)
# インデックスの削除
res3 = es.indices.delete(index="sample")
print(res3)

これを実行すれば、アクセスすることができます。
結構、簡単に使えますね。