[toc]
Search API overview
1. There are two types of Search API
- URI Search: Using query parameters in URIs
Request Body Search: Use ES to provide a more complete DSL based on JSON format:
Query Domain Specific Language Query Domain Specific Language
2. Specify the index for the query
grammar | scope |
---|---|
/_search | all indexes on the cluster |
/index1/_search | query on index1 |
/index1,index2/_search | index1 and index2 |
/index*/_search | Wildcard matching: query on the index related to the index name starting with index |
3. Query data preparation: List of data in users
index in ES
_index | _type | _id | ▲_score | name | age | gender | birth |
---|---|---|---|---|---|---|---|
users | _doc | 1 | 1 | niewj | 36 | male | 1985-01-01 |
users | _doc | 3 | 1 | lifubo | 33 | male | |
users | _doc | 4 | 1 | weibinbin | 32 | male | |
users | _doc | 2 | 1 | nie | 26 | male | 1995-02-02 |
4. URI query
4.1 curl method on the command line
Use "q", to specify a query string; "query string syntax", KV key-value pairs:
curl -XGET --user username:password "http://localhost:9200/users/_search?q=name:nie"
result:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.87546873,
"hits": [
{
"_index": "users",
"_type": "_doc",
"_id": "2",
"_score": 0.87546873,
"_source": {
"name": "nie",
"age": 26,
"gender": "male",
"birth": "1995-02-02"
}
}
]
}
}
4.2 In kibana DevTools:
GET /users/_search?q=name:nie
result:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.87546873,
"hits" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.87546873,
"_source" : {
"name" : "nie",
"age" : 26,
"gender" : "male",
"birth" : "1995-02-02"
}
}
]
}
}
5. Request Body Search
Support GET
, POST
5.1 curl method on the command line
curl -XGET --user username:password "http://localhost:9200/users/_search" -H 'Content-Type:application/json' -d '{"query":{"match":{"name":"nie"}}}'
- username: replace with your own username
- password: replace with your own password
result:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.87546873,
"hits": [
{
"_index": "users",
"_type": "_doc",
"_id": "2",
"_score": 0.87546873,
"_source": {
"name": "nie",
"age": 26,
"gender": "male",
"birth": "1995-02-02"
}
}
]
}
}
5.2 In kibana DevTools
GET /users/_search
{
"query": {
"match": {
"name": "nie"
}
}
}
result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.87546873,
"hits" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.87546873,
"_source" : {
"name" : "nie",
"age" : 26,
"gender" : "male",
"birth" : "1995-02-02"
}
}
]
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。