本文主要介绍用python操作elasticsearch的基本函数。
前提:配置elasticsearch的数据库,配置好账号密码
连接数据库
from elasticsearch import Elasticsearch
hosts = ['10.245.142.54:9200']
self.es = Elasticsearch(
hosts,
http_auth=('elastic', 'test123'),##账号密码
scheme="http",
)
新增数据
'''
新增
'''
def add(self):
result = self.es.index(index='lookback_contact'
,id='20220901-XXXXXX'
,body={'serialno': 'XXXXXX' })
print('新增结果:%s' % result)
更新数据
'''
更新
'''
def update(self):
result = self.es.update(index='lookback_contact'
,id='20220901-XXXXX'
,body={'serialno': 'XXXXXX' })
print('更新结果:%s' % result)
删除数据
'''
删除
'''
def delete(self):
result = self.es.delete(index="lookback_contact"
,doc_type="_doc"
,id='20220901-XXXXXX')
print('删除结果 %s' % result)
基础查询
'''
基础查询
'''
def base(self):
result = self.es.get(index="lookback_contact"
,doc_type='_doc'
,id='0003002501030013')
print(result)
按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()
切片
'''
切片
'''
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)
模糊查询
'''
模糊
# 查询方法:模糊查询(会被分词)。
# 比如 我爱你中国,会查到只包含:“我爱你”, “中国”的内容
'''
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)
包含查询
'''
包含查询
# 查询方法:模糊查询(不会被分词)。会查到包含:“我爱你中国”的内容
'''
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)
精准查询
'''
精准查询
'''
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)
多个条件精准查询
'''
多个条件精准查询
'''
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)
前缀查询
'''
前缀查询
'''
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)
通配符查询
'''
通配符查询
'''
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)
正则查询
'''
正则查询
'''
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)
多条件 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)
多条件 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)
多条件 []各条件都不满足
'''
多条件 []各条件都不满足
'''
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)
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)
大于小于查询
'''
# 大于小于查询
'''
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)
排序
'''
排序
'''
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)