示例代码环境 python:3.8  es:7.8.0
环境安装
pip install elasticsearch==7.8.0
from elasticsearch import Elasticsearch



#1、创建ES对象,创建连接
es = Elasticsearch(['127.0.0.1:9200'],ignore=[400, 405, 502])
print('---------------1--------------------------')

# 2、创建索引index:索引的名字,ignore:状态码
result=es.indices.create(index="user",ignore=400)
print(result)
print('---------------2--------------------------')

# 3、删除索引
result = es.indices.delete(index='user', ignore=[400, 404])
print(result)
print('-----------------3------------------------')


#4、新增
#es.index,向指定索引添加或更新文档,如果索引不存在,首先会创建该索引,然后再执行添加或者更新操作。
print(es.index(index='w2', doc_type='_doc', id='4', body={"name":"可可", "age": 18}))    # 正常
print(es.index(index='w2', doc_type='_doc', id=5, body={"name":"卡卡西", "age":22}))     # 正常
print(es.index(index='w2', doc_type='_doc', body={"name": "鸣人", "age": 22}))  # 可以不指定id,默认生成一个id
print('-----------------4------------------------')

#5、查询
#5.1  es.get,查询索引中指定文档 主键查询
print(es.get(index='w2', doc_type='_doc', id=5))
#print(es.get(index='w2', doc_type='doc'))  # TypeError: get() missing 1 required positional argument: 'id'
#print(es.get(index='w2',  id=5))  # TypeError: get() missing 1 required positional argument: 'doc_type'
print('-----------------5------------------------')

#5.2 es.search,执行搜索查询并获取与查询匹配的搜索匹配。这个用的最多,可以跟复杂的查询条件。
'''
    index要搜索的以逗号分隔的索引名称列表; 使用_all 或空字符串对所有索引执行操作。
    body 使用Query DSL(QueryDomain Specific Language查询表达式)的搜索定义。
    _source  返回指定字段。
    excludes  返回的所有字段中,排除哪些字段。
    includes从_source字段中提取和返回的字段列表,跟_source差不多
'''

#一般查询
body = {
  "query": {
    "match": {
       "age": 22
    }
  }
}
print(es.search(index='w2',   body=body))
#查询所有
body = {
  "query": {
    "match_all": {}
  }
}
print(es.search(index='w2',   body=body))
# #与上一条等价
print(es.search(index='w2',   body={"query": {"match":{"age": 18}}} ) )
# # 结果字段过滤
print(es.search(index='w2',   body={"query": {"match":{"age": 18}}},_source=['name', 'age']))
# # 结果字段过滤
print(es.search(index='w2',   body={"query": {"match":{"age": 18}}},_source=['name']))

#使用includes 指定返回字段
body1 ={
  "_source": {
    "includes": ["age"]
     },
  "query": {
      "match": {
          "age": 18
      }
  }
}
print(es.search(index='w2',  body=body1))

#excludes 返回的所有字段中,排除哪些字段
body2 ={
  "_source": {
    "excludes": ["age"]
     },
  "query": {
      "match": {
          "age": 18
      }
  }
}
print(es.search(index='w2',  body=body2))


#测试 模糊查询 name只输入“卡” 可以查出name=“卡卡西”的数据
body3 ={
  "query": {
      "match": {
          "name": "卡"
      }
  }
}
print(es.search(index='w2',  body=body3))

#5.3 get_source,通过索引、类型和ID获取文档的来源,其实,直接返回想要的字典。 对比如下结果
print(es.get(index='w2', doc_type='_doc', id=5))
print(es.get_source(index='w2',  id=5))


#5.4 es.count,执行查询并获取该查询的匹配数。比如查询年龄是18的文档。
body4 = {
    "query": {
        "match": {
            "age": 18
        }
    }
}
print(es.count(index='w2',  body=body4))  # {'count': 1, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
print(es.count(index='w2',  body=body4)['count'])  # 1

# 6 es.delete,删除指定的文档。比如删除文章id为4的文档,但不能删除索引,如果想要删除索引,还需要es.indices.delete来处理
print(es.index(index='w2', doc_type='_doc', id=6, body={"name":"yc", "age": 18}))
print(es.get(index='w2', doc_type='_doc', id=6))
print(es.delete(index='w2',  id=6))
print(es.get(index='w2', doc_type='_doc', id=6)) #查询不存在的会报错

# 7 es.delete_by_query,删除与查询匹配的所有文档。
'''
    index 要搜索的以逗号分隔的索引名称列表; 使用_all 或空字符串对所有索引执行操作。
    body 使用Query DSL的搜索定义。
'''
#print(es.search(index='w2'))
print(es.delete_by_query(index='w2',   body={"query": {"match":{"age": 22}}}))
print(es.search(index='w2'))

# 8 es.exists,查询elasticsearch中是否存在指定的文档,返回一个布尔值。
print(es.exists(index='w2',   id='4'))

# 9 es.info,获取当前集群的基本信息。
print(es.info())

# 10 es.ping,如果群集已启动,则返回True,否则返回False。
print(es.ping())