elasticsearch 学习

docker-compose安装官方文档

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 // 新密码

postmax 设置授权

将用户名:密码 base64编码

elastic:Ot3Epp9Y01=mr5L6nEJ3

得到

ZWxhc3RpYzpPdDNFcHA5WTAxPW1yNUw2bkVKMw==

设置请求头

Authorization

Basic ZWxhc3RpYzpPdDNFcHA5WTAxPW1yNUw2bkVKMw==

测试

在这里插入图片描述

或者使用下面这种设置授权的方式也可以

在这里插入图片描述

数据类型

数据类型

RESTful API

官方文档

_cat 相关API

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 格式化输出

不使用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>
  • target:要定位的数据流或索引的名称。
  • _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
}

自动生成文档 ID

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值