ElasticSearch - () Python操作es

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,返回哪个节点包含哪些分片的信息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值