欢迎莅临我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。
持续学习,不断总结,共同进步,为了踏实,做好当下事儿~
非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。 ✨✨ 欢迎订阅本专栏 ✨✨
The Start点点关注,收藏不迷路
|
Elasticsearch是基于Lucene的分布式搜索引擎,其核心特性包括:
示例映射:
{
"mappings": {
"properties": {
"title": {"type": "text", "analyzer": "ik_max_word"},
"price": {"type": "double"}
}
}
}
安装官方客户端:
pip install elasticsearch
# 可选DSL库
pip install elasticsearch-dsl
验证安装:
import elasticsearch
print(elasticsearch.__version__) # 应输出版本号如7.15.0
单节点连接(默认配置):
es = Elasticsearch() # 等价于localhost:9200
多节点集群连接:
es = Elasticsearch(
["node1:9200", "node2:9200"],
sniff_on_start=True # 启动时发现所有节点
)
带认证的HTTPS连接:
es = Elasticsearch(
["https://es-host:9200"],
http_auth=('admin', 'password'),
use_ssl=True,
verify_certs=True
)
es = Elasticsearch(
["node1:9200"],
timeout=30, # 请求超时秒数
max_retries=3, # 失败重试次数
retry_on_timeout=True
)
创建含自定义映射的索引:
mapping = {
"mappings": {
"properties": {
"content": {"type": "text"},
"timestamp": {"type": "date"}
}
}
}
es.indices.create(index="logs", body=mapping)
单文档插入:
doc = {"title": "Python教程", "price": 99.9}
es.index(index="books", id=1, body=doc) # 指定ID
批量插入(高效方式):
from elasticsearch.helpers import bulk
actions = [
{"_op_type": "index", "_index": "books", "_id": i, "title": f"Book {i}"}
for i in range(100)
]
bulk(es, actions)
乐观锁更新:
es.update(
index="books",
id=1,
body={"doc": {"price": 89.9}},
version=2, # 只有当前版本为2时才更新
version_type="external"
)
分页查询示例:
query = {
"query": {"match_all": {}},
"from": 0,
"size": 10,
"sort": [{"price": {"order": "desc"}}]
}
es.search(index="books", body=query)
布尔组合查询:
{
"query": {
"bool": {
"must": [{"match": {"title": "python"}}],
"filter": [{"range": {"price": {"gte": 50}}}]
}
}
}
价格分桶统计:
{
"aggs": {
"price_ranges": {
"range": {
"field": "price",
"ranges": [{"to": 50}, {"from": 50, "to": 100}]
}
}
}
}
使用生成器减少内存消耗:
def gen_data():
for i in range(10000):
yield {"_index": "logs", "_source": {"msg": f"Log entry {i}"}}
helpers.bulk(es, gen_data())
检查集群状态:
health = es.cluster.health()
print(health["status"]) # green/yellow/red
多字段加权查询:
{
"query": {
"multi_match": {
"query": "智能手机",
"fields": ["title^3", "description^2", "category"]
}
}
}
关键要点回顾:
常见问题:
from+size
,改用search_after
学习建议:
道阻且长,行则将至,让我们一起加油吧!
The Start点点关注,收藏不迷路
|
width=“100%”>