數(shù)操作系統(tǒng):windows10
我開始使用最新的版本,8.4.1但是使用過程中kibana啟動不了,就索性使用舊版;
下載地址:
es7.17.6 下載地址?
kibana7.17.6下載地址
解壓到合適的位置,更改elasticsearch.yml
添加配置如下:
cluster.name: robin-es
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
更改kibana.yml配置
i18n.locale: "zh-CN"
到各自的bin目錄下啟動兩個服務(wù)bat文件,
在瀏覽器中執(zhí)行http:://localhost:9200
可以看到j(luò)son就對了
{
"name" : "node-1",
"cluster_name" : "robin-es",
"cluster_uuid" : "pAvuRyRESuCHtbTnfdWrvA",
"version" : {
"number" : "7.17.6",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "f65e9d338dc1d07b642e14a27f338990148ee5b6",
"build_date" : "2022-08-23T11:08:48.893373482Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
使用python需要添加一下相關(guān)的庫,我這里使用國內(nèi)的庫,并且使用代理,
注意:建議使用對應(yīng)版本的庫,否則可能不兼容。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ elasticsearch==7.17.6 --proxy="http://127.0.0.1:1081"
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ elasticsearch[async]==7.17.6 --proxy="http://127.0.0.1:1081"
連接數(shù)據(jù)庫:
indexName = "student"
client = Elasticsearch(
['127.0.0.1:9200'],
# 在做任何操作之前,先進行嗅探
# sniff_on_start=True,
# # 節(jié)點沒有響應(yīng)時,進行刷新,重新連接
sniff_on_connection_fail=True,
# # 每 60 秒刷新一次
sniffer_timeout=60
)
寫幾個增刪改查的函數(shù):
需要注意:7.15版本以上使用了新的函數(shù),舊的方式已經(jīng)不適用了
# 推薦使用 elasticsearch 需要注意版本問題
from queue import Empty
from elasticsearch import Elasticsearch
from elasticsearch import *
import json
# es 7.17
def checkIndexByName(client, indexName):
try:
res = client.indices.get(index=indexName)
# print(res)
return True
except Exception as ex:
return False
# 創(chuàng)建索引
def createIndex(client, name, doc):
ret = False
try:
# Elasticsearch.options()
resp = client.indices.create(index=name, mappings=doc["mappings"])
# print(resp['result'])
ret = True
except Exception as ex:
print(ex)
return False
return ret
# 刪除索引
def dropIndex(client, name):
ret = False
try:
# Elasticsearch.options()
result = client.indices.delete(index=name)
ret = True
except:
return False
return ret
def addDoc(client, index, doc, id):
# 重復(fù)添加,數(shù)據(jù)覆蓋
try:
resp = client.index(index=index, document=doc, id=id)
print(resp['result'])
return True
except Exception as e:
print("create index error")
return False
def delDocFromIndex(client, index, id):
try:
res = client.delete(index=index, id=id)
print(res['_shards']['successful'])
return '1'
except Exception as e:
print(e)
return '0'
def findDocById(client, index, id):
try:
res = client.get(index=index, id=id)
return res['_source']
except Exception as e:
print(e)
return 'nil'
創(chuàng)建索引的過程,可以在外部配置文件中設(shè)置相關(guān)的參數(shù),
比如我們創(chuàng)建一個學(xué)生的相關(guān)索引,我們建立一個配置文件student.json
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"dynamic": "strict",
"properties": {
"name": {
"type": "key"
},
"age": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
之后,我們創(chuàng)建索引時候這樣使用:
def load_json(filePath):
data = open(filePath, 'r').read()
return json.loads(data)
docMapping = load_json("./student.json")
# print(docMapping)
#dropIndex(client, indexName, result)
ret = checkIndexByName(client, indexName)
if not ret:
print("\nindex is exsit = %d" % ret)
createIndex(client, indexName, docMapping)
如果沒有索引,則創(chuàng)建一下;
在kibana的開發(fā)工具中可以看到相關(guān)的結(jié)果:
之后, 添加2個記錄(文檔)試試
doc = {
"name": "灰太狼",
"age": 22,
"birthday": "2000-02-02",
"tags": ["男"]
}
res = addDoc(client, indexName, doc, 13810500001)
# print(res)
doc = {
"name": "美羊羊",
"age": 10,
"birthday": "2010-01-01",
"tags": ["女"]
}
res = addDoc(client, indexName, doc, 13810500002)
# print(res)
可以在kibana中看到:
?
?目前位置,基本的增刪改查,都實現(xiàn)了,但是還需要復(fù)雜的查詢:
bodyQueryAll = {
"query": {
"match_all": {}
}
}
res = client.search(index=indexName, query=bodyQueryAll["query"])
print("查詢到%d 個" % res['hits']['total']['value'])
items = res["hits"]["hits"]
# print(items)
for item in items:
print("index=%s, id=%s doc=%s" %
(item['_index'], item['_id'], item['_source']))
查詢到2 個
index=student, id=13810501001 doc={'name': '灰太狼', 'age': 22, 'birthday': '2000-02-02', 'tags': ['男']}
index=student, id=13810501002 doc={'name': '美羊羊', 'age': 21, 'birthday': '2000-01-01', 'tags': ['女']}
在kibana中,是這樣的:
?
知道了查詢后返回數(shù)據(jù)的結(jié)構(gòu)了,就可以提取我們想要的數(shù)據(jù)了,
再添加2個查詢函數(shù):
def queryAll(client, indexName):
bodyQueryAll = {
"query": {
"match_all": {}
}
}
res = client.search(index=indexName, query=bodyQueryAll["query"])
n = res['hits']['total']['value']
#print("查詢到%d 個" % n)
items = res["hits"]["hits"]
# print(items)
# for item in items:
# print("index=%s, id=%s doc=%s" %
# (item['_index'], item['_id'], item['_source']))
return (n, items)
def queryByDoc(client, indexName, query):
res = client.search(index=indexName, query=query)
n = res['hits']['total']['value']
items = res["hits"]["hits"]
return (n, items)
測試代碼如下:
print("查全量:")
res = queryAll(client, indexName)
n = res[0]
items = res[1]
# print(items)
for item in items:
print("index=%s, id=%s doc=%s" %
(item['_index'], item['_id'], item['_source']))
queryNames = {
"bool":
{
"should": [
{"match":
{"name": "美羊羊"}
},
{
"match": {"name": "喜羊羊"}
}
]
}
}
print("查名字:")
res = queryByDoc(client, indexName, queryNames)
n = res[0]
items = res[1]
# print(items)
for item in items:
print("index=%s, id=%s doc=%s" %
(item['_index'], item['_id'], item['_source']))
輸出:
查全量:
index=student, id=13810501001 doc={'name': '灰太狼', 'age': 22, 'birthday': '2000-02-02', 'tags': ['男']}
index=student, id=13810501002 doc={'name': '美羊羊', 'age': 21, 'birthday': '2000-01-01', 'tags': ['女']}
查名字:
index=student, id=13810501002 doc={'name': '美羊羊', 'age': 21, 'birthday': '2000-01-01', 'tags': ['女']}
kibana中這樣的:
?
參考:python操作Elasticsearch7.x - lshan - 博客園
Elasticsearch API Reference — Python Elasticsearch client 8.4.1 documentation
python操作Elasticsearch7.17.0_有勇氣的牛排的博客-CSDN博客
https://github.com/elastic/elasticsearch-py/issues/1698文章來源:http://www.zghlxwxcb.cn/news/detail-469469.html
elasticsearch——入門 - 走看看文章來源地址http://www.zghlxwxcb.cn/news/detail-469469.html
到了這里,關(guān)于python使用ElasticSearch7.17.6筆記的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!