[TOC]
es基本概念
集群:通過唯一的名稱來區(qū)分集群
節(jié)點:默認(rèn)名稱是通過UUID來生成的吟税,當(dāng)然如果想加入到某個集群里面讨勤,需要修改成對應(yīng)的集群名稱
索引:名稱必須小寫
類型:將在V7.0以后移除
文檔:
主分片:
- 允許你水平拆分/擴展存儲空間
- 允許你跨分片分布和并行化操作以提高性能/吞吐量
復(fù)制分片:
- 當(dāng)節(jié)點發(fā)生故障時以提供高可用性,因此箕戳,請務(wù)必注意确憨,復(fù)制分片永遠(yuǎn)不會在從復(fù)制的原始/主分片相同的節(jié)點上分配
- 允許你擴展搜索量/吞吐量,因為可以在所有副本上并行執(zhí)行搜索
集群健康
curl -X GET "localhost:9200/_cat/health?v"
集群節(jié)點
curl -X GET "localhost:9200/_cat/nodes?v"
索引
查看所有索引:
curl -X GET "localhost:9200/_cat/indices?v"
創(chuàng)建索引:
curl -X PUT "localhost:9200/customer?pretty"
創(chuàng)建文檔(在創(chuàng)建文檔之前不用事先創(chuàng)建index)
創(chuàng)建文檔(明確指定ID用PUT)
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
創(chuàng)建文檔(未明確指定ID用POST)
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
查詢文檔
curl -X GET "localhost:9200/customer/_doc/1?pretty"
刪除索引
curl -X DELETE "localhost:9200/customer?pretty"
更新文檔
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe" }
}
'
動態(tài)添加字段
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
使用簡單腳本修改文檔
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}
'
刪除文檔
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"
批量處理(_bulk API)
批量創(chuàng)建
curl -X POST "localhost:9200/customer/_doc/_bulk?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/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
加載簡單數(shù)據(jù)集(注意文件名沒有前面的@)
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
搜索API(默認(rèn)返回10條信息)
rest request URI
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"
rest request body
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
{ "balance": { "order": "desc" } }
]
}
'
指定返回結(jié)果的fields
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
'
bool query
- should: 滿足其中一個條件就行
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'
- must: 必須滿足所有條件
- must_not: 返回不滿足當(dāng)前篩選添加的結(jié)果集
- filter:篩選結(jié)果集
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
'
聚合(Aggregations)
es設(shè)置
設(shè)置es日志級別
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"transient": {
"logger.org.elasticsearch.transport": "trace"
}
}
'
es監(jiān)控
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{
"persistent": {
"xpack.monitoring.collection.enabled": true
}
}
'
公共選項
- pretty=true (json格式展開)
- human=false (以人方便閱讀的方式展示)
- filter_path=took,-hits.hits._id (-代表不展示該字段)
- flat_settings=true (以平面格式展示)
- error_trace=true (顯示錯誤堆棧)
index API
自動創(chuàng)建index
禁止自動創(chuàng)建index:action.auto_create_index = false(默認(rèn)為true)
可以指定pattern:action.auto_create_index = +aaa,-bbb,+ccc,- (+ 允許瞪醋、-禁止)
禁止自動創(chuàng)建mapping:index.mapper.dynamic = false
version
- 創(chuàng)建文檔時不指定默認(rèn)為1忿晕,后續(xù)修改一次+1
- 當(dāng)然可以在創(chuàng)建文檔的時候指定version(version>=0)
Operation Type
方式一:
curl -X PUT "localhost:9200/twitter/_doc/1?op_type=create" -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
方式二:
curl -X PUT "localhost:9200/twitter/_doc/1/_create" -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
op_type=create
or _create
強制指定為創(chuàng)建
模式,如果存在該文檔就會報錯
Routing(路由)
顯示指定路由到kimchy趟章,否則默認(rèn)為文檔ID的hash值
curl -X POST "localhost:9200/twitter/_doc?routing=kimchy" -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
Timeout
curl -X PUT "localhost:9200/twitter/_doc/1?timeout=5m" -H 'Content-Type: application/json' -d'
{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
'
delete API
- delete document
curl -X DELETE "localhost:9200/twitter/_doc/1"
- 可以刪除多個index杏糙、多個type
curl -X POST "localhost:9200/twitter,blog/_docs,post/_delete_by_query" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
- 查看刪除狀態(tài)
curl -X GET "localhost:9200/_tasks?detailed=true&actions=*/delete/byquery"
- 取消刪除任務(wù)
curl -X POST "localhost:9200/_tasks/r1A2WoRbTwKZ516z6NEs5A:36619/_cancel"
- 查看取消刪除任務(wù)情況
curl -X POST "localhost:9200/_delete_by_query/r1A2WoRbTwKZ516z6NEs5A:36619/_rethrottle?requests_per_second=-1"
- 并行刪除
update API
- 腳本更新
curl -X POST "localhost:9200/test/_doc/1/_update" -H 'Content-Type: application/json' -d'
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
}
}
'
- 局部更新
curl -X POST "localhost:9200/test/_doc/1/_update" -H 'Content-Type: application/json' -d'
{
"doc" : {
"name" : "new_name"
}
}
'
- upsert
Reindex API
賦值文檔到新索引里面去
curl -X POST "localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
'