docker-compose安装官方文档
version: '3'
services:
elasticsearch:
image: elasticsearch:8.3.2
container_name: elasticsearch
environment:
- "cluster.name=elasticsearch" #设置集群名称为elasticsearch
- "discovery.type=single-node" #以单一节点模式启动
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" #设置使用jvm内存大小
volumes:
# - /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
# - /data/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
ports:
- 9200:9200
- 9300:9300
docker-compose up -d
进入容器运行下面的指令
/usr/share/elasticsearch/bin/elasticsearch-reset-password
sh-5.0$ ./elasticsearch-reset-password -u elastic
This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y
Password for the [elastic] user successfully reset.
New value: Ot3Epp9Y01=mr5L6nEJ3 // 新密码
elastic:Ot3Epp9Y01=mr5L6nEJ3
得到
ZWxhc3RpYzpPdDNFcHA5WTAxPW1yNUw2bkVKMw==
Authorization
Basic ZWxhc3RpYzpPdDNFcHA5WTAxPW1yNUw2bkVKMw==
或者使用下面这种设置授权的方式也可以
数据类型
官方文档
GET
_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
/_cat/component_templates/_cat/transforms
/_cat/transforms/{transform_id}
/_cat/ml/anomaly_detectors
/_cat/ml/anomaly_detectors/{job_id}
/_cat/ml/trained_models
/_cat/ml/trained_models/{model_id}
/_cat/ml/datafeeds
/_cat/ml/datafeeds/{datafeed_id}
/_cat/ml/data_frame/analytics
/_cat/ml/data_frame/analytics/{id}
GET
_cat/health?v
不使用v
1657763223 01:47:03 elasticsearch green 1 1 2 2 0 0 0 0 - 100.0%
使用v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1657763175 01:46:15 elasticsearch green 1 1 2 2 0 0 0 0 - 100.0%
索引API
PUT
/索引名称
eg:
创建索引 pc
http://localhost:9200/pc
创建索引 phone
http://localhost:9200/phone
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "phone"
}
GET
_cat/indices
yellow open pc SV5lcjTTQD6CaFZL6CB-zg 1 1 0 0 225b 225b
yellow open phone 5U6GL2wxRr2J4KNYRqhweQ 1 1 0 0 225b 225b
DELETE
索引名称
eg:
删除索引 pc
http://localhost:9200/pc
删除索引 phone
http://localhost:9200/phone
{
"acknowledged": true
}
// 指定ID创建没有则创建,有则更新 _version 会增加
PUT /<target>/_doc/<_id>
// 创建文档,自动生成ID
POST /<target>/_doc/
// 没有创建,有报错
PUT /<target>/_create/<_id>
// 没有创建,有报错
POST /<target>/_create/<_id>
POST
pc/_create/90
没有则创建,有则报错
eg:
http://localhost:9200/pc/_create/92
-----
{
"name":"华为笔记本电脑MateBook X Pro 2022 14.2英寸11代酷睿i7 16G 512G锐炬显卡/3.1K触控全面屏/超级终端 皓月银",
"price":9279.00
}
-----
{
"_index": "pc",
"_id": "92",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 16,
"_primary_term": 1
}
-----
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[92]: version conflict, document already exists (current version [1])",
"index_uuid": "SV5lcjTTQD6CaFZL6CB-zg",
"shard": "0",
"index": "pc"
}
],
"type": "version_conflict_engine_exception",
"reason": "[92]: version conflict, document already exists (current version [1])",
"index_uuid": "SV5lcjTTQD6CaFZL6CB-zg",
"shard": "0",
"index": "pc"
},
"status": 409
}
POST
索引/_doc
eg:
http://localhost:9200/pc/_doc
-----
{
"name":"华为笔记本电脑MateBook X Pro 2022 14.2英寸11代酷睿i7 16G 512G锐炬显卡/3.1K触控全面屏/超级终端 皓月银",
"price":9279.00
}
-----
{
"_index": "pc",
"_id": "A6Ol-oEBW7Y2-w8cq7PK",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 36,
"_primary_term": 1
}
// 获取文档
GET <index>/_doc/<_id>
// 判断文档是否存在,不返回数据,如果存在返回200,否则返回404
HEAD <index>/_doc/<_id>
// 获取文档源数据
GET <index>/_source/<_id>
// 判断是否存在,如果存在返回200,否则返回404
HEAD <index>/_source/<_id>
PUT
pc/_doc/91
此借口,没有则创建,有则更新
http://localhost:9200/pc/_doc/92
-----
{
"name":"华为笔记本电脑MateBook X Pro 2022 14.2英寸11代酷睿i7 16G 512G锐炬显卡/3.1K触控全面屏/超级终端 皓月银",
"price":9279.00
}
-----
{
"_index": "pc",
"_id": "92",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 17,
"_primary_term": 1
}
-----
http://localhost:9200/pc/_doc/93
-----
{
"name":"华为笔记本电脑MateBook X Pro 2022 14.2英寸11代酷睿i7 16G 512G锐炬显卡/3.1K触控全面屏/超级终端 皓月银",
"price":9279.00
}
-----
{
"_index": "pc",
"_id": "93",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 18,
"_primary_term": 1
}
-----
{
"_index": "pc",
"_id": "93",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 19,
"_primary_term": 1
}
DELETE /<index>/_doc/<_id>
eg:
http://localhost:9200/pc/_doc/1
-----
{
"_index": "pc",
"_id": "1",
"_version": 1,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 60,
"_primary_term": 1
}
http://localhost:9200/pc/_doc/2
-----
{
"_index": "pc",
"_id": "2",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 63,
"_primary_term": 1
}
GET
_search