本文目录导读:

我来详细介绍Elasticsearch查询DSL的各种条件构建方法。
基本查询结构
{
"query": {
"bool": {
"must": [],
"must_not": [],
"should": [],
"filter": []
}
},
"from": 0,
"size": 10,
"sort": [],
"_source": [],
"aggs": {}
}
叶子查询(Leaf Queries)
精确值查询
// term查询
{
"query": {
"term": {
"status": "active"
}
}
}
// terms查询(多个精确值)
{
"query": {
"terms": {
"status": ["active", "pending"]
}
}
}
全文搜索
// match查询
{
"query": {
"match": {
"title": "elasticsearch guide"
}
}
}
// match_phrase(短语匹配)
{
"query": {
"match_phrase": {
"content": "大数据分析"
}
}
}
// multi_match(多字段搜索)
{
"query": {
"multi_match": {
"query": "elasticsearch",
"fields": ["title^3", "content", "tags"],
"type": "best_fields"
}
}
}
范围查询
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 500,
"boost": 2.0
}
}
}
}
// 日期范围
{
"query": {
"range": {
"created_at": {
"gte": "2024-01-01",
"lte": "2024-12-31",
"format": "yyyy-MM-dd"
}
}
}
}
存在/缺失查询
// exists查询
{
"query": {
"exists": {
"field": "email"
}
}
}
// missing查询(ES 7.x后使用must_not)
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "email"
}
}
}
}
}
复合查询(Compound Queries)
Bool查询(最重要)
{
"query": {
"bool": {
"must": [
{ "match": { "title": "搜索" } },
{ "term": { "status": "published" } }
],
"must_not": [
{ "term": { "is_deleted": true } }
],
"should": [
{ "match": { "tags": "技术" } },
{ "match": { "tags": "教程" } }
],
"filter": [
{ "range": { "price": { "gte": 100 } } },
{ "term": { "category": "books" } }
],
"minimum_should_match": 1
}
}
}
嵌套查询(Nested)
{
"query": {
"nested": {
"path": "comments",
"query": {
"bool": {
"must": [
{ "match": { "comments.author": "John" } },
{ "range": { "comments.score": { "gte": 5 } } }
]
}
},
"inner_hits": {}
}
}
}
父子查询
{
"query": {
"has_child": {
"type": "comment",
"query": {
"term": {
"author": "John"
}
}
}
}
}
// has_parent
{
"query": {
"has_parent": {
"parent_type": "article",
"query": {
"match": {
"title": "elasticsearch"
}
}
}
}
}
聚合查询(Aggregations)
{
"size": 0,
"aggs": {
"category_stats": {
"terms": {
"field": "category",
"size": 10
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"price_range": {
"range": {
"field": "price",
"ranges": [
{ "from": 0, "to": 100 },
{ "from": 100, "to": 500 },
{ "from": 500 }
]
}
},
"date_histogram": {
"date_histogram": {
"field": "created_at",
"interval": "month"
}
}
}
}
}
}
高级查询技巧
Function Score查询
{
"query": {
"function_score": {
"query": {
"match": { "title": "elasticsearch" }
},
"functions": [
{
"filter": { "term": { "is_featured": true } },
"weight": 2
},
{
"gauss": {
"created_at": {
"origin": "2024-01-01",
"scale": "30d",
"decay": 0.5
}
}
}
],
"score_mode": "multiply",
"boost_mode": "multiply"
}
}
}
Boosting查询
{
"query": {
"boosting": {
"positive": {
"match": { "content": "apple" }
},
"negative": {
"match": { "content": "pie" }
},
"negative_boost": 0.5
}
}
}
模糊匹配
{
"query": {
"fuzzy": {
"name": {
"value": "elasticsearh",
"fuzziness": 2,
"prefix_length": 1
}
}
}
}
实际应用示例
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{ "match": { "title": "搜索关键词" } },
{ "match": { "content": "搜索关键词" } }
],
"minimum_should_match": 1
}
}
],
"filter": [
{ "term": { "status": "published" } },
{ "range": { "publish_date": { "gte": "2024-01-01" } } },
{ "terms": { "category": ["技术", "教程"] } }
]
}
},
"sort": [
{ "publish_date": { "order": "desc" } },
"_score"
],
"_source": ["title", "content", "publish_date", "category"],
"from": 0,
"size": 20
}
这些查询DSL构建方法涵盖了大多数Elasticsearch使用场景,根据具体需求组合使用不同的查询类型,可以实现复杂的搜索逻辑。