Elasticsearch之基本使用

  

这里大概解答下各个目录、配置文件的作用:

目录配置文件描述
bin   放置脚本文件,如启动脚本 elasticsearch, 插件安装脚本等。
config elasticserch.yml elasticsearch 配置文件,如集群配置、jvm 配置等。
jdk   java 运行环境
data path.data 数据持久化文件
lib   依赖的相关类库
logs path.log 日志文件
modules   包含的所有 ES 模块
plugins   包含的所有已安装的插件
注意点:
有些童鞋的机器内存可能不够,就需要修改 JVM 参数,配置文件路径为 config/jvm.options,ES V7.1 版本默认为 1g, 老版本为2g, 你可以自行修改。
Xmx 和Xms 数值请设置相同;
Xmx 不要超过机器内存的 50%;
内存总量不要超过 30GB, 参见官方文档 https://www.elastic.co/cn/blog/a-heap-of-trouble;

1. 查看集群健康情况

http://localhost:9200/_cat

=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

 
http://localhost:9200/_cat/health?v

epoch      timestamp cluster     status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1586056370 03:12:50  main-search green           9         7     50  25    0    0        0             0                  -                100.0%

说明:v是用来要求在结果中返回表头

状态值说明

Green - everything is good (cluster is fully functional),即最佳状态
Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional),即数据和集群可用,但是集群的备份有的是坏的
Red - some data is not available for whatever reason (cluster is partially functional),即数据和集群都不可用

2. 查看所有节点

/_cat/nodes

3. 查看所有索引

http://localhost:9200/_cat/indices?v

green open .monitoring-kibana-7-2020.04.01 601ZG1FRQY688qAp3O1uaQ 1 1     8640       0   5.3mb   2.6mb
green open .monitoring-kibana-7-2020.04.02 J89ViHaQQC697nQ5gqMXtg 1 1     8639       0   5.3mb   2.6mb
green open .monitoring-kibana-7-2020.04.03 C-Sp-DDpTmWMZzdk1E3Wbw 1 1     3242       0   2.1mb     1mb
green open .monitoring-es-7-2020.04.05     3NT3phueS-qf76XDtwYKcw 1 1    34632   59482  60.6mb    31mb

4. 创建索引

PUT /customer?pretty

5. 添加文档到索引

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'

6. 查询所有文档

GET /customer/_search?q=*&sort=name:asc&pretty


json格式:
GET /customer/_search
{
  "query": { "match_all": {} },
  "sort": [
    {"name": "asc" }
  ]
}

索引管理

  1. 创建索引

PUT user
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 2
        }
    }
}

设置索引的分片数为3,备份数为2。注意:在ES中创建一个索引类似于在数据库中建立一个数据库(ES6.0之后类似于创建一个表)



说明:
默认的分片数是5到1024
默认的备份数是1
索引的名称必须是小写的,不可重名

简写:

PUT twitter
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}

  2. 创建mapping映射

注意:在ES中创建一个mapping映射类似于在数据库中定义表结构,即表里面有哪些字段、字段是什么类型、字段的默认值等;也类似于solr里面的模式schema的定义

PUT twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 2
        }
    },
   "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}

  3. 创建索引时加入别名定义

PUT twitter
{
    "aliases" : {
        "alias_1" : {},
        "alias_2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        }
    }
}

  4. 索引创建完成返回值

{
"acknowledged": true,   // 索引创建成功
"shards_acknowledged": true,  // 分片+副本创建,启动成功
"index": "logs" // 索引名称
}

  5. 查看索引的定义信息

  1) GET /twitter,可以一次获取多个索引(以逗号间隔) 获取所有索引 _all 或 用通配符*

{
"logs": {
"aliases": { },
"mappings": { },
"settings": {
"index": {
"creation_date": "1586100271307",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "50kKYpnoRX-p6rwxXuo2Jw",
"version": {
"created": "7050099"
},
"provided_name": "logs"
}
}
}
}

  2)索引设置项

GET /twitter/_settings

  3)数据定义

  

GET /twitter/_mapping

  6. 删除索引

DELETE /twitter

说明:

可以一次删除多个索引(以逗号间隔) 删除所有索引 _all 或 通配符 *

  7. 判断索引是否存在

HEAD twitter

HTTP status code 表示结果 404 不存在 , 200 存在

  8. 修改索引的settings信息

索引的设置信息分为静态信息和动态信息两部分。静态信息不可更改,如索引的分片数。动态信息可以修改。

REST 访问端点:
/_settings 更新所有索引的。
{index}/_settings 更新一个或多个索引的settings。

详细的设置项请参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings

  9. 修改备份数

PUT /twitter/_settings
{
    "index" : {
        "number_of_replicas" : 2
    }
}

  10. 设置回默认值,用null

PUT /twitter/_settings
{
    "index" : {
        "refresh_interval" : null
    }
}

  11. 设置索引的读写

index.blocks.read_only:设为true,则索引以及索引的元数据只可读
index.blocks.read_only_allow_delete:设为true,只读时允许删除。
index.blocks.read:设为true,则不可读。
index.blocks.write:设为true,则不可写。
index.blocks.metadata:设为true,则索引元数据不可读写。

  12. 索引模板

在创建索引时,为每个索引写定义信息可能是一件繁琐的事情,ES提供了索引模板功能,让你可以定义一个索引模板,模板中定义好settings、mapping、以及一个模式定义来匹配创建的索引。

注意:模板只在索引创建时被参考,修改模板不会影响已创建的索引

12.1 新增/修改名为tempae_1的模板,匹配名称为te* 或 bar*的索引创建:

PUT _template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}

  13. 查看索引模版

GET /_template/template_1
GET /_template/temp* 
GET /_template/template_1,template_2
GET /_template

  14. 删除模版

DELETE /_template/template_1

  15. Open/Close  Index   打开/关闭索引

POST /my_index/_close
POST /my_index/_open


说明:

关闭的索引不能进行读写操作,几乎不占集群开销。
关闭的索引可以打开,打开走的是正常的恢复流程。

   

  16. 索引添加新字段

{
    "properties": {
        "publish_date": {
            "type": "keyword"
        }
    }
}

  

原文地址:https://www.cnblogs.com/xingxia/p/Elasticsearch_usage.html