Elasticsearch基础查询 #

一、查询概述 #

1.1 查询结构 #

json
GET /products/_search
{
  "query": {
    <query_type>: {
      <field>: <value>
    }
  }
}

1.2 查询类型 #

text
查询类型
├── 全文查询
│   ├── match
│   ├── match_phrase
│   └── multi_match
├── 精确查询
│   ├── term
│   ├── terms
│   └── range
├── 复合查询
│   ├── bool
│   ├── boosting
│   └── constant_score
└── 特殊查询
    ├── exists
    ├── ids
    └── wildcard

1.3 查询上下文与过滤上下文 #

上下文 特点 使用场景
查询上下文 计算相关性得分 全文搜索
过滤上下文 不计算得分,结果可缓存 精确过滤
json
{
  "query": {
    "bool": {
      "must": { "match": { "title": "search" } },
      "filter": { "term": { "status": "published" } }
    }
  }
}

二、match查询 #

2.1 基本match查询 #

bash
GET /products/_search
{
  "query": {
    "match": {
      "name": "iPhone 15"
    }
  }
}

2.2 match查询参数 #

参数 说明 默认值
operator 操作符(or/and) or
minimum_should_match 最小匹配数 -
analyzer 分析器 字段分析器
fuzziness 模糊度 -
prefix_length 前缀长度 0
max_expansions 最大扩展数 50

2.3 operator参数 #

bash
GET /products/_search
{
  "query": {
    "match": {
      "name": {
        "query": "iPhone 15",
        "operator": "and"
      }
    }
  }
}

2.4 minimum_should_match #

bash
GET /products/_search
{
  "query": {
    "match": {
      "description": {
        "query": "quick brown fox jumps",
        "minimum_should_match": "75%"
      }
    }
  }
}

2.5 模糊匹配 #

bash
GET /products/_search
{
  "query": {
    "match": {
      "name": {
        "query": "iPhne",
        "fuzziness": "AUTO"
      }
    }
  }
}

三、match_phrase查询 #

3.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "match_phrase": {
      "description": "quick brown fox"
    }
  }
}

3.2 slop参数 #

bash
GET /products/_search
{
  "query": {
    "match_phrase": {
      "description": {
        "query": "quick fox",
        "slop": 2
      }
    }
  }
}

slop:允许中间间隔的词数。

3.3 match_phrase_prefix #

bash
GET /products/_search
{
  "query": {
    "match_phrase_prefix": {
      "name": {
        "query": "iPhone 15 P",
        "max_expansions": 10
      }
    }
  }
}

四、multi_match查询 #

4.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iPhone",
      "fields": ["name", "description", "brand"]
    }
  }
}

4.2 字段权重 #

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iPhone",
      "fields": ["name^3", "description", "brand^2"]
    }
  }
}

^3 表示权重提升3倍。

4.3 查询类型 #

类型 说明
best_fields 取最高分字段(默认)
most_fields 所有字段得分求和
cross_fields 跨字段匹配
phrase 短语匹配
phrase_prefix 短语前缀匹配
bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "quick brown fox",
      "type": "best_fields",
      "fields": ["title", "content"],
      "tie_breaker": 0.3
    }
  }
}

五、term查询 #

5.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "term": {
      "brand": "Apple"
    }
  }
}

5.2 term查询参数 #

参数 说明
value 查询值
boost 权重提升
case_insensitive 是否忽略大小写

5.3 避免对text字段使用term #

bash
GET /products/_search
{
  "query": {
    "term": {
      "name.keyword": "iPhone 15"
    }
  }
}

建议:text字段使用match查询,keyword字段使用term查询。

六、terms查询 #

6.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "terms": {
      "brand": ["Apple", "Samsung", "Google"]
    }
  }
}

6.2 terms查询参数 #

bash
GET /products/_search
{
  "query": {
    "terms": {
      "brand": {
        "index": "brands",
        "id": "popular_brands",
        "path": "brands"
      }
    }
  }
}

从另一个文档获取terms值。

6.3 minimum_should_match #

bash
GET /products/_search
{
  "query": {
    "terms": {
      "tags": ["tag1", "tag2", "tag3", "tag4"],
      "minimum_should_match": 2
    }
  }
}

七、range查询 #

7.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 500,
        "lte": 1000
      }
    }
  }
}

7.2 范围操作符 #

操作符 说明
gt 大于
gte 大于等于
lt 小于
lte 小于等于

7.3 日期范围查询 #

bash
GET /products/_search
{
  "query": {
    "range": {
      "created_at": {
        "gte": "2024-01-01",
        "lte": "2024-12-31",
        "format": "yyyy-MM-dd"
      }
    }
  }
}

7.4 相对日期 #

bash
GET /logs/_search
{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "now-7d",
        "lte": "now"
      }
    }
  }
}

日期数学表达式

表达式 说明
now 当前时间
now-1h 1小时前
now-1d 1天前
now-1w 1周前
now-1M 1月前
now-1y 1年前
now/d 当天开始

7.5 时间舍入 #

bash
GET /logs/_search
{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "now/d",
        "lt": "now+1d/d"
      }
    }
  }
}

八、exists查询 #

8.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "exists": {
      "field": "description"
    }
  }
}

8.2 查找缺失字段 #

bash
GET /products/_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "description"
        }
      }
    }
  }
}

九、ids查询 #

bash
GET /products/_search
{
  "query": {
    "ids": {
      "values": ["1", "2", "3"]
    }
  }
}

十、prefix查询 #

bash
GET /products/_search
{
  "query": {
    "prefix": {
      "name": {
        "value": "iPh"
      }
    }
  }
}

十一、wildcard查询 #

11.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "i*15"
      }
    }
  }
}

11.2 通配符说明 #

通配符 说明
* 匹配任意字符(包括空)
? 匹配单个字符

注意:wildcard查询性能较差,慎用。

十二、regexp查询 #

bash
GET /products/_search
{
  "query": {
    "regexp": {
      "name": "i[A-Z].*"
    }
  }
}

十三、fuzzy查询 #

bash
GET /products/_search
{
  "query": {
    "fuzzy": {
      "name": {
        "value": "iPhne",
        "fuzziness": "AUTO",
        "prefix_length": 2,
        "max_expansions": 50
      }
    }
  }
}

十四、查询结果 #

14.1 响应结构 #

json
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 100,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "products",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "name": "iPhone 15",
          "price": 999
        }
      }
    ]
  }
}

14.2 分页参数 #

bash
GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 10
}

14.3 字段过滤 #

bash
GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name", "price"]
}

14.4 排序 #

bash
GET /products/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    { "price": "desc" },
    { "created_at": "asc" }
  ]
}

十五、查询性能优化 #

15.1 使用filter代替query #

bash
GET /products/_search
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "brand": "Apple" } },
        { "range": { "price": { "lte": 1000 } } }
      ]
    }
  }
}

15.2 避免深度分页 #

bash
GET /products/_search
{
  "query": { "match_all": {} },
  "from": 10000,
  "size": 10
}

问题:from + size 不能超过 index.max_result_window(默认10000)。

解决方案:使用 search_after。

15.3 使用路由 #

bash
GET /products/_search?routing=apple
{
  "query": {
    "match": { "name": "iPhone" }
  }
}

十六、总结 #

本章介绍了Elasticsearch基础查询:

  1. match查询用于全文搜索
  2. term查询用于精确匹配
  3. range查询用于范围过滤
  4. 查询上下文计算得分,过滤上下文不计算
  5. 合理使用参数优化查询性能
  6. 避免深度分页问题

下一步,我们将学习复合查询。

最后更新:2026-03-27