📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

在索引中执行全文搜索、结构化查询、聚合等操作。

API #

GET /_search
POST /_search
GET /{index}/_search
POST /{index}/_search
GET /_all/_search
POST /_all/_search

API 的作用 #

该 API 是 Easysearch 的核心搜索接口,支持:

  • 全文搜索
  • 结构化查询
  • 复杂布尔查询
  • 聚合分析
  • 排序和分页
  • 高亮显示
  • 建议功能
  • 多索引搜索

API 的参数 #

路由参数 #

参数类型是否必填描述
{index}字符串索引名称。支持:单个索引、逗号分隔的多个索引、通配符表达式、_all(所有索引)

Query String 参数 #

搜索控制参数 #

参数类型是否必填默认值描述
q字符串-查询字符串(Lucene 查询语法)
from整数0从第几条结果开始
size整数10返回结果数量
timeout时间值-搜索超时时间
terminate_after整数0匹配指定数量后终止
scroll时间值-创建滚动上下文的时间
search_type字符串query_then_fetch搜索类型
request_cache布尔值-是否使用请求缓存

结果控制参数 #

参数类型是否必填默认值描述
explain布尔值false是否返回评分解释
version布尔值false是否返回文档版本号
seq_no_primary_term布尔值false是否返回序列号和主分片信息
track_scores布尔值false是否返回评分
track_total_hits布尔值/整数true是否跟踪总命中数
min_score浮点数-最小分数阈值

字段控制参数 #

参数类型是否必填默认值描述
_source布尔值/字符串true控制返回的源字段
_source_excludes字符串-排除的源字段
_source_includes字符串-包含的源字段
stored_fields字符串-返回的存储字段
docvalue_fields字符串-返回的 docvalue 字段
fields字符串-返回的字段

排序参数 #

参数类型是否必填默认值描述
sort字符串_score排序字段,格式:field:ascfield:desc

其他参数 #

参数类型是否必填默认值描述
routing字符串-路由值
preference字符串-分片偏好:_local_primary
allow_partial_search_results布尔值-是否允许部分结果
batched_reduce_size整数512批量减少大小
max_concurrent_shard_requests整数-最大并发分片请求数
pre_filter_shard_size整数128预过滤分片大小
ccs_minimize_roundtrips布尔值true最小化跨集群搜索往返

请求体参数(POST) #

{
  "query": { ... },
  "aggs": { ... },
  "sort": [ ... ],
  "from": 0,
  "size": 10,
  "highlight": { ... },
  "_source": [ ... ],
  "script_fields": { ... }
}

示例 #

基本搜索(匹配所有) #

GET /my_index/_search
{
  "query": {
    "match_all": {}
  }
}

响应示例:

{
  "took": 5,
  "timed_out": false,
  "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
  "hits": {
    "total": {
      "value": 100,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "my_index",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": { "field": "value" }
      }
    ]
  }
}

全文搜索 #

GET /my_index/_search
{
  "query": {
    "match": {
      "title": "search engine"
    }
  }
}

使用查询字符串 #

GET /my_index/_search?q=title:easysearch

分页 #

GET /my_index/_search
{
  "from": 10,
  "size": 10,
  "query": {
    "match_all": {}
  }
}

排序 #

GET /my_index/_search
{
  "sort": [
    { "date": "desc" },
    { "_score": "desc" }
  ],
  "query": {
    "match_all": {}
  }
}

字段过滤 #

GET /my_index/_search
{
  "_source": ["title", "author"],
  "query": {
    "match_all": {}
  }
}

布尔查询 #

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "search" } }
      ],
      "filter": [
        { "term": { "status": "published" } }
      ],
      "must_not": [
        { "range": { "views": { "lte": 0 } } }
      ]
    }
  }
}

聚合查询 #

GET /sales/_search
{
  "size": 0,
  "aggs": {
    "by_category": {
      "terms": {
        "field": "category.keyword"
      },
      "aggs": {
        "total_revenue": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

高亮显示 #

GET /my_index/_search
{
  "query": {
    "match": {
      "content": "easysearch"
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}

多索引搜索 #

GET /index1,index2/_search
{
  "query": {
    "match_all": {}
  }
}

使用通配符 #

GET /logs-*/_search
{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "now-1d"
      }
    }
  }
}

滚动搜索 #

GET /my_index/_search?scroll=1m
{
  "size": 100,
  "query": {
    "match_all": {}
  }
}

范围查询 #

GET /products/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 10,
        "lte": 100
      }
    }
  }
}

过滤结果 #

GET /my_index/_search
{
  "post_filter": {
    "term": {
      "status": "active"
    }
  },
  "query": {
    "match_all": {}
  }
}

脚本字段 #

GET /my_index/_search
{
  "script_fields": {
    "discounted_price": {
      "script": {
        "source": "doc['price'] * params.discount",
        "params": {
          "discount": 0.9
        }
      }
    }
  }
}

常见查询类型 #

查询类型示例
match_all匹配所有文档
match全文搜索
term精确匹配
terms多值精确匹配
range范围查询
bool布尔组合查询
wildcard通配符查询
prefix前缀查询
exists字段存在查询
fuzzy模糊查询

使用场景 #

场景 1:日志搜索 #

GET /logs-*/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "message": "error" } }
      ],
      "filter": [
        { "range": { "@timestamp": { "gte": "now-1h" } } }
      ]
    }
  },
  "sort": [
    { "@timestamp": "desc" }
  ]
}

场景 2:电商产品搜索 #

GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "laptop",
      "fields": ["name", "description", "brand"]
    }
  },
  "post_filter": {
    "term": { "in_stock": true }
  },
  "sort": [
    { "popularity": "desc" }
  ],
  "aggs": {
    "by_brand": {
      "terms": { "field": "brand.keyword" }
    }
  }
}

场景 3:数据分析 #

GET /metrics/_search
{
  "size": 0,
  "query": {
    "range": {
      "@timestamp": {
        "gte": "now-24h"
      }
    }
  },
  "aggs": {
    "hourly": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "hour"
      },
      "aggs": {
        "avg_value": {
          "avg": { "field": "value" }
        }
      }
    }
  }
}

注意事项 #

  1. GET vs POST:复杂查询建议使用 POST 方法
  2. from + size 限制:深分页性能差,建议使用 search_after
  3. 通配符搜索* 开头的通配符查询性能较差
  4. 聚合大小:大量聚合可能占用大量内存
  5. 请求缓存:相同结构的请求会被缓存,修改参数时要小心