python直接操作Elasticsearch7

python直接操作Elasticsearch7的命令

    本文Python代码在python3.6中测试通过,仅做记录,有问题请执教。谢谢🙏

 

简介

 本次代码基于ES7.9,python版本3.6。ES服务器是基于docker中的ES7.10

安装命令

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)

现在常用的就这些,后期还会补充,有什么错误的地方,请指正谢谢。

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值