ES入門教程
1. 創建ES對象
from elasticsearch import Elasticsearch
# 實例化一個ip為localhost,端口為9200,允許超時一小時的es對象
es = Elasticsearch(hosts="localhost",port=9200,timeout=3600)
# 1. 創建 索引
index_name = "test"
es.indices.create(index=index_name)# 2. 刪除 索引
es.indices.delete(index='123')# 3. 插入數據
doc = {"name": "方天", "age": "23"}
es.index(index=index_name, id=2, document=doc)# 4. 刪除數據
## 4.1 es.delete 刪除指定 id 數據
es.delete(index='test',id='2')# 5. 更新數據
## 5.1 es.update():更新指定字段
doc = {'name': '李邱俊','age': '20'}
es.update(index='test',id='2',doc=doc)
2. 數據查詢(最重要)
1. es.search()
:按照指定規則查詢
res = es.search(index='test', query={'match_all': {}})
print(res)
參數說明:
參數 | 說明 |
---|---|
index | 要查詢的索引名稱 |
size | 查詢多少條數據(默認10) |
from_ | 從第幾條開始查詢(用于分頁) |
filter_path | 過濾返回字段,只顯示指定內容 |
query | 查詢規則 |
sort | 排序方式 |
2. 常見查詢方式
? 2.1 查詢所有數據:match_all
res = es.search(index='test', query={'match_all': {}})
? 2.2 模糊查詢(分詞):match
res = es.search(index='test', query={'match': {'name': '方'}})
? 2.3 短語匹配(不分詞):match_phrase
res = es.search(index='test', query={'match_phrase': {'name': '方天'}})
? 2.4 精確查詢單值:term
res = es.search(index='test', query={'term': {'name.keyword': '方天'}})
注意:如果字段是
text
類型,需要用.keyword
進行精確匹配。
? 2.5 精確查詢多值:terms
res = es.search(index='test', query={'terms': {'name.keyword': ['方天', '李邱俊']}})
? 2.6 多字段查詢:multi_match
res = es.search(index='test',query={'multi_match': {'query': '方天','fields': ['name', 'age']}}
)
? 2.7 前綴查詢:prefix
res = es.search(index='test', query={'prefix': {'name.keyword': '方'}})
? 2.8 通配符查詢:wildcard
res = es.search(index='test', query={'wildcard': {'name.keyword': '方?天'}})
? 表示一個字符,* 表示0個或多個字符
? 2.9 正則查詢:regexp
res = es.search(index='test', query={'regexp': {'name.keyword': '方.*'}})
? 2.10 多條件查詢:bool
must
:與(AND)
res = es.search(index='test', query={'bool': {'must': [{'match': {'name': '方天'}},{'term': {'age': '23'}}]}
})
should
:或(OR)
res = es.search(index='test', query={'bool': {'should': [{'match': {'name': '方天'}},{'match': {'name': '李邱俊'}}]}
})
must_not
:非(NOT)
res = es.search(index='test', query={'bool': {'must_not': [{'term': {'name.keyword': '方天'}}]}
})
? 2.11 存在字段查詢:exists
res = es.search(index='test', query={'exists': {'field': 'age'}})
? 2.12 范圍查詢:range
res = es.search(index='test', query={'range': {'age': {'gte': 20,'lte': 30 }}
})
? 2.13 嵌套查詢:nested
假設數據結構為:
{"name": "方天","info": {"hobby": "籃球","city": "北京"}
}
查詢嵌套字段:
res = es.search(index='test', query={'nested': {'path': 'info','query': {'match': {'info.hobby': '籃球'}}}
})
3. 排序:sort
升序(asc)
res = es.search(index='test', sort={'age': {'order': 'asc'}})
降序(desc)
res = es.search(index='test', sort={'age': {'order': 'desc'}})
4. 分頁查詢:size
和 from_
res = es.search(index='test', size=5, from_=0)
5. 過濾返回字段:filter_path
res = es.search(index='test',filter_path=['hits.hits._source.name']
)
6. 完整示例
# 查詢 name 包含“方”且 age 在 20 到 30 之間,按 age 升序排列,只返回前 5 條
res = es.search(index='test',query={'bool': {'must': [{'match': {'name': '方'}}],'filter': [{'range': {'age': {'gte': 20, 'lte': 30}}}]}},sort={'age': {'order': 'asc'}},size=5
)# 打印結果
for hit in res['hits']['hits']:print(hit['_source'])