[TOC]
## **測試數據準備:官方樣例數據accounts.json**
在開始探索之前,我們先下載官方提供的樣例數據集,導入到我們的集群中。
官方樣例數據集下載地址:[右鍵下載](https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true)
百度云盤鏈接: https://pan.baidu.com/s/15wtt3olKf06KxugXSqMq2w 提取碼: vse4
將下載的accounts.json 上傳到當前ES服務器目錄中,執行以下命令
```
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
```
執行成功后,查詢索引
```
root@ubuntu:/home/guanfuchang# curl -X GET "localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open bank TadPjNB3T9G4qDm5j8SVwA 5 1 1000 0 506.1kb 506.1kb
yellow open customer H4FuIykjRIeAbPij8yLRug 5 1 4 0 14.2kb 14.2kb
```
發現已經新增了索引`bank`,其中有1000個document。
## **搜索API `_search `**
我們有兩種方式進行搜索:
- 在請求URL中傳參
```
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"
```
- 在請求BODY中傳參
```
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
'
```
通常,我們會選擇在BODY中使用JSON格式進行傳參。
上面兩種方式,查詢的結果是一樣的。**查詢關鍵字**為*,代表所有值。**排序**是根據account_number升序,**默認是返回10條數據**。
返回格式如下:

### **查詢語句介紹**
#### **基本參數選項:query、from、size、sort、_source**
```
GET /bank/_search
{
"query": {"match_all": {}},
"from": 10,
"size": 2,
"sort": [
{"balance": { "order": "desc" } }
],
"_source":["account_number","balance"]
}
```
**query**:指定查詢條件,這里使用` { "match_all": {} }`表示查詢條件匹配所有記錄
**from**:表示從第n條匹配記錄開始取值,默認為0
**size**:表示匹配條數,默認10
**sort**:表示排序,這里使用`{ "balance": { "order": "desc" }}`,表示按balance降序排序,這里也可以寫成`[{ "balance": "desc" }]`
**_source**:表示查詢字段,這里使用` ["account_number", "balance"]
`表示返回結果中,只需要返回"account_number", "balance"兩個字段即可。默認返回所有字段。
上面的查詢結果如下:

#### **查詢匹配條件**
上面例子中,我們在選項query中,使用了` { "match_all": {} }`表示查詢條件匹配所有記錄,下面以一系列的例子介紹各種匹配條件
##### **match查詢**:查詢 account_number=20 的document
```
GET /bank/_search
{
"query": {
"match": {"account_number": 20}
}
}
```
查詢結果如下:

##### **match**查詢:查詢 address 中包含 “mill” 的document
```
GET /bank/_search
{
"query": {
"match": {"address": "mill"}
}
}
```
##### **match**查詢:查詢 address 中包含 “mill” 或者 “lane” 的document
```
GET /bank/_search
{
"query": {
"match": {"address": "mill lan"}
}
}
```
##### **match_phrase**查詢:查詢 address 中包含短語 “mill lane” 的document
```
GET /bank/_search
{
"query": {
"match_phrase": {"address": "mill lane"}
}
}
```
##### **bool and**關系查詢:查詢 address 中同時包含 “mill” 和 “lane” 的document
```
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
```
##### **bool or** 關系查詢:查詢 address 中包含 “mill” 或者 “lane” 的document
```
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
```
##### **bool not**關系查詢:查詢 address 中即不存在 “mill” 也不存在 “lane” 的document
```
GET /bank/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
```
##### **bool 組合**查詢:查詢 age=40,state!="ID" 的document
```
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": 40 } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
```
##### **bool filter**查詢:查詢 20000<=balance<=30000 的document
```
GET /bank/_search
{
"query": {
"bool": {
"filter":{
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
```