网站首页 > 资源文章 正文
1.Python模块的安装
pip install elasticsearch
2.Python 连接ElasticSearch
from elasticsearch import Elasticsearch
# es = Elasticsearch() # 默认连接本地elasticsearch
# es = Elasticsearch(['127.0.0.1:9200']) # 连接本地9200端口
es = Elasticsearch(
["192.168.1.10", "192.168.1.11", "192.168.1.12"], # 连接集群,以列表的形式存放各节点的IP地址
sniff_on_start=True, # 连接前测试
sniff_on_connection_fail=True, # 节点无响应时刷新节点
sniff_timeout=60 # 设置超时时间
)
3.配置忽略响应的状态码
es = Elasticsearch(['127.0.0.1:9200'],ignore=400) # 忽略返回的400状态码
es = Elasticsearch(['127.0.0.1:9200'],ignore=[400, 405, 502]) # 以列表的形式忽略多个状态码
4.es连接对象的操作
- 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
- es.get,查询索引中指定文档
print(es.get(index='w2', doc_type='doc', id=5))
- es.get_source,通过索引类型和ID获取文档的来源,直接返回需要的字典
print(es.get_source(index='py3', doc_type='doc', id='1')) # {'name': '王五', 'age': 19}
es.search,执行搜索查询并获取与查询匹配的搜索匹配。这个用的最多,可以跟复杂的查询条件。
- index: 要搜索的以逗号分隔的索引名称列表; 使用_all 或空字符串对所有索引执行操作。
- doc_type: 要搜索的以逗号分隔的文档类型列表; 留空以对所有类型执行操作。
- body: 使用Query DSL(QueryDomain Specific Language查询表达式)的搜索定义。
- _source: 返回_source字段的true或false,或返回的字段列表,返回指定字段。
- _source_exclude:要从返回的_source字段中排除的字段列表,返回的所有字段中,排除哪些字段。
- _source_include:从_source字段中提取和返回的字段列表,跟_source差不多。
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 20}}})) # 一般查询
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source=['name', 'age'])) # 结果字段过滤
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source_exclude =[ 'age']))
print(es.search(index='py3', doc_type='doc', body={"query": {"match":{"age": 19}}},_source_include =[ 'age']))
- es.count ,执行查询获取查询到的数量
body = {
"query": {
"match": {
"age": 18
}
}
}
print(es.count(index='py2', doc_type='doc', body=body)) # {'count': 1, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
print(es.count(index='py2', doc_type='doc', body=body)['count']) # 1
print(es.count(index='w2')) # {'count': 6, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
print(es.count(index='w2', doc_type='doc')) # {'count': 6, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
- es.create,添加一条数据,索引不存在的话创建索引添加数据
print(es.create(index='py3', doc_type='doc', id='1', body={"name": '王五', "age": 20}))
print(es.get(index='py3', doc_type='doc', id='3'))
- es.delete,删除指定的文档
print(es.delete(index='py3', doc_type='doc', id='4'))
- es.delete_by_query,删除与查询匹配的所有文档。
print(es.delete_by_query(index='py3', doc_type='doc', body={"query": {"match":{"age": 20}}}))
- es.exists,查询elasticsearch中是否存在指定的文档,返回一个布尔值。
print(es.exists(index='py3', doc_type='doc', id='1'))
- es.info,获取当前集群的基本信息。
- es.ping,如果群集已启动,则返回True,否则返回False。
5. es对象的Indices 创建索引,定义mapping
- es.indices.create,在Elasticsearch中创建索引,用的最多。
body = {
"mappings": {
"doc": {
"dynamic": "strict", # 严格的格式
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"url": {
"type": "text"
},
"action_type": {
"type": "text"
},
"content": {
"type": "text"
}
}
}
}
}
es.indices.create('py4', body=body) # 给索引py4添加索引的映射信息
- es.indices.analyze,返回分词结果。
es.indices.analyze(body={'analyzer': "ik_max_word", "text": "皮特和茱丽当选“年度模范情侣”Brad Pitt and Angelina Jolie"})
- es.indices.delete,在Elasticsearch中删除索引。
print(es.indices.delete(index='py4'))
print(es.indices.delete(index='w3')) # {'acknowledged': True}
- es.indices.put_alias,为一个或多个索引创建别名,查询多个索引的时候,可以使用这个别名。
index 别名应指向的逗号分隔的索引名称列表(支持通配符),使用_all对所有索引执行操作。
name要创建或更新的别名的名称。
body别名的设置,例如路由或过滤器。
print(es.indices.put_alias(index='py4', name='py4_alias')) # 为单个索引创建别名
print(es.indices.put_alias(index=['py3', 'py2'], name='py23_alias')) # 为多个索引创建同一个别名,联查用
- es.indices.delete_alias,删除一个或多个别名。
print(es.indices.delete_alias(index='alias1'))
print(es.indices.delete_alias(index=['alias1, alias2']))
- es.indices.get_mapping,检索索引或索引/类型的映射定义。
print(es.indices.get_mapping(index='py4'))
- es.indices.get_settings,检索一个或多个(或所有)索引的设置。
print(es.indices.get_settings(index='py4'))
- es.indices.get,允许检索有关一个或多个索引的信息。
print(es.indices.get(index='py2')) # 查询指定索引是否存在
print(es.indices.get(index=['py2', 'py3']))
- es.indices.get_alias,检索一个或多个别名。
print(es.indices.get_alias(index='py2'))
print(es.indices.get_alias(index=['py2', 'py3']))
- es.indices.get_field_mapping,检索特定字段的映射信息。
print(es.indices.get_field_mapping(fields='url', index='py4', doc_type='doc'))
print(es.indices.get_field_mapping(fields=['url', 'title'], index='py4', doc_type='doc'))
5.Cluster(集群相关)
- es.cluster.get_settigns,获取集群设置。
print(es.cluster.get_settings())
- es.cluster.health,获取有关群集运行状况的非常简单的状态。
print(es.cluster.health())
- es.cluster.state,获取整个集群的综合状态信息。
print(es.cluster.state())
- es.cluster.stats,返回群集的当前节点的信息。
print(es.cluster.stats())
6.Node (节点相关)
- es.nodes.info,返回集群中节点的信息。
print(es.nodes.info()) # 返回所节点
print(es.nodes.info(node_id='node1')) # 指定一个节点
print(es.nodes.info(node_id=['node1', 'node2'])) # 指定多个节点列表
- es.nodes.stats,获取集群中节点统计信息。
print(es.nodes.stats())
print(es.nodes.stats(node_id='node1'))
print(es.nodes.stats(node_id=['node1', 'node2']))
- es.nodes.hot_threads,获取指定节点的线程信息。
print(es.nodes.hot_threads(node_id='node1'))
print(es.nodes.hot_threads(node_id=['node1', 'node2']))
- es.nodes.usage,获取集群中节点的功能使用信息。
print(es.nodes.usage())
print(es.nodes.usage(node_id='node1'))
print(es.nodes.usage(node_id=['node1', 'node2']))
7.Cat(查询)
- es.cat.allocation,返回分片使用情况。
print(es.cat.allocation())
print(es.cat.allocation(node_id=['node1']))
print(es.cat.allocation(node_id=['node1', 'node2'], format='json'))
- es.cat.count,Count提供对整个群集或单个索引的文档计数的快速访问。
print(es.cat.count()) # 集群内的文档总数
print(es.cat.count(index='py3')) # 指定索引文档总数
print(es.cat.count(index=['py3', 'py2'], format='json')) # 返回两个索引文档和
- es.cat.health,从集群中health里面过滤出简洁的集群健康信息。
- es.cat.indices,返回索引的信息;也可以使用此命令进行查询集群中有多少索引。
- es.cat.master,返回集群中主节点的IP,绑定IP和节点名称。
- es.cat.plugins,返回节点的插件信息。
- es.cat.shards,返回哪个节点包含哪些分片的信息。
- 上一篇: 李宁球拍系列详细介绍
- 下一篇: ElasticSearch笔记系列——简单查询、条件查询、聚合查询
猜你喜欢
- 2024-12-31 ElasticSearch笔记系列——简单查询、条件查询、聚合查询
- 2024-12-31 李宁球拍系列详细介绍
- 2024-12-31 Intel酷睿 14代ES工程样品曝光,不过是移动版P系列U
- 2024-12-31 ElasticSearch系列-索引创建
- 2024-12-31 快速了解黑鲨5系列,主打方向性能,散热和快充,性价比并不高
- 2024-12-31 防水透气膜有正反面吗?ES系列防水透气膜是什么材料?
- 2024-12-31 FOSSIL Georgia系列 ES3894 女款时装腕表 629元包邮
- 2024-12-31 400元档610660主板23年巨献!精粤全系列BIOS更新修复功耗降100%
- 2024-12-31 Jasmine Tookes 演绎维密最新内衣系列Lookb
- 2024-12-31 Mesto美卓ES?系列水平筛图纸ES202/203/302/303/402/403/502/503
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 电脑显示器花屏 (79)
- 403 forbidden (65)
- linux怎么查看系统版本 (54)
- 补码运算 (63)
- 缓存服务器 (61)
- 定时重启 (59)
- plsql developer (73)
- 对话框打开时命令无法执行 (61)
- excel数据透视表 (72)
- oracle认证 (56)
- 网页不能复制 (84)
- photoshop外挂滤镜 (58)
- 网页无法复制粘贴 (55)
- vmware workstation 7 1 3 (78)
- jdk 64位下载 (65)
- phpstudy 2013 (66)
- 卡通形象生成 (55)
- psd模板免费下载 (67)
- shift (58)
- localhost打不开 (58)
- 检测代理服务器设置 (55)
- frequency (66)
- indesign教程 (55)
- 运行命令大全 (61)
- ping exe (64)
本文暂时没有评论,来添加一个吧(●'◡'●)