宜春網(wǎng)站建設(shè)公司哪家好公司網(wǎng)站建設(shè)服務(wù)機(jī)構(gòu)
文章目錄
- 基本操作
- 更多查詢方法
- 1. 查詢?nèi)繑?shù)據(jù)
- 2. 針對某個(gè)確定的值/字符串的查詢:term、match
- 3. 在多個(gè)選項(xiàng)中有一個(gè)匹配,就查出來:terms
- 4. 數(shù)值范圍查詢:range
- 5. 多個(gè)條件同時(shí)觸發(fā) bool
- 6. 指定返回值個(gè)數(shù) size
- 7. 返回指定列 _source
- 完整示例程序
基本操作
- 首先連接Elasticsearch數(shù)據(jù)庫,然后創(chuàng)建一個(gè)自定義的索引
from elasticsearch import Elasticsearch
import random
from elasticsearch import helpers# 連接到本地的 Elasticsearch 服務(wù)
es = Elasticsearch(hosts=["http://localhost:9200"])
index_name = "my_index"# 創(chuàng)建一個(gè)索引名為 "my_index" 的索引
if es.indices.exists(index=index_name): # 如果索引存在,刪除es.indices.delete(index=index_name)
es.indices.create(index=index_name) # 新建索引
- 新增隨機(jī)數(shù)據(jù)
這里我們創(chuàng)建隨機(jī)數(shù)據(jù)項(xiàng),包含value_num與value_choice項(xiàng),
# 新增隨機(jī)數(shù)據(jù)項(xiàng)
add_value_list: list = []
for _ in range(1000):num = random.randint(1, 100)add_value_list.append({"_index": index_name, # 注意!個(gè)參數(shù)決定要插入到哪個(gè)索引中"value_num": random.randint(1, 100),"value_choice": ["c1", 'c2', 'c3'][random.randint(0, 2)],})
# 批量插入數(shù)據(jù)
helpers.bulk(es, add_value_list)
- 查詢操作
# 查詢操作
_body_query = {"query": {"range": {"value_num": {"gte": 40, # >= 40"lte": 60 # <= 60}}},"size": 20, # 查詢20條
}
response = es.search(index=index_name, body=_body_query)
# 打印查詢的結(jié)果
for _hit in response["hits"]["hits"]:_value = _hit['_source']print("value_num:", _value["value_num"], " value_choice:", _value['value_choice'])
- 更新數(shù)據(jù)項(xiàng)
這里,我們將查詢出的數(shù)據(jù)中,通過文檔ID與修改的數(shù)據(jù)重新為數(shù)據(jù)賦值
# 更新操作
for _hit in response["hits"]["hits"]:update_body = {"doc": {"value_choice": "c4", # 更新value_choice字段為c4}}res = es.update(index=index_name, id=_hit['_id'], body=update_body)
- 刪除數(shù)據(jù)項(xiàng)
# 刪除操作
for _hit in response["hits"]["hits"]:res = es.delete(index=index_name, id=_hit['_id'])
更多查詢方法
1. 查詢?nèi)繑?shù)據(jù)
_body_query = {"query":{"match_all":{}}
}
2. 針對某個(gè)確定的值/字符串的查詢:term、match
match會執(zhí)行多個(gè)term操作,term操作精度更高
_body_query = {"query": {"match": {"value_choice": "c1"}}
}
_body_query = {"query": {"term": {"value_choice": "c1"}}
}
3. 在多個(gè)選項(xiàng)中有一個(gè)匹配,就查出來:terms
_body_query = {"query": {"terms": {"value_choice": ["c1", "c2"],}}
}
4. 數(shù)值范圍查詢:range
查詢>=40且<=60的數(shù)據(jù)
_body_query = {"query": {"range": {"value_num": {"gte": 40, # >= 40"lte": 60 # <= 60}}}
}
5. 多個(gè)條件同時(shí)觸發(fā) bool
布爾查詢可以同時(shí)查詢多個(gè)條件,也稱為組合查詢,構(gòu)造查詢的字典數(shù)據(jù)時(shí),query后緊跟bool,之后再跟bool的判斷條件,判斷條件有下面幾個(gè):
- filter:過濾器
- must:類似and,需要所有條件都滿足
- should:類似or,只要能滿足一個(gè)即可
- must_not:需要都不滿足
寫完判斷條件后,在判斷條件的list里再緊跟查詢操作的具體細(xì)節(jié)
_body_query = {"query": {"bool": {"should": [{"match": {"value_choice": "c1"} # value_choice = "c1"},{"range": {"value_num": {"lte": 50}} # value_num <= 50}]}},
}
6. 指定返回值個(gè)數(shù) size
在構(gòu)造的字典中添加size關(guān)鍵字即可
_body_query = {"query": {"range": {"value_num": {"gte": 40, # >= 40"lte": 60 # <= 60}}},"size": 20,
}
7. 返回指定列 _source
_body_query = {"query": {"range": {"value_num": {"gte": 40, # >= 40"lte": 60 # <= 60}}},"_source": ["value_num"] # 這里指定返回的fields
}
完整示例程序
from elasticsearch import Elasticsearch
import random
from elasticsearch import helpers# 連接到本地的 Elasticsearch 服務(wù)
es = Elasticsearch(hosts=["http://localhost:9200"])
index_name = "my_index"# 創(chuàng)建一個(gè)索引名為 "my_index" 的索引
if es.indices.exists(index=index_name): # 如果索引存在,刪除es.indices.delete(index=index_name)
es.indices.create(index=index_name) # 新建索引# 生成隨機(jī)數(shù)據(jù)
add_value_list: list = []
for _ in range(1000):num = random.randint(1, 100)add_value_list.append({"_index": index_name, # 注意!個(gè)參數(shù)決定要插入到哪個(gè)索引中"value_num": random.randint(1, 100),"value_choice": ["c1", 'c2', 'c3'][random.randint(0, 2)],})
# 批量插入數(shù)據(jù)
helpers.bulk(es, add_value_list)# ================== 開始增刪改查 ==================
_body_query = {"query": {"range": {"value_num": {"gte": 40, # >= 40"lte": 60 # <= 60}}},"size": 20,
}response = es.search(index=index_name, body=_body_query) # 查詢10條
# 打印查詢的結(jié)果
for _hit in response["hits"]["hits"]:_value = _hit['_source']print("value_num:", _value["value_num"], " value_choice:", _value['value_choice'])# 更新操作
for _hit in response["hits"]["hits"]:update_body = {"doc": {"value_choice": "c4", # 更新value_choice字段為c4}}res = es.update(index=index_name, id=_hit['_id'], body=update_body)# 刪除操作
for _hit in response["hits"]["hits"]:res = es.delete(index=index_name, id=_hit['_id'])