Elasticsearch 提供了多种搜索方式以满足不同场景下的查询需求。以下是一些常用的搜索类型及其实战演示,使用 curl
命令与 Elasticsearch 服务器交互(假设服务器运行在 http://localhost:9200
)。
1. 全文搜索(Full-text search)
Match Query
使用 match
查询在一个或多个字段中搜索与指定词语或短语相匹配的文档。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"content": "Elasticsearch tutorial"
}
}
}'
Multi-match Query
multi_match
查询可以在多个字段上执行相同的标准全文查询。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"multi_match": {
"query": "Elasticsearch introduction",
"fields": ["title", "description", "tags"]
}
}
}'
2. 精确匹配(Exact match)
Term Query
term
查询用于查找字段中精确值匹配的文档。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"status": "published"
}
}
}'
Terms Query
terms
查询允许一次性指定多个精确值,查找与这些值之一匹配的文档。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"terms": {
"category": ["electronics", "books", "clothing"]
}
}
}'
3. 范围查询(Range queries)
Range Query
range
查询用于查找字段值在指定区间内的文档。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"range": {
"price": {
"gte": 50,
"lte": 100
}
}
}
}'
4. 模糊搜索与通配符查询
Fuzzy Query
fuzzy
查询允许对某个词进行模糊匹配,容忍拼写错误或其他微小差异。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"fuzzy": {
"name": {
"value": "elastisearch",
"fuzziness": "AUTO"
}
}
}
}'
Wildcard Query
wildcard
查询支持使用通配符(*
和 ?
)进行匹配。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"wildcard": {
"product_name": "elec*"
}
}
}'
5. 组合查询(Compound queries)
Bool Query
bool
查询用于组合多个条件,支持 must
(必须满足)、should
(至少一个满足)、must_not
(必须不满足)子句。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "category": "electronics" } },
{ "range": { "price": { "gte": 100 } } }
],
"must_not": { "term": { "brand": "outdated_brand" } }
}
}
}'
6. 地理空间搜索
Geo-distance Query
查找距离某个地理位置一定范围内的文档。
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"geo_distance": {
"distance": "10km",
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
}
}'
7. 深度分页与滚动搜索
对于大量数据的分页查询,可以使用 from
和 size
参数进行常规分页,或者使用 scroll
API 进行滚动搜索以避免深度分页的性能问题。
Regular Pagination
curl -X GET "http://localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
"from": 50,
"size": 20,
"query": {
"match_all": {}
}
}'
Scroll Search
# 初始化滚动上下文
curl -X GET "http://localhost:9200/my_index/_search?scroll=1m" -H 'Content-Type: application/json' -d'
{
"size": 100,
"query": {
"match_all": {}
}
}'
# 使用返回的 `_scroll_id` 和 `scroll` 参数继续获取下一批数据
curl -X GET "http://localhost:9200/_search/scroll?scroll=1m&scroll_id=<scroll_id>"
以上只是 Elasticsearch 支持的多种搜索方式的一部分示例。实际应用中,可以根据具体业务需求灵活组合使用这些查询类型,并结合其他高级特性如排序、高亮、聚合等,以构建复杂的搜索和数据分析请求。同样的查询操作也可以通过 Kibana Dev Tools Console 或官方提供的各种语言客户端进行。