zoukankan      html  css  js  c++  java
  • python操作Elasticsearch7.x

    python3  elasticsearch 先看下面我给准备的干货,如果不能满足,pls 

    官网文档  :https://pypi.org/project/elasticsearch/ 

     dev 文档: https://elasticsearch-py.readthedocs.io/en/v7.15.1/

      开发API官网 例子: https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/examples.html

    update delete documnt  见文章最后

    1依赖: python3.7 

    pip3 install elasticsearch==7.9.1

    1、连接ES,创建索引

    from elasticsearch import Elasticsearch
     
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
    index_name = 'my_index'
     
    request_body = {
        'mappings': {
            'properties': {
                'name': {
                    'type': 'text'
                },
                'id': {
                    'type': 'integer'
                },  
            }
        }
    }
     
    # 创建索引
    es.indices.create(index=index_name, body=request_body)

    2、判断索引是否存在

    为防止在创建索引的时候出现重复,产生错误,在创建之前最好判断一下索引是否存在

    from elasticsearch import Elasticsearch
     
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
    # 索引存在,先删除索引
    if es.indices.exists(index_name):
        es.indices.delete(index=index_name)
    else:
        print('索引不存在,可以创建')
    # 创建索引
    es.indices.create(index=index_name, body=request_body)
          
    # 查看索引的信息
    print(es.info())

    也可以访问 :localhost:9200/_cat/indices?v  查看所有的索引

    3、删除索引

    删除指定地址的ES服务的索引

    from elasticsearch import Elasticsearch
     
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
    es.indices.delete(index=index_name)

    4、索引增加数据

    from elasticsearch import Elasticsearch
    index_name = 'my_index'
     
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
    request_body = {
        'mappings': {
            'properties': {
                'name': {
                    'type': 'text'
                },
                'id': {
                    'type': 'integer'
                },  
            }
        }
    }
     
    # 索引存在,先删除索引
    if es.indices.exists(index_name):
        es.indices.delete(index=index_name)
    else:
        print('索引不存在,可以创建')
    # 创建索引
    es.indices.create(index=index_name, body=request_body)
     
    es.index(index=index_name, id='1', body={
                'name': '法外狂徒-张三',
                'id': 1,
            }
            )
     
    es.index(index=index_name, id='2', body={
                'name': '普法教育-李四',
                'id': 2,
            }
            )

    5、获取数据

    from elasticsearch import Elasticsearch
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
    # 查询数据包含两种 get search
     
    # get 查询数据
    res = es.get(index=index_name, id='1')    # 我用的是es7,doc_type 可以不传,也可以查询到
     
    # search 查询数据
    # 查询所有商品数据 match_all
    body = {
        'query': {
            'match_all': {}
        },
        'from': page * page_size,       # 从第几条数据开始
        'size': page_size   # 获取多少条数据
    }
    requ_data = es.search(index=index_name, body=body)
     
     
    # 精确查找 term  查询name='法外狂徒-张三'的所有数据 
    body = {
        'query': {
            'term': {
                'name': '法外狂徒-张三'
            }
        },
    }
     
    # 精确查找 terms 
    body = {
        'query': {
            'terms': [
                '法外狂徒-张三', '普法教育-李四'
            ]
        },
    }
     
    # match: 匹配name包含 ‘法外狂徒-张三’的所有数据
    body = {
        'query': {
            'match': [
                'name': '法外狂徒-张三'
            ]
        },
    }
     
    # 查询id和name包含  法外狂徒-张三
     
    body = {
        'query': {
            'multi_match': [
                'query': '法外狂徒-张三',
                'fields': ['name', 'id']
            ]
        },
    }
     
    # 搜索出id为1或者2的所有数据
     
    body = {
        'query': {
            'ids': [
                'type': 'test_type',
                'values': ['1', '2']
            ]
        },
    }

    6、复合查询bool

    # 符合查询bool
    # bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足)
    from elasticsearch import Elasticsearch
     
     
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
     
    body = {
        'query': {
            'bool': {
                'must': [
                    {
                        'term': {
                            'name': '法外狂徒-张三'
                        }
                    },
                    {
                        'term': {
                            'id': 1
                        }
                    }
                ]    
            }    
        }
    }
     
    # 查询name=‘法外狂徒-张三’并且id=1的数据
    es.search(index=index_name, body=body)

    7、切片式查询

    # 切片式查询
    from elasticsearch import Elasticsearch
     
     
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
     
    body = {
        'query': {
            'match_all': {}    
        },
        'from': 2,        # 从第二条数据开始
        'size': 4         # 获取4条数据
    }
     
    # 从第2条数据开始,获取4条数据
    es.search(index=index_name, body=body)

    8、范围查询

    # 范围查询
    from elasticsearch import Elasticsearch
     
     
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
     
    body = {
        'query': {
            'range': {
                'id': {
                    'gte': 1,        # >=1   
                    'lte': 5,        # <=5
                }
            }    
        },
    }
     
    # 查询1<=id<=5的所有数据  
    # gte 和 lte 也可以换成换成 from 和 to
    es.search(index=index_name, body=body)

    9、前缀查询

    # 前缀查询
    from elasticsearch import Elasticsearch
     
     
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
     
    body = {
        'query': {
            'prefix': {
                'name': ''
            }    
        },
    }
     
    # 查询前缀为 ‘法’的所有数据
    es.search(index=index_name, body=body)

    10、通配符查询

    # 通配符查询
    from elasticsearch import Elasticsearch
     
     
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
     
    body = {
        'query': {
            'wildcard': {
                'name': '*三'
            }    
        },
    }
     
    # 查询所有以 ‘三’结尾的数据
    es.search(index=index_name, body=body)

    11、查询的数据排序

    # 排序查询
    from elasticsearch import Elasticsearch
     
     
    index_name = 'my_index'
    es = Elasticsearch(
        ['127.0.0.1:9200'],
        # 在做任何操作之前,先进行嗅探
        # sniff_on_start=True,
        # # 节点没有响应时,进行刷新,重新连接
        sniff_on_connection_fail=True,
        # # 每 60 秒刷新一次
        sniffer_timeout=60
    )
     
     
    body = {
        'query': {
            'match_all': {} 
        },
        'sort': {
            'id': {                    # 根据id字段升序排序
                'order': 'asc'         # asc 升序, desc 降序
            }
        }
    }
     
    # 查处所有数据并按照id大小排序
    es.search(index=index_name, body=body)

    11. update  documnt:  update:https://www.elastic.co/guide/en/elasticsearch/reference/7.15/search-search.html

    To update a document, you need to specify three pieces of information: indexid, and a body:

    from datetime import datetime
    from elasticsearch import Elasticsearch
    es = Elasticsearch()
    
    doc = {
        'author': 'author_name',
        'text': 'Interesting modified content...',
        'timestamp': datetime.now(),
    }
    res = es.update(index="test-index", id=1, body=doc)
    print(res['result'])

     

    12 Deleting a document

    You can delete a document by specifying its index, and id in the delete() method:

    es.delete(index="test-index", id=1)

    原文:https://blog.csdn.net/weixin_41979456/article/details/111932972

  • 相关阅读:
    51nod 1227 平均最小公倍数
    51nod 1238 最小公倍数之和 V3
    「G2016 SCOI2018 Round #2」B
    51nod 1258 序列求和 V4
    2301: [HAOI2011]Problem b
    POJ
    NOIP2017解题报告
    笔记-[ZJOI2014]力
    题解-Little C Loves 3 III
    李超线段树
  • 原文地址:https://www.cnblogs.com/lshan/p/15510018.html
Copyright © 2011-2022 走看看