elasticsearch-pyを試す

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)

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

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です