Elasticsearch6.x indices apis(索引api)


1.索引api

indices apis 用於管理索引划分,索引設置,索引別名,字段映射,索引模板

index management

1.1 create index

創建索引,可以指定設置和字段映射,也可以不指定,甚至可以省略創建索引過程,es會自動創建,示例:

curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}
'


1.2 delete index

指定索引名,或者通配符來刪除索引,不能使用別名,使用*或_all會刪除所有索引。示例:

curl -X DELETE "localhost:9200/twitter"

1.3 get index

獲取索引的信息,示例:

curl -X GET "localhost:9200/twitter"

1.4 indices exists

檢查索引或者別名是否存在,示例:

curl -X HEAD "localhost:9200/twitter"

1.5 open/close index api

索引可以關閉打開,關閉之后該索引不可用(不能讀寫,會阻塞),需要使用時可以再把索引打開,示例:

curl -X POST "localhost:9200/my_index/_close"
curl -X POST "localhost:9200/my_index/_open"

關閉索引占用大量磁盤空間,可能導致環境出問題,將cluster.indices.close.enable 置為false可以關閉 索引的close功能

1.6 shrink index(收縮索引)

收縮索引api允許將現有索引縮小為具有較少主分片的新索引。

目標索引中請求的主分片數必須是源索引中主分片數的一個約數,如8可以縮小為4,2,1(這是為了保持一致性hash),收縮的工作原理如下:
  首先創建一個和源索引定義一致但分片數較少的目標索引
  然后將源索引中的段硬鏈接到目標索引(如果文件系統不支持硬鏈接,則復制)
  最后,恢復目標索引,就像一個剛重新打開的封閉索引
  示例:

curl -X POST "localhost:9200/my_source_index/_shrink/my_target_index" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 1, 
    "index.codec": "best_compression" 
  },
  "aliases": {
    "my_search_indices": {}
  }
}
'

1.7 split index(拆分索引)

split index api允許將一個已經存在的索引拆分為新索引,其中每個原始主分片在新索引中會拆分為兩個或多個主分片。
可以拆分索引的次數由index.number_of_routing_shards設置決定。路由分片的數量指定內部使用的散列空間。
  number_of_shards 當前索引的分片數
  number_of_routing_shards 拆分時可以拆分的分片數
how does splitting work
  首先,創建和目標索引設置一致的新索引(除了新索引的分片數比源索引大)
  然后將源索引中的段硬鏈接到目標索引
  底層文件拷貝完畢后,所有文檔將再次hashed,刪除不屬於本分片的文檔
  最后,恢復源索引
why doesn't es support increment resharding?
  hash一致性問題
  示例:
  拆分前需要將索引設置為只讀

curl -X PUT "localhost:9200/my_source_index/_settings" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index.blocks.write": true 
  }
}
'

開始分片,指定新索引的設置

curl -X PUT "localhost:9200/my_source_index" -H 'Content-Type: application/json' -d'
{
    "settings": {
        "index.number_of_shards" : 1,
        "index.number_of_routing_shards" : 2 
    }
}
'

1.8 rellover index(滾動索引)

rollover index api允許在現有索引太大或者太舊時,將索引別名轉移到新索引
示例:

curl -X PUT "localhost:9200/logs-000001" -H 'Content-Type: application/json' -d'
{
  "aliases": {
    "logs_write": {}
  }
}
'

# Add > 1000 documents to logs-000001,然后執行下面的語句會生成一個新的索引

curl -X POST "localhost:9200/logs_write/_rollover" -H 'Content-Type: application/json' -d'
{
  "conditions": {
    "max_age":   "7d",
    "max_docs":  1000,
    "max_size":  "5gb"
  }
}
'

mapping managemenet

1.9 put mapping

put mapping api允許將字段添加到現有索引或更改現有字段的映射,示例:

curl -X PUT "localhost:9200/twitter/_mapping/_doc" -H 'Content-Type: application/json' -d'
{
  "properties": {
    "email": {
      "type": "keyword"
    }
  }
}
'

1.10 get mapping

獲取索引中定義的映射關系(映射關系對比於數據庫中的表結構,每個索引存在一個映射關系),示例:

curl -X GET "localhost:9200/twitter/_mapping/_doc"

1.11 get field mapping

獲取索引中某個具體字段的映射,示例(獲取字段title的映射關系):

curl -X GET "localhost:9200/publications/_mapping/_doc/field/title"

1.12 types exists

判斷某個type是否存在,示例:

curl -X HEAD "localhost:9200/twitter/_mapping/tweet"

alias management 索引別名管理

1.13 index aliases

給索引賦予一個別名,示例:

curl -X PUT "localhost:9200/logs_201305/_alias/2013"
curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}
'
curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } }
    ]
}
'

index settings(索引設置,用於控制索引操作的一些行為)

1.14 update indices settings

