ES入门教程 (python 版)

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. 分页查询:sizefrom_
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'])

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

矩深AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值