一、es特性
二、使用命令
查看集羣健康狀況
curl 127.0.0.1:9200/_cat/health?v
列出所有node節點
curl 127.0.0.1:9200/_cat/nodes?v
查看索引信息
curl 127.0.0.1:9200/_cat/indices?v
創建索引
curl -X PUT 127.0.0.1:9200/customer?pretty
添加文檔
curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
檢索索引的文檔
curl -X GET "localhost:9200/customer/_doc/1?pretty&pretty"
刪除索引
curl -X DELETE "localhost:9200/customer?pretty"
更新文檔
curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}'
索引一個新文檔
curl -X PUT "localhost:9200/customer/_doc/2?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}'
索引一個沒有顯式id的文檔
curl -X POST "localhost:9200/customer/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}'
更新文檔
curl -X POST "localhost:9200/customer/_update/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe" }
}'
更新文檔並添加字段
curl -X POST "localhost:9200/customer/_update/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}'
使用腳本執行更新年齡到25歲
curl -X POST "localhost:9200/customer/_update/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}'
查看文檔
curl -X GET "localhost:9200/customer/_doc/2?pretty"
刪除文檔
curl -X DELETE "localhost:9200/customer/_doc/2?pretty&pretty"
批量創建文檔
curl -X POST "localhost:9200/customer/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }'
更新第一個文檔並刪除第二個文檔
curl -X POST "localhost:9200/customer/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}'
搜索文檔 "*"參數匹配索引中的所有文檔, account_number按照文檔的這個字段升序排列
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty"
注意命中的準確性由請求參數track_total_hits控制,當設置爲true時,請求將精確地跟蹤總命中(“relationship”:“eq”)。默認值爲10,000,這意味着總命中數可以精確地跟蹤到10,000個文檔。通過顯式地將track_total_hits設置爲true,可以強制進行準確的計數。有關詳細信息,請參閱請求主體文檔
同一效果一樣,用json風格查詢請求體
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}'
三、DSL查詢語言
1.
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} }
}
'
2.
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"size": 1
}
'
size: 返回文檔數
3.返回第10到第19個文檔
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}
'
from:從哪個索引文檔開始,默認爲0
4. 降序排列並返回前10個文檔
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
'
5.默認返回搜索文檔的所有字段,可以指定返回字段
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
'
只返回["account_number", "balance"]字段
6.match匹配查詢
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "account_number": 20 } }
}
'
只返回account_number是20的文檔
7.匹配address包含"mill"的文檔,不區分大小寫
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "address": "mill" } }
}
'
8.返回address 包含"mill" 或者"lane"的文檔
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "address": "mill lane" } }
}
'
9.match的變體,返回address包含"mill lane"字段的文檔
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_phrase": { "address": "mill lane" } }
}
'
10.bool查詢返回匹配到{ "match": { "address": "mill" } },和{ "match": { "address": "lane" } }的文檔,must指定必須同時滿足
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'
11.bool或查詢滿足{ "match": { "address": "mill" } },或{ "match": { "address": "lane" } }的文檔,should指定滿足其中一個條件即可
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
12,bool反向查詢 查詢既不滿足{ "match": { "address": "mill" } },也不滿足{ "match": { "address": "lane" } }的文檔,must_not跟mast相反
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'
13.bool聯合查詢,must,must_not同時使用
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
'
_score字段值越大,查詢的文檔越相關
四、filter過濾
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
五、聚合查詢
1.按狀態對所有賬戶分組,然後返回按count降序排列的前10個(默認)狀態(也是默認)
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}
'
相當於sql語句:
SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC LIMIT 10;
注意:將size=0設置爲不顯示搜索結果,因爲我們只想看到響應中的聚合結果,group_by_state以state字段聚合
2.聚合求平均值
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
'
本示例按狀態計算平均帳戶餘額(同樣只針對按count降序排列的前10個狀態)
3.平均值降序聚合
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
'
4.多個聚合一起使用
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}
'
這個例子展示了我們如何按照年齡等級(20-29歲,30-39歲,40-49歲)分組,然後按性別分組,最後得到每個年齡等級,每個性別的平均賬戶餘額