一、連接elasticsearch
// 下載es第三方庫
pip install elasticsearch
// 導包
from elasticsearch import Elasticsearch
// 連接
es = Elasticsearch(['127.0.0.1:9200'])
// 創(chuàng)建索引
es.indices.create(index='test_index1', ignore=400)
"""
官方文檔:https://elasticsearch-py.readthedocs.io/
"""
二、基本操作
// 刪除索引
es.indices.delete(index='test_index1')
// 插入數(shù)據(jù)
es.index(index='test_index1', doc_type='doc', id=1, body={"name": "kitty", "age": 18})
"""
注意:若不指定id,則id為20為隨機碼
"""
三、基本查詢
// 按id查詢
es.get(index='test_index1', doc_type='doc', id=1)
// 全查(此時不允許出現(xiàn)id!)
body = {
"query": {
"match_all": {}
}
}
es.search(index='test_index1', doc_type='doc', body=body)
// 按條件查詢
body = {
"query": {
"bool": {
"must": [
"match": {
"name": "zqx"
}
],
"filter": {
"range": {
"age": {"gte": 18 }
}
}
}
}
}
es.search(index='goods', doc_type='fruit', body=body)
// 按條件刪除
1)es.delete(index='test_index1', doc_type='doc', id=1)
2)body = {
"query": {
"match": {
"name": "zqx"
}
}
}
es.delete_by_query(index='test_index1', doc_type='doc', body=body)
四、創(chuàng)建mapping
body = {
"mappings": {
"doc": {
"properties": {
"name": {
"type": "text",
},
"age": {
"type": "long"
}
}
}
}
}
es.indices.create(idnex='shang', body=body)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。