写点什么

Elasticsearch Search API 基础语法

用户头像
escray
关注
发布于: 2021 年 02 月 18 日
Elasticsearch Search API 基础语法

Elasticsearch Search API 基础语法,文字内容来自 B 站中华石杉 Elasticsearch 顶尖高手系列课程核心知识篇,英文内容来自官方文档。

Search your data


A search query, or query, is a request for information about data in Elasticsearch data streams or indices.


A search consists of one or more queries that are combined and sent to Elasticsearch.

Search API 的基本语法


Search API: Returns search hits that match the query defined in the request.


Allows you to execute a search query and get back search hits that match the query.


Request


GET /<target>/_searchGET /_searchPOST /<target>/_searchPOST /_search
复制代码


GET /my-index-000001/_search{  "query": {    "term": {	"user.id": "kimchy"    }  }}
复制代码


挑了几个自己觉的重要的参数,抄了一下官方文档,挂一漏万。


Path parameters


<target> (Optional, string) Comma-separated list of data streams, indices, and index alias to search. Wildcard(*) expressions are support.


Query parameter


from: (Optional, integer) Starting document offset. Defaults to 0.


size: (Optional, integer) Defines the number of hits to return. Defaults to 10.


By default, you cannot page through more than 10,000 hits using the from and size parameters.


sort: (Optional, string) A comma-separated list of <field>:<direction> pairs.


source:


Request body


Response body


took: (integer) Milliseconds it took Elasticsearch to execute the request. This value is calculated by measuring the time elapsed between receipt of a request on the coordinating node and the time at which the coordinating node is ready to send the response.


Took time includes:


  • Communication time between the coordinating node and data nodes

  • Time the request spends in the search thread pool, queued for execution

  • Actual execution time


Took time does not include:


  • Time needed to send the request to Elasticsearch

  • Time needed to serialize the JSON response

  • Time needed to send the response to a client


time_out: (Boolean) If true, the request timed out before completion; returned results may be partial or empty.


hits: (object) Contains returned documents and metadata.


GET /_search
GET /index1,index2/_search
GET /_search{  "from": 0,  "size": 10}
复制代码
GET or POST


http 协议中 get 是否可以带上 request body ?


HTTP 协议,一般不允许 get 请求带上 request body,但是因为 get 更加适合描述查询数据的操作,因此还是这么用了


GET /_search?from=0&size=10
POST /_search{  "from":0,  "size":10}
复制代码


碰巧,很多浏览器,或者是服务器,也都支持 GET+request body 模式


如果遇到不支持的场景,也可以用 POST /_search


发布于: 2021 年 02 月 18 日阅读数: 27
用户头像

escray

关注

Let's Go 2017.11.19 加入

在学 Elasticsearch 的项目经理

评论

发布
暂无评论
Elasticsearch Search API 基础语法