python操作elasticsearch

本文介绍了如何使用Python连接和操作Elasticsearch数据库,包括基本的增删改查功能,以及基于Lucene语法的查询、切片查询、模糊查询、精准查询、正则表达式查询、排序等高级操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要介绍用python操作elasticsearch的基本函数。

前提:配置elasticsearch的数据库,配置好账号密码

  1. 连接数据库

from elasticsearch import Elasticsearch

hosts = ['10.245.142.54:9200']
self.es = Elasticsearch(
    hosts,
    http_auth=('elastic', 'test123'),##账号密码
    scheme="http",
)
  1. 新增数据

'''
新增
'''
def add(self):
    result = self.es.index(index='lookback_contact'
        ,id='20220901-XXXXXX'
        ,body={'serialno': 'XXXXXX' })
    print('新增结果:%s' % result)
  1. 更新数据

'''
更新
'''
def update(self):
    result = self.es.update(index='lookback_contact'
        ,id='20220901-XXXXX'
        ,body={'serialno': 'XXXXXX' })
    print('更新结果:%s' % result)
  1. 删除数据

'''
删除
'''
def delete(self):
    result = self.es.delete(index="lookback_contact"
        ,doc_type="_doc"
        ,id='20220901-XXXXXX')
    print('删除结果 %s' % result)
  1. 基础查询

'''
基础查询
'''
def base(self):
    result = self.es.get(index="lookback_contact"
        ,doc_type='_doc'
        ,id='0003002501030013')    
    print(result)
  1. 按lucene语法查询

'''
按lucene语法查询
'''
def lucene(self):
    '''
    index - 索引名
    q - 查询指定匹配 使用Lucene查询语法
    from_ - 查询起始点 默认0
    doc_type - 文档类型
    size - 指定查询条数 默认10
    field - 指定字段 逗号分隔
    sort - 排序 字段:asc/desc
    body - 使用Query DSL
    scroll - 滚动查询
    '''
    result = self.es.search(index="order"
        ,doc_type="lookback_contact"
        ,q="m_id:20220901-XXXXXXX")
    print(result)
    self.es.close()
  1. 切片

'''
切片
'''
def slice(self):
    body = {
        'from' : 0,
        'size' : 20,
    }
    # 定义过滤字段,最终只显示此此段信息
    filter_path=['hits.hits._source.m_scsj',  # 字段1
                'hits.hits._source.m_slhm']   # 字段2
    result = self.es.search(index="order"
        ,doc_type="lookback_contact"
        ,filter_path = filter_path
        ,body = body)
    print(result)
  1. 模糊查询

'''
模糊
# 查询方法:模糊查询(会被分词)。
# 比如 我爱你中国,会查到只包含:“我爱你”, “中国”的内容
'''
def match(self):
    body = {
        'query':{
            'match':{
                'm_ext1' : 'XXXXXX'
            }
        },
        'size' : '20'
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']   # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_contact'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 包含查询

'''
包含查询
# 查询方法:模糊查询(不会被分词)。会查到包含:“我爱你中国”的内容
'''
def match_phrase(self):
    body = {
        'query':{
            'match_phrase':{
                 'm_ext1' : 'XXXXXX'   #keyword
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_ext2']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_contact'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 精准查询

'''
精准查询
'''
def term(self):
    body = {
        'query':{
            'term':{
                 'm_slhm' : 'XXXXXX'
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_ext2']   # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_contact'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 多个条件精准查询

'''
多个条件精准查询
'''
def terms(self):
    body = {
        'query':{
            'terms':{
                 'm_slhm' : ['13XXXXXX ','13XXXXXX']
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_contact'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 前缀查询

'''
前缀查询
'''
def prefix(self):
    body = {
        'query': {
            'prefix': { 
                'm_slhm': '135'  # 查询前缀是指定字符串的数据
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_contact'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 通配符查询

'''
通配符查询
'''
def wildcard(self):
    body = {
        'query': {
            'wildcard': { 
                'm_slhm': '*526?'  #  # ?代表一个字符,*代表0个或多个字符
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_main'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 正则查询

'''
正则查询
'''
def regexp(self):
    body = {
        'query': {
            'regexp': { 
                'm_slhm': '*[0-9].+'  # 
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_main'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 多条件 and 查询

'''
多条件 and 查询 
'''
def multi_must(self):
    body = {
        'query': {
            'bool': {
                'must':[
                    {'term':{'m_slhm' : '13XXXXXXX'}},
                    {'terms':{'m_slhm' : ['13XXXXXX']}},
                ]
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_main'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 多条件 or 查询

'''
多条件 or 查询 
'''
def multi_should(self):
    body = {
        'query': {
            'bool': {
                'should':[
                    {'term':{'m_slhm' : '13XXXXXX'}},
                    {'terms':{'m_slhm' : ['1XXXXXX']}},
                ]
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_main'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 多条件 []各条件都不满足

'''
多条件 []各条件都不满足
'''
def multi_mustnot(self):
    body = {
        'query': {
            'bool': {
                'must_not':[
                    {'term':{'m_slhm' : '13XXXXX'}},
                    {'terms':{'m_slhm' : ['13XXXX']}},
                ]
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_main'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. exists、bool嵌套查询

'''
# exists、bool嵌套查询
# 存在ziduan1的情况下,ziduan2的值必须为指定字段
'''
def exists(self):
    body = {
        'query': {
            'exists': { 
                'field': '15XXXXX'  # 查询存在ziduan1的数据
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_main'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 大于小于查询

'''
# 大于小于查询
'''
def range(self):
    body = {
        'query': {
            'range': { 
                'm_slhm': {
                    'gte' : 120,        #大于
                    'lt' : 10000000000  #小于
                }
            }
        }
    }
    filter_path=['hits.hits._source.m_ext1',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_main'
        ,filter_path = filter_path
        ,body=body)
    print(result)
  1. 排序

'''
排序
'''
def matchAll(self):
    body = {
        'query':{
            'match_all':{}
        },
        "sort": {
            'm_slsj':{"order":"asc" }
        },
    }
    filter_path=['hits.hits._source.m_slsj',  # 字段1
                'hits.hits._source.m_slhm']  # 字段2
    result = self.es.search(index='order'
        ,doc_type='lookback_main'
        ,filter_path = filter_path
        ,body=body)
    print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个高效工作的家伙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值