Python够直接,从它开始是个不错的选择。
Elasticsearch客户端列表:https://www.elastic.co/guide/en/elasticsearch/client/index.html
Python API:https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/index.html
参考文档:http://elasticsearch-py.readthedocs.io/en/master/index.html
安装
我在CentOS 7上安装了Python3.6,安装时使用下面的命令:
pip3 install elasticsearch
安装时需要root权限
牛刀小试
由于Elasticsearch索引的文档是JSON形式,而MongoDB存储也是以JSON形式,因此这里选择通过MongoDB导出数据添加到Elasticsearch中。
使用MongoDB的Python API时,需要先安装pymongo,命令:pip3 install pymongo
import traceback
from pymongo import MongoClient
from elasticsearch import Elasticsearch
# 建立到MongoDB的连接
_db = MongoClient('mongodb://127.0.0.1:27017')['blog']
# 建立到Elasticsearch的连接
_es = Elasticsearch()
# 初始化索引的Mappings设置
_index_mappings = {
"mappings": {
"user": {
"properties": {
"title": { "type": "text" },
"name": { "type": "text" },
"age": { "type": "integer" }
}
},
"blogpost": {
"prop