1、Search APIs
Most search APIs are multi-index, with the exception of the Explain API endpoints.
1.1、 Search
Returns search hits that match the query defined in the request.
- Request
GET /<index>/_search
GET /_search
POST /<index>/_search
POST /_search
-
Example
-
query parameter
curl -XGET 'http://10.143.228.25:9200/student/_search?q=age:20&pretty'
# 查询指定返回字段
curl -XGET 'http://10.143.228.25:9200/student/_search?_source=name,age&pretty'
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "_doc",
"_id" : "oFmkQHIBrm6ikcYVWSpb",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "门捷列夫",
"age" : 20,
"address" : "俄罗斯",
"birthday" : "1990-02-03"
}
},
{
"_index" : "student",
"_type" : "_doc",
"_id" : "oVmkQHIBrm6ikcYVXCpE",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "门捷列夫",
"age" : 20,
"address" : "俄罗斯",
"birthday" : "1990-02-03"
}
}
]
}
}
- request body
curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{
"query" : {
"term" : { "age" : "20" }
}
}
'
response示例:
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "student",
"_type" : "_doc",
"_id" : "oFmkQHIBrm6ikcYVWSpb",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "门捷列夫",
"age" : 20,
"address" : "俄罗斯",
"birthday" : "1990-02-03"
}
},
{
"_index" : "student",
"_type" : "_doc",
"_id" : "oVmkQHIBrm6ikcYVXCpE",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "门捷列夫",
"age" : 20,
"address" : "俄罗斯",
"birthday" : "1990-02-03"
}
}
]
}
}
Doc Value Fields
curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{
"query" : {
"term" : { "age" : "20" }
},
"_source":["name","age"]
}'
From / Size
curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{
"from" : 0, "size" : 10,
"query" : {
"term" : { "age" : "20" }
},
"_source":["name","age"]
}'
Sort
curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{
"from" : 0, "size" : 10,
"query" : {
"term" : { "age" : "20" }
},
"_source":["name","age"],
"sort":[{"age":"asc"}]
}'
1.2、Request Body Search
Specifies search criteria as request body parameters.
- Request
GET /<index>/_search
{
"query": {<parameters>}
}
- Example
curl -XGET 'http://10.143.228.25:9200/student/_doc/1?pretty'
response示例:
{
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"id" : 1,
"name" : "门捷列夫",
"age" : 20,
"address" : "俄罗斯",
"birthday" : "1990-02-03"
}
}
- 返回指定字段
curl -XGET 'http://10.143.228.25:9200/student/_doc/1?pretty&_source=name,age'
response示例:
{
"_index" : "student",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "门捷列夫",
"age" : 20
}
}
1.3、Count API
Gets the number of matches for a search query.
- Request
GET /<index>/_count
- Example
1、基于URI形式
curl -XGET 'http://10.143.228.25:9200/student/_count?q=age:20&pretty'
response示例:
{
"count" : 4,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
}
}
2、基于request body形式
curl -XGET 'http://10.143.228.25:9200/student/_count?pretty' -H 'Content-Type: application/json' -d'
{
"query" : {
"term" : { "age" : 20 }
}
}'
response示例:
{
"count" : 4,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
}
}
- 使用bool(must、must_not、should)
filter
可以实现对结果的过滤
curl -X GET "10.143.228.25:9200/student/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "age": 20 } }
],
"must_not": [
{ "match": { "address": "美国" } }
],
"should": [
{ "match": { "age": 20 } }
],
"filter": {
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
}
}
}
'
>更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-count.html
## 1.4、Search Shards API
>Returns the indices and shards that a search request would be executed against.
- Request
```shell
GET /<index>/_search_shards
- Example
curl -XGET 'http://10.143.228.25:9200/student/_search_shards?pretty'
response示例:
{
"nodes" : {
"fkdv_0G4SCW5YLqxkWL1qg" : {
"name" : "node2",
"ephemeral_id" : "MBLiX3TKRJihyd7aiGLjnA",
"transport_address" : "10.143.228.26:9300",
"attributes" : {
"ml.machine_memory" : "8350584832",
"ml.max_open_jobs" : "20",
"xpack.installed" : "true"
}
},
"E6pmMAUdShurP50uAV3kIA" : {
"name" : "node3",
"ephemeral_id" : "7eSlOGxnSw65qIEGktBaDQ",
"transport_address" : "10.143.228.198:9300",
"attributes" : {
"ml.machine_memory" : "8350584832",
"ml.max_open_jobs" : "20",
"xpack.installed" : "true"
}
},
"XiTKce8fST2vhReZvE-1dA" : {
"name" : "node1",
"ephemeral_id" : "rQYF0SGZRWmNe23mXyKWEw",
"transport_address" : "10.143.228.25:9300",
"attributes" : {
"ml.machine_memory" : "8350576640",
"xpack.installed" : "true",
"ml.max_open_jobs" : "20"
}
}
},
"indices" : {
"student" : { }
},
"shards" : [
[
{
"state" : "STARTED",
"primary" : false,
"node" : "E6pmMAUdShurP50uAV3kIA",
"relocating_node" : null,
"shard" : 0,
"index" : "student",
"allocation_id" : {
"id" : "3iTj8tTNSdiDjaUvBgb3gA"
}
},
{
"state" : "STARTED",
"primary" : false,
"node" : "XiTKce8fST2vhReZvE-1dA",
"relocating_node" : null,
"shard" : 0,
"index" : "student",
"allocation_id" : {
"id" : "rOYuBhe7ToqugfDwBifSEQ"
}
},
{
"state" : "STARTED",
"primary" : true,
"node" : "fkdv_0G4SCW5YLqxkWL1qg",
"relocating_node" : null,
"shard" : 0,
"index" : "student",
"allocation_id" : {
"id" : "9jSRbREKS9eCFDrEKrOeOQ"
}
}
],
[
{
"state" : "STARTED",
"primary" : false,
"node" : "fkdv_0G4SCW5YLqxkWL1qg",
"relocating_node" : null,
"shard" : 1,
"index" : "student",
"allocation_id" : {
"id" : "erg_YsbmQPWQFrXAm_icpQ"
}
},
{
"state" : "STARTED",
"primary" : true,
"node" : "E6pmMAUdShurP50uAV3kIA",
"relocating_node" : null,
"shard" : 1,
"index" : "student",
"allocation_id" : {
"id" : "Hvd9J2IcTmOPRESGtruAAQ"
}
},
{
"state" : "STARTED",
"primary" : false,
"node" : "XiTKce8fST2vhReZvE-1dA",
"relocating_node" : null,
"shard" : 1,
"index" : "student",
"allocation_id" : {
"id" : "Wgl0mc6zQmG4qIwvLJMS8Q"
}
}
],
[
{
"state" : "STARTED",
"primary" : false,
"node" : "fkdv_0G4SCW5YLqxkWL1qg",
"relocating_node" : null,
"shard" : 2,
"index" : "student",
"allocation_id" : {
"id" : "T9dZf0nATfCKlN0EbDca6w"
}
},
{
"state" : "STARTED",
"primary" : false,
"node" : "E6pmMAUdShurP50uAV3kIA",
"relocating_node" : null,
"shard" : 2,
"index" : "student",
"allocation_id" : {
"id" : "CRmJU1_2RBegt8mPZXQt-A"
}
},
{
"state" : "STARTED",
"primary" : true,
"node" : "XiTKce8fST2vhReZvE-1dA",
"relocating_node" : null,
"shard" : 2,
"index" : "student",
"allocation_id" : {
"id" : "mTGFJhjmS0yIbsioZRxnsg"
}
}
]
]
}
3、More
4、elasticsearch-header插件
elasticsearch-header插件就像mysql之navicat或者sqlYog,一个客户端插件,如上的一些curl命令可以通过界面操作,提高易用性。
安装步骤详见源码的readme即可。
部分截图:
下面的是我的公众号二维码图片,欢迎关注。