1.python鏈接ES:
from elasticsearch import Elasticsearch
self.es = Elasticsearch([{'host': "192.168.1.88", 'port': 9200}])
可以指定鏈接的ES的IP
2.ES插入數(shù)據(jù):
2.1 插入單條數(shù)據(jù)
for i in range(10):
self.es.index(index="my-index", doc_type="test-type", body={"any": "data01", "timestamp": i})
2.2批量插入數(shù)據(jù) [{},{}]
from elasticsearch import helpers
helpers.bulk(self.es, [{'_index':"my-index",'_type':'test-type'}, {}])
3.ES刪除數(shù)據(jù):
3.1 刪除單條數(shù)據(jù) 指定id
self.es.delete(index='my-index', doc_type='test-type', id='jqcvIGsB6xO89rzf_sKP')
3.2 刪除指定條件數(shù)據(jù) 批量刪除
# 根據(jù)指定條件刪除 查詢條件必須符合DLS格式
query = {
# 查詢語句
"query": {
'range': {'timestamp': {'lte': "2019-06-04T10:06:12.629748"}}
}
}
self.es.delete_by_query(index='my-index', doc_type='test-type', body=query)
4.ES更新數(shù)據(jù):
ES不支持更新操作辙芍,具體更新操作底層實(shí)現(xiàn)的原理是: 刪除原來索引的數(shù)據(jù),插入新索引的數(shù)據(jù)。每一次更新,es的_version字段內(nèi)容會(huì)遞增陨界。
4.1 依據(jù)id更新單條數(shù)據(jù):
# 更新單條數(shù)據(jù)
doc = {
'doc': {'test': "哈哈哈",
'sdfs':"fdasfsdf"
}
}
self.es.update(index='my-index', doc_type='test-type', id="tqdHIGsB6xO89rzfnsIL", body=doc)
4.2 依據(jù)條件,批量更新數(shù)據(jù):
body = {
# 查詢條件
'query': {
'term': {
"timestamp": 6
}
},
# 更新內(nèi)容 第一種更新方式
'script': {
"source": "ctx._source.key_name = 'update_value'",
},
# 更新內(nèi)容 第二種更新方式
'script': {
'source': "ctx._source.key_name = params.tags",
"params": {
"tags": "hahhah"
},
},
# 更新內(nèi)容 第三種更新方式
'script': {
"source": "if(ctx._source.key_name == 'es_value'){ctx._source.key2_name='update_value'}else{ctx._source.key2_name='update_value'}", # if else 更新
}
}
self.es.update_by_query(index='my-index', doc_type='test-type', body=body)
5.ES查詢數(shù)據(jù):
5.1 普通查詢:
第一種方式:
# 返回全部數(shù)據(jù)
result = self.es.search(index='my-index', doc_type='test-type')
第二種方式:
body ={
'query':{
'match_all':{}
}
}
result = self.es.search(index='my-index',doc_type='test-type',body=body)
5.2 條件查詢:
5.2.1 term 精確查詢
# term 查詢timestamp為0的所有數(shù)據(jù)
body = {
'query': {
'term': {
'properties.provinces_name.keyword': '陜西'
}
},
}
5.2.2 terms查詢多個(gè)條件
# terms 查詢多個(gè)條件 返回timestamp為0或者1的所有數(shù)據(jù)
body = {
'query': {
'terms': {
'timestamp': [0, 1]
}
}
}
5.2.3 match 匹配多個(gè)分詞
# # match 精確匹配 test 值為lalalla的數(shù)據(jù)
body = {
'query': {
'match': {
'properties.provinces_name.keyword': '陜西省'
}
}
}
match 的效果 測(cè)試 對(duì)于中文來說幌氮,等價(jià)于term眷蜓,如果匹配的是英文詞語,例如 'pre fix hha' 則會(huì)匹配 包含 pre 和 fix 已經(jīng) hha三個(gè)詞的key想鹰。
5.2.4 multi_match 匹配多個(gè)key中包含關(guān)鍵字的數(shù)據(jù)
# multi_match 匹配多個(gè)key中包含關(guān)鍵字的數(shù)據(jù)
body = {
'query': {
'multi_match': {
"query": "關(guān)鍵字",
"fields": ['test', '待匹配key']
}
}
}
5.2.5 ids 依據(jù)id匹配多個(gè)值
body = {
'query': {
'ids': {
'values': ['SacWIGsB6xO89rzf1RBH', 'VqcWIGsB6xO89rzf1RBH','id值']
}
}
}
5.2.6 range 范圍查詢
# 范圍查詢 range
body = {
'query': {
'range': {
'properties.id': {
'gte': 11777028, # >
'lte': 11777151, # <
}
}
}
}
5.2.7 前綴查詢 prefix
# 前綴查詢 prefix
body = {
'query': {
'prefix': {
'properties.addr.keyword': '陜西省'
}
}
}
5.2.8 wildcard 通配符查詢
# 通配符查詢 wildcard
body = {
'query': {
'wildcard':{
'properties.addr.keyword': '陜西*'
}
}
}
wildcard 也可以用做 模糊查詢紊婉,* 代表著一個(gè)任意值。
5.3 復(fù)合查詢 bool查詢, must 辑舷,should,must_not
5.3.1 must 返回滿足所有條件的值喻犁。 and 與
# must [] 滿足所有條件才會(huì)返回
body = {
'query': {
'bool': {
'must': [
{
'term': {
'properties.addr.keyword': '安康市旬陽縣一零二省道',
}
},
{
'term': {
"properties.name.keyword": "大嶺鋪"
}
}
]
}
}
}
5.3.2 should 返回滿足任意一個(gè)條件的值。 or 或
# should [] 滿足任意一個(gè)條件
body = {
'query': {
'bool': {
'should': [
{
'term': {
'properties.addr.keyword': '陜西省安康市漢濱區(qū)G69(銀百高速)',
}
},
{
'term': {
"properties.name.keyword": "大嶺鋪"
}
}
]
}
}
}
5.3.3 must _not 返回不滿足所有條件的值何缓。 ! 非
# must_not ! 非 所有條件都不滿足
body = {
'query': {
'bool': {
'must_not': [
{
'term': {
'properties.addr.keyword': '陜西省安康市漢濱區(qū)G69(銀百高速)',
}
},
{
'term': {
"properties.name.keyword": "大嶺鋪"
}
}
]
}
},
'from': 0, # 返回指定數(shù)量
'size': 50,
}
ES的條件查詢肢础,默認(rèn)是返回10條數(shù)據(jù)。 可以通過 "from","size"碌廓, 來指定返回?cái)?shù)據(jù)的數(shù)量和位置传轰。
5.4 sort 排序
body = {
'query': {
'match_all': {}
},
'sort': {
'properties.id': { # 根據(jù)某個(gè)字段升序降序
"order": "desc" # asc升序, desc降序
}
}
}
5.5 響應(yīng)過濾 file_path
# 只需要獲取_id數(shù)據(jù)谷婆,多個(gè)條件用逗號(hào)隔開
result = self.es.search(index='map_data', doc_type='Feature',
filter_path=['hits.hits._id,hits.hits._source.properties.area_name'])
5.6 count 查詢數(shù)量
# 查詢數(shù)據(jù)數(shù)量 count
result = self.es.count(index='map_data', doc_type='Feature')
print(result)
最終的查詢語句如下:
result = self.es.search(index='map_data', doc_type='Feature', body=body)
print(result['hits']['hits'])
print(len(result['hits']['hits']))
body 對(duì)應(yīng)的是各個(gè)查詢方法的語句慨蛙。
至此 就是python 操作Elasticsearch常用的增刪改查的操作辽聊。
想要完整代碼,還有哪些不懂的小伙伴可以私我留言期贫,或者加我QQ3479920009跟匆,備注CSDN。