# Elasticsearch Search API怎么使用
## 1. 引言
Elasticsearch作為當前最流行的分布式搜索和分析引擎,其核心功能是通過Search API實現高效的數據檢索。本文將全面解析Elasticsearch Search API的使用方法,包括基礎查詢、復合查詢、分頁排序、高亮顯示等核心功能,并配合實際示例演示如何構建各種搜索場景。
---
## 2. Search API基礎
### 2.1 基本請求格式
Elasticsearch的搜索請求主要通過`_search`端點實現,支持GET和POST兩種HTTP方法:
```json
GET /{index}/_search
POST /{index}/_search
{
"query": { ... }
}
最簡單的搜索是不帶任何條件的空查詢,返回索引中的所有文檔(默認前10條):
GET /products/_search
響應結構說明:
{
"took": 5, // 查詢耗時(ms)
"timed_out": false, // 是否超時
"hits": {
"total": { // 匹配總數
"value": 100,
"relation": "eq"
},
"max_score": 1.0, // 最高得分
"hits": [ // 結果數組
{
"_index": "products",
"_id": "1",
"_score": 1.0,
"_source": { ... } // 原始文檔
}
]
}
}
GET /products/_search
{
"query": {
"match": {
"name": "智能手機"
}
}
}
精確匹配未經分析的字段:
{
"query": {
"term": {
"status": {
"value": "in_stock"
}
}
}
}
{
"query": {
"range": {
"price": {
"gte": 1000,
"lte": 5000
}
}
}
}
組合多個查詢條件的布爾邏輯:
{
"query": {
"bool": {
"must": [
{ "match": { "name": "手機" } }
],
"filter": [
{ "range": { "price": { "lte": 3000 } } },
{ "term": { "brand": "華為" } }
],
"must_not": [
{ "term": { "status": "out_of_stock" } }
]
}
}
}
{
"query": {
"boosting": {
"positive": { "match": { "name": "華為" } },
"negative": { "match": { "category": "配件" } },
"negative_boost": 0.5
}
}
}
{
"query": { "match_all": {} },
"from": 10, // 從第10條開始
"size": 5, // 返回5條結果
"sort": [
{ "price": "desc" },
"_score" // 默認按相關性排序
]
}
{
"query": {
"match": { "description": "高性能" }
},
"highlight": {
"fields": {
"description": {
"pre_tags": ["<em>"],
"post_tags": ["</em>"]
}
}
}
}
{
"size": 0,
"aggs": {
"price_stats": {
"stats": { "field": "price" }
},
"brands": {
"terms": { "field": "brand.keyword", "size": 5 }
}
}
}
filter
上下文(不計算得分)query
上下文{
"query": {
"bool": {
"must": { "match": { "name": "筆記本" } },
"filter": { "range": { "price": { "gte": 5000 } } }
}
}
}
減少網絡傳輸量:
{
"_source": ["name", "price"],
"query": { ... }
}
{
"query": {
"match": {
"name": {
"query": "智能手表",
"fuzziness": "AUTO"
}
}
}
}
GET /ecommerce/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "防水藍牙耳機",
"fields": ["name^3", "description"],
"operator": "and"
}
}
],
"filter": [
{ "term": { "category": "電子產品" } },
{ "range": { "price": { "lte": 999 } } },
{ "terms": { "brand": ["索尼", "Bose", "JBL"] } }
]
}
},
"highlight": {
"fields": {
"name": {},
"description": {}
}
},
"sort": [
{ "sales": "desc" },
{ "rating": "desc" }
],
"from": 0,
"size": 20,
"aggs": {
"price_histogram": {
"histogram": {
"field": "price",
"interval": 200
}
}
}
}
Q1: 如何實現拼音搜索? A: 需要安裝拼音分析插件,配置自定義分析器:
PUT /products
{
"settings": {
"analysis": {
"analyzer": {
"pinyin_analyzer": {
"tokenizer": "my_pinyin"
}
},
"tokenizer": {
"my_pinyin": {
"type": "pinyin",
"keep_first_letter": true
}
}
}
}
}
Q2: 搜索結果不穩定怎么辦?
A: 可能原因包括:
- 分片數據未刷新(嘗試?refresh=true
)
- 使用了相關性不穩定的查詢(如function_score
)
- 存在副本分片數據不一致
Elasticsearch Search API提供了強大而靈活的搜索能力,關鍵要點包括: - 熟練掌握Query DSL的語法結構 - 合理組合各種查詢條件和過濾器 - 通過聚合實現數據分析功能 - 使用高亮、排序等增強用戶體驗 - 注意性能優化和資源控制
通過本文的示例和實踐建議,開發者可以構建出高效、精準的搜索解決方案。 “`
注:本文實際約3100字(中文字符),包含了Elasticsearch Search API的核心使用方法和實踐建議。如需擴展特定部分或添加更多示例,可以進一步補充相關內容。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。