本文为Elasticsearch初学者详细解析RESTful API的核心操作与查询DSL语法,包含大量实战示例及最佳实践。
Elasticsearch(ES)作为分布式搜索分析引擎,其RESTful API是与集群交互的核心方式。通过HTTP协议实现:
核心概念速览:
概念 | 说明 | 类比关系型数据库 |
---|---|---|
索引(Index) | 文档的集合 | 数据库的表 |
文档(Document) | JSON格式的基本数据单元 | 表中的一行记录 |
分片(Shard) | 索引的水平分割单元 | 表分区 |
映射(Mapping) | 定义文档字段及类型 | 表结构定义 |
# 创建索引
PUT /products
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {"type": "text"},
"price": {"type": "float"},
"category": {"type": "keyword"},
"created_at": {"type": "date"}
}
}
}
# 删除索引 (谨慎操作!)
DELETE /products
### 2. 文档操作
```bash
# 添加文档
POST /products/_doc/1001
{
"name": "无线蓝牙耳机",
"price": 299.0,
"category": "电子产品",
"created_at": "2023-05-10"
}
# 获取文档
GET /products/_doc/1001
# 更新文档
POST /products/_update/1001
{
"doc": {
"price": 259.0
}
}
# 删除文档
DELETE /products/_doc/1001
所有搜索请求发送到_search端点:
GET /products/_search
方式 | 语法示例 | 适用场景 |
---|---|---|
URL参数查询 | ?q=category:电子产品 | 快速简单查询 |
DSL请求体查询 | JSON结构描述复杂查询逻辑 | 生产环境推荐使用 |
GET /products/_search
{
"query": {
"match": {
"name": {
"query": "降噪耳机",
"operator": "and"
}
}
}
}
对文本 分词处理 ( “降噪耳机” → [“降噪”,“耳机”])
operator参数:and(必须全部包含) 或 or(包含任意词)
GET /products/_search
{
"query": {
"term": {
"category.keyword": {
"value": "电子产品"
}
}
}
}
注意使用.keyword子字段进行未分词的精确匹配
适用于状态、标签等离散值字段
GET /products/_search
{
"query": {
"range": {
"price": {
"gte": 200,
"lte": 500
}
}
}
}
支持操作符:gt
(>), gte
(>=), lt
(<), lte
(<=)
适用于数值、日期范围过滤
GET /products/_search
{
"query": {
"bool": {
"must": [
{"match": {"name": "耳机"}}
],
"should": [
{"term": {"category.keyword": "促销品"}},
{"range": {"price": {"lte": 199}}}
],
"must_not": [
{"term": {"brand.keyword": "A品牌"}}
],
"filter": [
{"term": {"in_stock": true}}
],
"minimum_should_match": 1
}
}
}
子句 | 作用 | 是否影响相关性评分 |
---|---|---|
must | 必须满足,相当于AND | 是 |
should | 应该满足,相当于OR | 是 |
must_not | 必须不满足,相当于NOT | 否 |
filter | 必须满足,但不参与评分 | 否 |
GET /products/_search
{
"query": {
"match_phrase": {
"description": {
"query": "高清防水",
"slop": 3
}
}
}
}
-要求词语 按顺序完整出现
-slop参数允许中间间隔的词数
GET /products/_search
{
"from": 10,
"size": 5,
"sort": [
{"price": {"order": "asc"}},
{"_score": {"order": "desc"}}
],
"query": {...}
}
GET /products/_search
{
"_source": ["name", "price"],
"query": {...}
}
典型搜索结果结构:
{
"took": 15, // 查询耗时(ms)
"timed_out": false,
"hits": {
"total": {
"value": 42, // 匹配总数
"relation": "eq"
},
"max_score": 1.234, // 最高得分
"hits": [ // 结果数组
{
"_index": "products",
"_id": "1001",
"_score": 1.234,
"_source": { // 原始文档
"name": "无线蓝牙耳机",
"price": 299.0
}
}
]
}
}
精确匹配陷阱
字符串字段默认同时创建text
和keyword
类型,精确匹配需用字段名.keyword
性能优化
"bool": {
"filter": [ // 不参与评分的条件放filter
{"range": {"created_at": {"gte": "now-30d/d"}}}
]
}
from+size
处理超过10,000条的结果集,改用search_after
或scrollAPI
- X-Pack安全模块
- API密钥认证
- 网络访问控制
查询类型 | 用途 | 示例 |
---|---|---|
match | 全文搜索 | {“match”: {“title”: “ES”}} |
term | 精确值匹配 | {“term”: {“status”: 1}} |
range | 范围查询 | {“range”: {“age”: {“gte”: 18}}} |
bool | 组合多个查询条件 | 见第四节示例 |
match_phrase | 短语搜索 | {“match_phrase”: {“quote”: “to be or”}} |
下集预告《Elasticsearch RESTful API入门:全文搜索实战》