1、安裝es以及kibana
參看:https://www.cnblogs.com/kakatadage/p/9922359.htmlhtml
2、查看官方使用文檔
參看:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/index.htmlnode
3、index相關操做數據結構
注:如下 test 均爲索引名app
1.建立index
(1)最簡單的建立方式,參數均使用默認配置less
PUT /test
(2)能夠帶三個參數:aliases、mappings以及settingselasticsearch
- aliases: 給一個或者多個index賦予另一個別名
eg:
給單個index添加別名
ide
POST /_aliases
{
"actions" : [
{
"add" : {
"index" : "test1", "alias" : "alias1"
}
}
]
}
orui
PUT /test/_alias/alias1
給多個index添加別名spa
POST /_aliases
{
"actions" : [
{
"add" : {
"indices" : ["test1", "test2"], "alias" : "alias1"
}
}
]
}
建立索引時取別名code
PUT /test
{
"aliases": {
"test-1": {}
}
}
- mappings:索引index裏面的數據結構,就像JAVA對象同樣,其裏面還包含這種屬性(Field;注意:ES 不支持修改已有的Filed名字和類型)
eg:
PUT /test/_mapping
{
"properties": {
"email": {
"type": "keyword"
}
}
}
獲取mapping
GET /test/_mapping
獲取mapping field
GET /test/_mapping/field/fieldName
刪除mapping field
- settings: index的一些參數有以下參數:
-- include_type_name(是否包含mapping type;默認 false;注意:mapping types 在7.x的時候被移除,具體緣由參看:https://www.elastic.co/guide/en/elasticsearch/reference/7.3/removal-of-types.html#_custom_type_field)
-- wait_for_active_shards(等待副本節點都處於活動狀態才進行操做,不然一直等待直至超時;默認1,即僅等待主分片在繼續操做以前處於活動狀態)
-- timeout(操做超時時間;默認30s)
-- master_timeout (鏈接master節點超時時間;默認30s)
eg:
PUT /test
{
"settings" : { // 在7.x版本:分片數和備份數默認都是 1
"number_of_shards" : "1",
"number_of_replicas" : "1",
},
"mappings" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
注意:因爲在7.x的時候,mapping types被移除,因此每一個index只存儲一個實體
2.建立Mapping
(1)在建立index的時候建立
PUT /test
{
"settings" : { // 在7.x版本:分片數和備份數默認都是 1
"number_of_shards" : "1",
"number_of_replicas" : "1",
},
"mappings" : {
"properties" : {
"field1" : { "type" : "text" }
}
}
}
(2)在已有的index裏建立
PUT /test/_mapping
{
"properties": {
"email": {
"type": "keyword"
}
}
}
注意:ES 不支持修改已有的Filed名字和類型
3.刪除index
(1)刪除單個
DELETE /twitter
(2)刪除多個/所有(注意:此操做很是危險,因此此操做是能夠禁止的,即將 elasticsearch.yml 配置中的 action.destructive_requires_name 設置爲true,此時刪除索引就須要指明明確的索引名)
DELETE _all 或者 DELETE xx*
4.獲取index詳情
(1)獲取單個
GET /test
(2)獲取多個/全部
GET _all 或者 GET xx*
5.判斷某個index是否存在(存在則返回 200;不然返回 404)
HEAD /test
注意:此處 test 爲索引名,可是若是不存在名爲 test 的索引,可是存在別名爲 test 也判斷爲true
6.關閉/開啓索引(一旦關閉則沒法對索引進行讀寫操做)
POST /test/_close
POST /test/_open
注意:也能夠和刪除操做同樣使用_all關鍵字或模糊匹配關閉多個索引,固然也能夠禁用此操做,即設置 action.destructive_requires_name 爲 true。關閉索引操做很是佔用磁盤空間,因此能夠設置 cluster.indices.close.enable = false 禁用關閉索引操做,默認是 true.
7.收縮索引(將一個索引遷至一個主分片更少的索引,可是目標索引的分片數,必須是源索引分片數的因子。好比,源索引的分片數是:8,那麼目標索引的分片數能夠是:4, 2, 1;)
(1)將索引設置爲只讀且將全部副本遷至同一個節點(收縮前提條件:副本必須在同一節點)
PUT /my_source_index/_settings
{
"settings": {
"index.routing.allocation.require._name":
"shrink_node_name", // 強制將每一個分片的副本重定位到名爲shrink_node_name的節點
"index.blocks.write": true // 設置爲只讀
}
}
(2)建立目標索引(基於源索引複製一份)
POST my_source_index/_shrink/my_target_index?copy_settings=true
{
"settings": {
"index.routing.allocation.require._name": null, // 清除源索引 強制將每一個分片的副本重定位到名爲shrink_node_name的節點 配置
"index.blocks.write": null // 清除源索引 設置爲只讀 配置
}
}
(3)遷移
POST my_source_index/_shrink/my_target_index?copy_settings=true
{
"settings": {
"index.number_of_replicas": 1, // 副本個數
"index.number_of_shards": 1, // 分片數
"index.codec": "best_compression" // 最佳壓縮僅在對索引進行新寫入時生效,例如強制合併分片到單個段時
},
"aliases": {
"my_search_indices": {}
}
}
8.拓展索引(和收縮相反)
拆分規則:索引能夠拆分屢次,但拆分的最大分片數是由建立索引是的number_of_routing_shards決定的。拆分後的分片數量需是number_of_routing_shards的因子,即number_of_routing_shards是拆分後分片數的倍數。
例如,原有主分片爲5,number_of_routing_shards=30的索引,能夠按以下幾種狀況拆分:
5 → 10 → 30 (split by 2, then by 3)
5 → 15 → 30 (split by 3, then by 2)
5 → 30 (split by 6)
(1) 將源索引設置爲只讀
PUT /my_source_index/_settings
{
"settings": {
"index.blocks.write": true
}
}
(2)遷移
POST my_source_index/_split/my_target_index
{
"settings": {
"index.number_of_shards": 3
}
}
注意:拆分必須知足的條件:
- 目標索引必須不存在
- 索引的主碎片必須少於目標索引。
- 目標索引中的主碎片數量必須是源索引中的主碎片數量的一個因子。
- 處理拆分進程的節點必須有足夠的空閒磁盤空間來容納現有索引的第二個副本
9.利用別名自動拋棄舊索引而從新指向新索引
(1)建立索引並帶上別名
PUT /logs-000001
{
"aliases": {
"logs_write": {}
}
}
(2)設置生成新索引規則
POST /logs_write/_rollover
{
"conditions": {
"max_age": "7d", // 7天
"max_docs": 1000, // 1000行
"max_size": "5gb" // 5GB
}
}
即logs-000001文件自建立以來存活7天或者最大文檔數超過1000或者索引主分片最大超過5GB 則會自動建立logs-000002.其索引生成規則:若是現有索引的名稱以 - 和數字結尾,例如 logs-000001,而後新索引的名稱將遵循相同的模式,遞增數字(logs-000002)。若是不是以-和數字結尾則須要自定義名字
POST /logs_write/_rollover/my_new_index_name
{
"conditions": {
"max_age": "7d",
"max_docs": 1000,
"max_size": "5gb"
}
}
10.凍結/解凍索引(被凍結的索引沒法進行寫入操做)
POST /my_index/_freeze
POST /my_index/_unfreeze
4、數據相關操做(根據ID基礎操做)
1.數據插入
一共以下四種方式:_doc 和 _create 區別在於,_doc 是若是插入數據已經存在則會更新,而_create只能是插入不存在的數據;默認狀況下若是插入的index以及mapping不存在則會自動建立,也能夠經過設置讓其不自動建立,設置參數爲:action.auto_create_index
PUT /<index>/_doc/<_id> POST /<index>/_doc/ PUT /<index>/_create/<_id> POST /<index>/_create/<_id>
(1)自增ID插入(使用POST請求)
POST my-index/_doc
{
"age": 0
}
(2)本身建立ID插入(使用PUT請求)
PUT my-index/_doc/2
{
"age": 0
}
(3)upSert方法,記錄不存在就插入,不然就執行腳本作更新
POST test/_doc/2/_update
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
},
"upsert" : {
"counter" : 1
}
}
(4)記錄不存在就插入,不然就更新相應字段(counter),無需更新的字段不要傳,不然會更新,即便字段值爲null也會更新爲null
POST test/_doc/1/_update
{
"doc" : {
"counter" : 7
}
}
(5)對已有數據進行更新
POST bigdata-archive/_update_by_query
{
"script": {
"source": "ctx._source['imageCount']=0"
},
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "imageCount"
}
}
]
}
}
}
2.數據查找
一共以下四種方式: GET 獲取數據,HEAD 驗證數據是否存在;默認狀況下每次 GET 都會刷新索引以保證數據是最新的,也能夠關閉此功能,經過設置:realtime = false 實現;_source 只返回 fields(可指定返回哪些field) 而 _doc 返回 index 全部信息;
GET <index>/_doc/<_id> HEAD <index>/_doc/<_id> GET <index>/_source/<_id> HEAD <index>/_source/<_id>
3.刪除數據
DELETE /<index>/_doc/<_id>
4.更新數據
POST /<index>/_update/<_id>
(1)通常修改方式,固然也能夠用數據插入方式覆蓋已有的字段
POST test/_update/1
{
"doc" : {
"name" : "new_name"
}
}
(2)使用腳本修改(默認是使用ES腳本)
POST test/_update/1
{
"script" : {
"source": "ctx._source.counter +=
params.count",
"lang": "painless",
"params" : {
"count" : 4
}
}
}
5、查詢
1.根據條件分頁查詢(具體有關QUERY操做參看官網文檔:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl.html)
POST test/_search
{
"query": {
"match_all": {
}
// "match": {
// "filedName": xxx
// }
},
"sort": [
{
"counter": {
"order": "asc"
}
}
],
"from": 1,
"size": 1
}
2.聚合查詢(aggs:聚合查詢關鍵詞;參看官網文檔:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-aggregations.html)
POST test/_search
{
"aggs": {
"avg_grade": {
"avg": {
"field": "counter"
}
}
}
}
3.模糊查詢(具體參看官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl-query-string-query.html)
POST test/_search
{
"query": {
"query_string": {
"query": 1,
"fields": ["counter"]
}
}
}