ES 經常使用命令

ES 經常使用命令

查看版本

curl -XGET 'localhost:9200'
{
  "name" : "5koA13t",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "kQhdXKz4S_6iWNZy7NITuw",
  "version" : {
    "number" : "5.4.2",
    "build_hash" : "929b078",
    "build_date" : "2017-06-15T02:29:28.122Z",
    "build_snapshot" : false,
    "lucene_version" : "6.5.1"
  },
  "tagline" : "You Know, for Search"
}

查看索引狀態

curl -XGET 'localhost:9200/_cat/indices?v&pretty'
health status index                   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_task_manager    EYi69PoYQASniv_dTpvkdA   1   0          2            0     30.8kb         30.8kb
green  open   kibana_sample_data_logs L0oSKJgnRmOxCp3zSHZojw   1   0      14075            0     11.5mb         11.5mb
green  open   .kibana_1               SaISvKYHQ6Su72LVtLpv9A   1   0         44            1    127.7kb        127.7kb

新建 index mapping

curl -X PUT "localhost:9200/netdisk-document-v1" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "docId": {"type": "long"},
      "title":   {"type": "text", "analyzer": "hanlp_index", "search_analyzer": "hanlp_nlp", "index_options" : "offsets"}, 
      "content": {"type": "text" , "analyzer": "hanlp_index", "search_analyzer": "hanlp_nlp", "index_options" : "offsets"}, 
      "uploadDate": {"type": "date"}
    }
  }
}
'

查看 index mapping

curl -XGET 'localhost:9200/netdisk-document-v1/_mapping/?pretty'

刪除 index

curl -X DELETE 'localhost:9200/netdisk-document-v1?pretty'

設置 index 別名 alias

curl -X PUT "localhost:9200/netdisk-document-v1/_alias/netdisk-document?pretty"

刪除 index 別名 alias

curl -X DELETE "localhost:9200/netdisk-document-v1/_alias/netdisk-document?pretty"

alias 和 index 互查

curl -X GET "localhost:9200/*/_alias/netdisk-document?pretty"
curl -X GET "localhost:9200/netdisk-document-v1/_alias/*?pretty"

返回的結果都是git

{
  "netdisk-document-v1" : {
    "aliases" : {
      "netdisk-document-cn" : { }
    }
  }
}

重建索引 reindex

由於 index 的 mapping 只能新增字段,不能修改現有的字段,只能經過重建索引能夠完成github

curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "netdisk-document-v1"
  },
  "dest": {
    "index": "netdisk-document-v2"
  }
}
'

查看 Elasticsearch 後臺任務

能夠查看重建索引目前的進度shell

curl -X GET "localhost:9200/_tasks?detailed=true&actions=*reindex&pretty"

新增記錄和查詢

新增和更新記錄npm

curl -X PUT "localhost:9200/netdisk-document-v1/_doc/123" -H 'Content-Type: application/json' -d'
{
    "title": "這是一個測試標題", 
    "content": "這是一個測試內容", 
    "uploadDate": "2019-08-08T08:00:00"
}
'

刪除記錄json

curl -X DELETE "localhost:9200/netdisk-document-v1/_doc/123"

查詢 DSLsegmentfault

term/match/match_phrase 的區別 https://www.jianshu.com/p/eb30eee13923app

設置 minimum_should_match https://my.oschina.net/u/3625378/blog/1492575curl

curl -XGET "http://localhost:9200/report-doc-cn/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match_phrase": {
            "title": {
                "query": "新能源"
            }
        }
    }
}'

測試 analyzer

curl -XPOST 'http://localhost:9200/_analyze?pretty' -H 'Content-Type: application/json' -d'
{
  "analyzer": "hanlp_index",
  "text": "5G是將來重要的趨勢"
}'

備份和恢復

使用 es 提供的 snapshot 功能

首先須要編輯config/elasticsearch.yml文件增長備份存儲庫的位置。好比path.repo: /tmpelasticsearch

創建repo測試

curl -XPUT 'http://localhost:9200/_snapshot/my_repository' -d '
{
    "type": "fs",
    "settings": {
        "location": "/tmp/my_repository",
        "compress": true
    }
}'

返回

{"acknowledged":true}

建立 snapshot

curl -XPUT "http://localhost:9200/_snapshot/my_repository/snap_1?wait_for_completion=true" -d '
{
    "indices": "logstash-event-login,logstash-event-view",
    "ignore_unavailable": "true",
    "include_global_state": false
}'

wait_for_completion參數表示會等到snapshot完成才返回,不加這個參數也能夠經過其餘接口獲取到快照的進度

curl -XGET "http://localhost:9200/_snapshot/my_repository/snap_1/_status?pretty"
查看 snapshot
curl -XGET 'http://localhost:9200/_snapshot/my_repository/snap_1?pretty'

返回結果

{
  "snapshots" : [
    {
      "snapshot" : "snap_1",
      "uuid" : "1PXR1UNeSCK_BlfK_ZMyTg",
      "version_id" : 5040299,
      "version" : "5.4.2",
      "indices" : [
        "logstash-event-login",
        "logstash-event-view"
      ],
      "state" : "SUCCESS",
      "start_time" : "2017-07-11T08:18:26.567Z",
      "start_time_in_millis" : 1499761106567,
      "end_time" : "2017-07-11T08:18:28.660Z",
      "end_time_in_millis" : 1499761108660,
      "duration_in_millis" : 2093,
      "failures" : [ ],
      "shards" : {
        "total" : 10,
        "failed" : 0,
        "successful" : 10
      }
    }
  ]
}
刪除快照
curl -XDELETE 'http://localhost:9200/_snapshot/my_repository/snap_1'

查看全部快照

curl -XGET 'http://localhost:9200/_snapshot/my_repository/_all?pretty'

恢復 snapshot

curl -XPOST "http://localhost:9200/_snapshot/my_repository/snap_1/_restore?wait_for_completion=true&pretty" -d '
{
    "indices": "logstash-event-login",
    "ignore_unavailable": "true",
    "include_global_state": false,
    "rename_pattern": "logstash-event-login",
    "rename_replacement": "restore_logstash-event-login"
}'

參數 rename_patternrename_replacement 用來正則匹配要恢復的索引,而且重命名。下面的例子:test-index => copy_index, test_2 => coyp_2

curl -XPOST "http://localhost:9200/_snapshot/my_repository/snap_1/_restore?wait_for_completion=true&pretty" -d '
{
    "indices": "test-index,test-2",
    "ignore_unavailable": "true",
    "include_global_state": false,
    "rename_pattern": "test-(.+)",
    "rename_replacement": "copy_$1"
}'

使用 elasticdump

安裝

npm install elasticdump -g

elasticdump 的 input 和 output 均可以指定爲 elasticsearch 和文件

# 導出數據
elasticdump --input=http://localhost:9200/logstash-event-login --output=data.json --type=data
# 導出mapping
elasticdump --input=http://localhost:9200/logstash-event-login --output=mapping.json --type=mapping


# 導入mapping
elasticdump --input=mapping.json --output=http://localhost:9200/elasticdump-event-login --type=mapping
# 導入數據
elasticdump --input=data.json --output=http://localhost:9200/elasticdump-event-login --type=data

參考資料

  1. https://wenchao.ren/archives/371
  2. http://www.tuicool.com/articl...
  3. https://segmentfault.com/a/11...
  4. https://github.com/taskrabbit...
相關文章
相關標籤/搜索