4

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"

返回的结果都是

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

重建索引 reindex

因为 index 的 mapping 只能新增字段,不能修改现有的字段,只能通过重建索引可以完成

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

查看 Elasticsearch 后台任务

可以查看重建索引目前的进度

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

新增记录和查询

新增和更新记录

curl -X PUT "localhost:9200/netdisk-document-v1/_doc/123" -H 'Content-Type: application/json' -d'
{
    "title": "这是一个测试标题", 
    "content": "这是一个测试内容", 
    "uploadDate": "2019-08-08T08:00:00"
}
'

删除记录

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

查询 DSL

term/match/match_phrase 的区别 https://www.jianshu.com/p/eb30eee13923

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

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: /tmp

建立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...

openmartin
71 声望3 粉丝

古典占星 。师从台湾杨国正老师。|| 占星看盘请私信。|| 伟大的灵魂都是雌雄同体 || 开放心态,契约精神