zoukankan      html  css  js  c++  java
  • python对接elasticsearch的进阶操作

    一.关于别名的操作

    • es.indices.put_alias,为一个或多个索引创建别名,查询多个索引的时候,可以使用这个别名
    print(es.indices.put_alias(index='p3', name='p3_alias'))  # 为单个索引创建别名
    print(es.indices.put_alias(index=['p3', 'p2'], name='p23_alias'))  # 为多个索引创建同一个别名,联查用
    • es.indices.delete_alias,删除一个或多个别名
    #必须指定索引和要删除的别名,因为一个索引可能对应多个别名 index和name的参数必须同时传入
    pprint(es.indices.delete_alias(index=['person'],name='wocao')) #{'acknowledged': True} 
    pprint(es.indices.delete_alias(index=['person','person1'],name='wocao')) #{'acknowledged': True}
    • es.indices.get_alias,查询索引所存在的别名
    print(es.indices.get_alias(index=['person1']))  #{'person1': {'aliases': {'wocao': {}}}}
    
    print(es.indices.get_alias(index=['p2', 'p3']))
    •  es.indices.exists_alias,判断一个索引是否存在某个别名
    print(es.indices.exists_alias(name='wocao',index='person'))  #True
    
    

    二. 查看索引的相关配置

      es.indices.get_mapping,检索索引或索引/类型的映射定义

    pprint(es.indices.get_mapping(index='person'))

      es.indices.get_settings,检索一个或多个(或所有)索引的设置。

    pprint(es.indices.get_settings(index='person'))

      es.indices.get,允许检索有关一个或多个索引的信息。

     print(es.indices.get(index='person'))    # 查询指定索引是否存在
     print(es.indices.get(index=['person', 'person1']))

      结果:

    {'person': {'aliases': {'wocao': {}},
                'mappings': {'properties': {'age': {'type': 'long'},
                                            'name': {'fields': {'keyword': {'ignore_above': 256,
                                                                            'type': 'keyword'}},
                                                     'type': 'text'},
                                            'sex': {'fields': {'keyword': {'ignore_above': 256,
                                                                           'type': 'keyword'}},
                                                    'type': 'text'}}},
                'settings': {'index': {'creation_date': '1589706232019',
                                       'number_of_replicas': '1',
                                       'number_of_shards': '1',
                                       'provided_name': 'person',
                                       'uuid': 'ZfGU_CCbSdq-7dtqdxrLWA',
                                       'version': {'created': '7010199'}}}},
     'person1': {'aliases': {},
                 'mappings': {'properties': {'age': {'type': 'long'},
                                             'name': {'fields': {'keyword': {'ignore_above': 256,
                                                                             'type': 'keyword'}},
                                                      'type': 'text'},
                                             'sex': {'fields': {'keyword': {'ignore_above': 256,
                                                                            'type': 'keyword'}},
                                                     'type': 'text'}}},
                 'settings': {'index': {'creation_date': '1589719495845',
                                        'number_of_replicas': '1',
                                        'number_of_shards': '1',
                                        'provided_name': 'person1',
                                        'uuid': 'P8KAm08JRseulsYoQ5pEYw',
                                        'version': {'created': '7010199'}}}}}

      es.indices.get_field_mapping,检索特定字段的映射信息

    pprint(es.indices.get_field_mapping(fields=['age','name'], index='person'))

      结果:

    {'person': {'mappings': {'age': {'full_name': 'age',
                                     'mapping': {'age': {'type': 'long'}}},
                             'name': {'full_name': 'name',
                                      'mapping': {'name': {'fields': {'keyword': {'ignore_above': 256,
                                                                                  'type': 'keyword'}},
                                                           'type': 'text'}}}}}}

      其他操作:   

    • es.indices.exists_type,检查索引/索引中是否存在类型/类型。
    • es.indices.flush,明确的刷新一个或多个索引。
    • es.indices.get_template,按名称检索索引模板。
    • es.indices.open,打开一个封闭的索引以使其可用于搜索。
    • es.indices.close,关闭索引以从群集中删除它的开销。封闭索引被阻止进行读/写操作。
    • es.indices.clear_cache,清除与一个或多个索引关联的所有缓存或特定缓存。
    • es.indices.get_uprade,监控一个或多个索引的升级程度。
    • es.indices.put_mapping,注册特定类型的特定映射定义。
    • es.indices.put_settings,实时更改特定索引级别设置。
    • es.indices.put_template,创建一个索引模板,该模板将自动应用于创建的新索引。
    • es.indices.rollove,当现有索引被认为太大或太旧时,翻转索引API将别名转移到新索引。API接受单个别名和条件列表。别名必须仅指向单个索引。如果索引满足指定条件,则创建新索引并切换别名以指向新别名。
    • es.indices.segments,提供构建Lucene索引(分片级别)的低级别段信息

    三.cat查询

    • es.cat.aliases,返回别名信息。
    # 查询别名为wocao的索引的索引名字
    print(es.cat.aliases(name='person')) 
    
    #wocao person - - -
    #wocao person1 - - -
    
    print(es.cat.aliases(name='person', format='json'))
    #[{'alias': 'wocao', 'index': 'person', 'filter': '-', 'routing.index': '-', 'routing.search': '-'}, {'alias': 'wocao', 'index': 'person1', 'filter': '-', 'routing.index': '-', 'routing.search': '-'}]
    • 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='p3'))  # 指定索引文档总数
    print(es.cat.count(index=['p3', 'p2'], format='json'))  # 返回两个索引文档和
    • es.cat.fielddata,基于每个节点显示有关当前加载的fielddata的信息。有些数据为了查询效率,会放在内存中,fielddata用来控制哪些数据应该被放在内存中,而这个es.cat.fielddata则查询现在哪些数据在内存中,数据大小等信息。

    1 print(es.cat.fielddata())
    2 print(es.cat.fielddata(format='json', bytes='b'))
    1 bytes 单位'b','k','kb','m','mb','g','gb','t','tb' ,'p','pb'
    • es.cat.health,从集群中health里面过滤出简洁的集群健康信息
    1 print(es.cat.health())
    2 print(es.cat.health(format='json'))
    • es.cat.help,返回es.cat的帮助信息。
    1 print(es.cat.help())
    • es.cat.indices,返回索引的信息。
    1 print(es.cat.indices())
    2 print(es.cat.indices(index='p3'))
    3 print(es.cat.indices(index='p3', format='json'))
    • es.cat.master,返回集群中主节点的IP,绑定IP和节点名称。
    1 print(es.cat.master())
    2 print(es.cat.master(format='json'))
    • es.cat.nodeattrs,返回节点的自定义属性。
    1 print(es.cat.nodeattrs())
    2 print(es.cat.nodeattrs(format='json'))
    • es.cat.nodes,返回节点的拓扑,这些信息在查看整个集群时通常很有用,特别是大型集群。我有多少符合条件的节点?
    1 print(es.cat.nodes())
    2 print(es.cat.nodes(format='json'))
    • es.cat.plugins,返回节点的插件信息。
    1 print(es.cat.plugins())
    2 print(es.cat.plugins(format='json'))
    • es.cat.segments,返回每个索引的Lucene有关的信息。
    1 print(es.cat.segments())
    2 print(es.cat.segments(index='p3'))
    3 print(es.cat.segments(index='p3', format='json'))
    • es.cat.shards,返回哪个节点包含哪些分片的信息。
    1 print(es.cat.shards())
    2 print(es.cat.shards(index='p3'))
    3 print(es.cat.shards(index='p3', format='json'))
    • es.cat.thread_pool,获取有关线程池的信息。
    1 print(es.cat.thread_pool())

    转自:https://www.cnblogs.com/Alexephor/p/11398060.html

    更多玩法https://elasticsearch-py.readthedocs.io/en/master/api.html

  • 相关阅读:
    【从0开始Tornado网站】主页登录和显示的最新文章
    2014阿里巴巴网上笔试题-文章3大标题-公共最长的字符串长度
    取消改变基本数据——应用备忘录模式
    Hibernate进化史-------Hibernate概要
    xcode 快捷键
    Android多画面幻灯片:ViewPager基础上,利用与PagerTabStrip出生缺陷(源代码)
    uva 11991
    创建与删除索引
    HDU1203_I NEED A OFFER!【01背包】
    Java面试宝典2013版(超长版)
  • 原文地址:https://www.cnblogs.com/tjp40922/p/12906905.html
Copyright © 2011-2022 走看看