ES
它是一个 分布式的 全文搜索与数据分析引擎。
与关系型数据库对比:
一个ES集群可以包含多个索引(数据库),每个索引又包含了很多类型(表),类型中包含了很多文档(行),每个文档使用 JSON 格式存储数据,包含了很多字段(列)
基本操作:
连接:
安装
pip install elasticsearch
连接
from elasticsearch import Elasticsearch
# 实例化
es = Elasticsearch([{
"host": "ip", "port": 9200}])
插入:
创建数据库
es.indices.create(index="index_name", ignore=400)
创建数据库,并忽略400错误(数据库重复时,会返回400)
插入数据
body = {
"name": "李李小明",
"age": 7,
"sex": "male"
}
es.index(index="es_test", doc_type='_doc', body=body)
插入多条
doc = [
{
"index": {
'_index': "es_test", "_type": '_doc', '_id': 1}},
{
"name": "王王大志", "age": 8, "sex": "male"},
{
"index": {
'_index': "es_test", "_type": '_doc', '_id': 2}},
{
"name": "刘刘大彪", "age": 8, "sex": "male"},
{
"index": {
'_index': "es_test"