引言
Elasticsearch 作为最流行的搜索引擎之一,其强大的查询功能是核心价值所在。本文将全面详细介绍 Elasticsearch 的各种查询类型,每个查询都配以具体示例,帮助您彻底掌握 ES 查询的使用技巧。
一、全文检索查询(Full Text Queries)
1. match 查询 - 最基础的全文搜索
作用:对文本进行分词后匹配,支持模糊搜索。
json
// 示例:搜索包含"elasticsearch"或"tutorial"的文档
{
"query": {
"match": {
"content": "elasticsearch tutorial"
}
}
}
// 实际应用:博客搜索
GET /articles/_search
{
"query": {
"match": {
"title": "机器学习 人工智能"
}
}
}
特点:
-
自动分词处理
-
支持 OR 操作(默认)
-
可设置
operator为 AND 要求全部匹配
2. match_phrase 查询 - 精确短语匹配
作用:匹配完整的短语,保持词项顺序和接近度。
json
// 示例:搜索完整短语"quick brown fox"
{
"query": {
"match_phrase": {
"title": "quick brown fox"
}
}
}
// 支持 slop 参数允许词项间隔
{
"query": {
"match_phrase": {
"title": {
"query": "quick fox",
"slop": 1 // 允许中间有1个其他词
}
}
}
}
实际案例:
json
// 搜索产品型号
{
"query": {
"match_phrase": {
"product_name": "iPhone 15 Pro"
}
}
}
// 搜索地址
{
"query": {
"match_phrase": {
"address": "北京市海淀区"
}
}
}
3. match_phrase_prefix 查询 - 短语前缀搜索
作用:用于实现搜索建议,最后一个词项进行前缀匹配。
json
// 示例:实现自动完成功能
{
"query": {
"match_phrase_prefix": {
"title": "elasticsearch tuto"
}
}
}
// 完整参数配置
{
"query": {
"match_phrase_prefix": {
"title": {
"query": "quick bro",
"slop": 2,
"max_expansions": 10 // 限制扩展数量
}
}
}
}
应用场景:
-
搜索框自动提示
-
产品名称搜索建议
-
地址搜索
4. match_bool_prefix 查询 - 布尔前缀匹配
作用:前N-1个词精确匹配,最后一个词前缀匹配。
json
// 示例:宽松的短语前缀搜索
{
"query": {
"match_bool_prefix": {
"title": "elasticsearch tuto"
}
}
}
// 等价于:elasticsearch AND tuto*
与 match_phrase_prefix 对比:
-
match_bool_prefix:不要求词项顺序和接近度 -
match_phrase_prefix:要求词项顺序和接近度
5. multi_match 查询 - 多字段搜索
作用:在多个字段中执行相同的搜索查询。
json
// 基础用法
{
"query": {
"multi_match": {
"query": "elasticsearch",
"fields": ["title", "content", "tags"]
}
}
}
// 带权重配置
{
"query": {
"multi_match": {
"query": "无线耳机",
"fields": [
"product_name^3", // 权重3倍
"category^2", // 权重2倍
"description^1.5", // 权重1.5倍
"tags" // 默认权重1倍
]
}
}
}
// 不同类型配置
{
"query": {
"multi_match": {
"query": "elasticsearch tutorial",
"fields": ["title", "content"],
"type": "best_fields", // 还可选 most_fields, cross_fields
"tie_breaker": 0.3
}
}
}
实际应用:
json
// 电商商品搜索
GET /products/_search
{
"query": {
"multi_match": {
"query": "华为手机",
"fields": ["name^3", "category^2", "brand^2", "description"],
"type": "best_fields"
}
}
}
6. query_string 查询 - 高级查询语法
作用:支持完整的Lucene查询语法。
json
// 基础用法
{
"query": {
"query_string": {
"query": "elasticsearch AND tutorial",
"fields": ["title", "content"]
}
}
}
// 复杂查询示例
{
"query": {
"query_string": {
"query": "(title:elasticsearch OR content:elasticsearch) AND author:john_doe AND publish_date:[2024-01-01 TO *]",
"default_operator": "AND"
}
}
}
// 支持各种操作符
{
"query": {
"query_string": {
"query": "name:iPhone* AND price:[5000 TO 10000] -color:\"red\"",
"fields": ["name", "category", "description"]
}
}
}
语法特性:
-
AND/OR/NOT布尔逻辑 -
field:value字段指定 -
*通配符 -
[min TO max]范围查询 -
"phrase"短语搜索 -
~模糊搜索
7. simple_query_string 查询 - 安全的查询字符串
作用:简化版的query_string,语法错误不会导致查询失败。
json
{
"query": {
"simple_query_string": {
"query": "elasticsearch +tutorial -beginner",
"fields": ["title", "content"],
"default_operator": "and"
}
}
}
二、词项级别查询(Term-level Queries)
8. term 查询 - 精确值匹配
作用:对不分词的字段进行精确匹配。
json
{
"query": {
"term": {
"status": {
"value": "published"
}
}
}
}
// 实际应用:枚举值搜索
{
"query": {
"term": {
"category": "electronics"
}
}
}
9. terms 查询 - 多值精确匹配
json
{
"query": {
"terms": {
"tags": ["elasticsearch", "logstash", "kibana"]
}
}
}
10. range 查询 - 范围查询
json
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 1000
}
}
}
}
// 日期范围查询
{
"query": {
"range": {
"publish_date": {
"gte": "2024-01-01",
"lt": "2024-02-01",
"format": "yyyy-MM-dd"
}
}
}
}
11. exists 查询 - 存在性检查
json
{
"query": {
"exists": {
"field": "image_url"
}
}
}
12. prefix 查询 - 前缀匹配
json
{
"query": {
"prefix": {
"sku": "IPHONE-15"
}
}
}
13. wildcard 查询 - 通配符匹配
json
{
"query": {
"wildcard": {
"email": "*@gmail.com"
}
}
}
14. regexp 查询 - 正则表达式
json
{
"query": {
"regexp": {
"phone": "[0-9]{3}-[0-9]{3}-[0-9]{4}"
}
}
}
15. fuzzy 查询 - 模糊匹配
json
{
"query": {
"fuzzy": {
"title": {
"value": "elastisearch",
"fuzziness": "AUTO"
}
}
}
}
三、复合查询(Compound Queries)
16. bool 查询 - 布尔逻辑组合
作用:组合多个查询条件的最常用方式。
json
{
"query": {
"bool": {
"must": [
{ "match": { "title": "elasticsearch" } }
],
"should": [
{ "match": { "tags": "tutorial" } },
{ "match": { "tags": "beginner" } }
],
"must_not": [
{ "range": { "price": { "gt": 1000 } } }
],
"filter": [
{ "term": { "status": "published" } },
{ "range": { "publish_date": { "gte": "2024-01-01" } } }
]
}
}
}
子句说明:
-
must:必须匹配,贡献得分 -
should:应该匹配,贡献得分 -
must_not:必须不匹配,不贡献得分 -
filter:必须匹配,不贡献得分(性能更好)
17. boosting 查询 - 权重控制
json
{
"query": {
"boosting": {
"positive": {
"match": { "content": "apple" }
},
"negative": {
"match": { "content": "pie" }
},
"negative_boost": 0.2
}
}
}
18. constant_score 查询 - 固定分数
json
{
"query": {
"constant_score": {
"filter": {
"term": { "category": "premium" }
},
"boost": 1.5
}
}
}
四、实战综合示例
电商商品搜索系统
json
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "无线蓝牙耳机",
"fields": ["name^3", "category^2", "description"],
"type": "best_fields"
}
}
],
"filter": [
{
"range": {
"price": {
"gte": 100,
"lte": 2000
}
}
},
{
"term": {
"in_stock": true
}
},
{
"terms": {
"brand": ["apple", "samsung", "sony"]
}
}
],
"should": [
{
"range": {
"rating": {
"gte": 4.5,
"boost": 2.0
}
}
},
{
"term": {
"is_premium": {
"value": true,
"boost": 1.5
}
}
}
]
}
},
"highlight": {
"fields": {
"name": {},
"description": {}
}
},
"sort": [
{
"_score": "desc"
},
{
"sales": "desc"
}
],
"from": 0,
"size": 20
}
日志分析系统
json
GET /logs/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "ERROR OR WARN OR Exception",
"fields": ["message", "level"]
}
}
],
"filter": [
{
"range": {
"timestamp": {
"gte": "2024-03-01T00:00:00",
"lte": "2024-03-01T23:59:59"
}
}
},
{
"terms": {
"application": ["web-api", "payment-service"]
}
}
]
}
},
"aggs": {
"error_by_app": {
"terms": {
"field": "application"
}
}
}
}
五、性能优化建议
-
合理使用查询类型:
-
精确匹配用
term查询 -
全文搜索用
match查询 -
过滤条件用
filter上下文
-
-
索引设计优化:
-
为搜索字段设置合适的mapping
-
使用keyword类型存储不分词的字段
-
-
查询优化:
-
避免前导通配符
*search -
限制模糊搜索的fuzziness
-
使用query_string时注意语法复杂度
-
总结
Elasticsearch 提供了丰富多样的查询类型,可以满足从简单到复杂的各种搜索需求。掌握这些查询的使用场景和特点,能够帮助您构建更加高效和精准的搜索系统。
核心选择原则:
-
简单全文搜索 →
match查询 -
精确短语匹配 →
match_phrase查询 -
搜索建议 →
match_phrase_prefix查询 -
多字段搜索 →
multi_match查询 -
复杂逻辑 →
bool查询组合 -
精确值过滤 →
term/terms查询 -
高级搜索语法 →
query_string查询
通过灵活组合这些查询类型,您可以构建出功能强大、性能优异的搜索解决方案。
1814

被折叠的 条评论
为什么被折叠?



