Elasticsearch 查询 DSL 完整教程

Elasticsearch 的查询 DSL(Domain Specific Language)是一种基于 JSON 的查询语言,提供了丰富的查询类型和聚合功能。掌握查询 DSL 是高效使用 Elasticsearch 的关键。本文将系统讲解各种查询类型、过滤器、聚合和高级搜索技巧。在开始之前,请确保你已经完成 Elasticsearch 部署

一、全文搜索查询

1.1 match 查询

# 标准全文搜索(会分词)
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d '{
  "query": {
    "match": {
      "description": "轻薄笔记本电脑"
    }
  }
}'

# 指定操作符(AND - 所有词都必须匹配)
{
  "query": {
    "match": {
      "description": {
        "query": "轻薄笔记本",
        "operator": "and"
      }
    }
  }
}

# minimum_should_match
{
  "query": {
    "match": {
      "description": {
        "query": "轻薄高性能笔记本",
        "minimum_should_match": "75%"
      }
    }
  }
}

1.2 multi_match 查询

{
  "query": {
    "multi_match": {
      "query": "笔记本电脑",
      "fields": ["name^3", "description"],
      "type": "best_fields"
    }
  }
}

name^3 表示 name 字段权重是 description 的 3 倍。type 可选 best_fields、most_fields、cross_fields、phrase 等。

1.3 match_phrase 查询

{
  "query": {
    "match_phrase": {
      "description": {
        "query": "轻薄笔记本",
        "slop": 2
      }
    }
  }
}

二、精确查询

# term 精确匹配(不分词)
{
  "query": {
    "term": { "category": "笔记本" }
  }
}

# terms 多值匹配
{
  "query": {
    "terms": { "category": ["笔记本", "手机", "平板"] }
  }
}

# range 范围查询
{
  "query": {
    "range": {
      "price": { "gte": 3000, "lte": 8000 }
    }
  }
}

# exists 字段存在
{
  "query": {
    "exists": { "field": "discount" }
  }
}

# wildcard 通配符
{
  "query": {
    "wildcard": { "name": "Think*" }
  }
}

三、复合查询

# bool 组合查询
{
  "query": {
    "bool": {
      "must": [
        { "match": { "description": "笔记本" } }
      ],
      "filter": [
        { "range": { "price": { "gte": 5000, "lte": 10000 } } },
        { "term": { "category": "笔记本" } }
      ],
      "should": [
        { "match": { "description": "轻薄" } },
        { "match": { "description": "高性能" } }
      ],
      "must_not": [
        { "term": { "status": "discontinued" } }
      ],
      "minimum_should_match": 1
    }
  }
}

bool 查询要点:must 和 filter 都是必须满足的条件,区别是 filter 不计算相关性得分且可以缓存;should 是加分项;must_not 是排除条件。

四、聚合分析

# 按类别分组统计
{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": { "field": "category", "size": 10 },
      "aggs": {
        "avg_price": { "avg": { "field": "price" } },
        "max_price": { "max": { "field": "price" } },
        "min_price": { "min": { "field": "price" } }
      }
    }
  }
}

# 价格区间分布
{
  "size": 0,
  "aggs": {
    "price_ranges": {
      "range": {
        "field": "price",
        "ranges": [
          { "to": 3000 },
          { "from": 3000, "to": 6000 },
          { "from": 6000, "to": 10000 },
          { "from": 10000 }
        ]
      }
    }
  }
}

# 日期直方图
{
  "size": 0,
  "aggs": {
    "sales_by_month": {
      "date_histogram": {
        "field": "created_at",
        "calendar_interval": "month"
      },
      "aggs": {
        "total_revenue": { "sum": { "field": "price" } }
      }
    }
  }
}

五、搜索高亮

{
  "query": {
    "match": { "description": "笔记本电脑" }
  },
  "highlight": {
    "pre_tags": [""],
    "post_tags": [""],
    "fields": {
      "description": {
        "fragment_size": 150,
        "number_of_fragments": 3
      }
    }
  }
}

六、排序和分页

# 排序
{
  "query": { "match_all": {} },
  "sort": [
    { "price": "desc" },
    { "_score": "desc" }
  ],
  "from": 0,
  "size": 10
}

# search_after 深度分页(推荐)
{
  "query": { "match_all": {} },
  "sort": [{ "price": "desc" }, { "_id": "asc" }],
  "size": 10,
  "search_after": [5999.00, "doc_id_123"]
}

七、搜索建议(Suggest)

{
  "suggest": {
    "name_suggest": {
      "prefix": "Think",
      "completion": {
        "field": "name_suggest",
        "size": 5,
        "skip_duplicates": true
      }
    }
  }
}

八、实用查询模板

# 电商搜索模板:关键词搜索 + 分类过滤 + 价格区间 + 排序
{
  "query": {
    "bool": {
      "must": [
        { "multi_match": { "query": "笔记本电脑", "fields": ["name^3", "description"] } }
      ],
      "filter": [
        { "term": { "category": "笔记本" } },
        { "range": { "price": { "gte": 5000, "lte": 15000 } } },
        { "term": { "in_stock": true } }
      ]
    }
  },
  "sort": [{ "_score": "desc" }, { "sales_count": "desc" }],
  "highlight": { "fields": { "name": {}, "description": {} } },
  "aggs": {
    "brands": { "terms": { "field": "brand", "size": 20 } },
    "price_stats": { "stats": { "field": "price" } }
  },
  "from": 0,
  "size": 20
}

总结

Elasticsearch 查询 DSL 功能丰富,从简单的全文搜索到复杂的聚合分析都能胜任。建议从 match 和 bool 查询开始,逐步掌握聚合和高级特性。更多优化技巧请参考 Elasticsearch 性能优化教程。选购搬瓦工 VPS 请查看 全部方案,使用优惠码 NODESEEK2026 享受 6.77% 折扣,通过 bwh81.net 进入官网。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。