實時修改索引級別設置,rest形式為: endpoint(端點) /_settings(更新所有索引), {index}/_settings(更新一個或多個),請求正文包含更新的設置,示例:

curl -X PUT "localhost:9200/twitter/_settings" -H 'Content-Type: application/json' -d'
{
    "index" : {
        "number_of_replicas" : 2
    }
}
'

1.15 get settings

獲取索引的設置項(即索引中行為有關的定義,如數據庫某個庫的可配置參數:連接數,大小),示例:

curl -X GET "localhost:9200/log_2013_*/_settings"

1.16 analyze

對文本執行分析過程並返回分析結果(返回給定字符串的分詞結果),示例:

curl -X GET "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'
{
  "analyzer" : "standard",     //指定使用的分析器
  "text" : "this is a test"      //要分析的字段類型和內容
}
'

獲取分詞器拆分結果的更詳細信息,示例:

curl -X GET "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'
{
  "tokenizer" : "standard",   //分詞器
  "filter" : ["snowball"],      //過濾器,對分詞結果進行進一步過濾
  "text" : "detailed output",
  "explain" : true,
  "attributes" : ["keyword"] 
}
'

1.17 index templates

指定創建索引時自動應用的模板,主要包括setting(索引的屬性)和mapping(索引的字段映射等設置)。
匹配模式是索引模板中設置"index_patterns": ["te*", "bar*"],這樣所有以te,bar開頭的索引都會應用這個模板
示例:

curl -X PUT "localhost:9200/_template/template_1" -H 'Content-Type: application/json' -d'
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        }
      }
    }
  }
}
'

刪除索引模板:

  curl -X DELETE "localhost:9200/_template/template_1"
獲取索引模板:
  curl -X GET "localhost:9200/_template/template_1" 獲取指定索引模板,支持*
  curl -X GET "localhost:9200/_template" 獲取所有索引模板

monitoring

1.18 indices stats

索引級別統計信息提供有關索引上發生的不同操作的統計信息,api提供有關索引范圍的統計信息(盡管大多數統計信息也可以使用節點級別范圍獲取)

1.19 indices segments

獲取當前節點的各個分片包含的段信息,包括段名(文件名),段中文檔數量,大小,刪除數量
示例:

curl -X GET "localhost:9200/shakespeare/_segments?pretty"

1.20 indices recovery

檢查索引是否正在恢復,恢復到什么位置了
示例:

curl -X GET "localhost:9200/shakespeare/_recovery?pretty"

1.21 indices shard stores

獲取分片的信息,可以指定獲取不同狀態的分片,示例:

curl -X GET "localhost:9200/_shard_stores?status=green"

status management(狀態管理)

1.22 clear cache(清空緩存)

curl -X POST "localhost:9200/twitter/_cache/clear"

1.23 refresh(刷新數據)

刷新某個索引的修改、創建,使得查詢可見
示例:

curl -X POST "localhost:9200/twitter/_refresh"

1.24 flush()

將數據刷新到索引存儲(磁盤)並清除內存事物日志(transactionlog log)
示例:

curl -X POST "localhost:9200/shakespeare/_flush?pretty"

請求參數:
wait_if_ongoing 如果另一個刷新在執行,是否等待,默認false,分片級拋異常
force 強制立即刷新
synced flush
  es追蹤每個分片的索引活動。5分鍾內沒有收到索引請求的分片自動被標記為inactive。
  synced flush為每個分片生成unique marker(sync_id)。
  由於處於非活動狀態的索引被sync id標記,因此可以用作檢測兩個分片的lucene索引是否相同的快速方法。
  可以通過手動的方式進行sync flush調用而不需要等待5min,示例:

curl -X POST "localhost:9200/twitter/_flush/synced"

flush和refresh的區別

數據寫入es完整過程:

  外部文檔數據 -> index-buffer -> 文件系統緩存(同時sync translog) -> 磁盤。

refresh
  索引文檔的同時可以進行搜索。

  實時搜索是基於內存的(index-buffer)。

  es索引提交不會把數據直接寫到磁盤,而是將index-buffer中文檔解析完成的segment寫到filesystem cache中(避免磁盤損耗,而且已經可以提供搜索功能)。

  此時translog依然記錄着這些索引請求,直到flush刷新到磁盤。
  refresh即為index-buffer到文件緩存系統的過程,目的是所有已有數據可搜索(將數據交給lucene進行索引,但是可能還沒有commit,因為lucene的commit比較耗時)
flush

  從文件緩存系統到磁盤的過程,目的是數據不丟失(flush操作實際就是觸發lucene進行commit,然后將事物日志translog落地到磁盤)

  es的每個shard會每30分鍾執行一次flush操作
  當translog的數據達到某個上限的時候會進行一次flush操作

1.25 force merge

強制合並分片中的段(段是lucene存儲文檔的最小單位,不可修改,每次refresh都會生成一個新的段,包含最新的數據,后續會自動合並),示例:

curl -X POST "localhost:9200/twitter/_forcemerge"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM