Elasticsearch

Docker部署

# 拉取镜像
docker pull elasticsearch
# 启动镜像
# "discovery.type=single-node" 标识单机启动
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:8.2.0
# 进入docker,配置ES的各项密码
./bin/elasticsearch-setup-passwords interactive

image-20220901210824403

CURD

避坑指南

  1. 请求的时候一定要用https协议
  2. 设置认证方式为Basic Auth认证,并输入用户名密码(其中默认用户名为elastic)
创建索引 PUT https://127.0.0.1:9200/shopping
响应:
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "shopping"
}
查询索引详情 GET https://127.0.0.1:9200/shopping
响应:
{
    "shopping": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "shopping",
                "creation_date": "1662037867915",
                "number_of_replicas": "1",
                "uuid": "XuXFzSv8TR6alqCdUq1SXQ",
                "version": {
                    "created": "8020099"
                }
            }
        }
    }
}
查看索引列表 GET https://127.0.0.1:9200/_cat/indices?v
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   shopping XuXFzSv8TR6alqCdUq1SXQ   1   1          0            0       225b           225b
创建文档(随机ID) https://127.0.0.1:9200/shopping/_doc
请求body
{
    "title" : "小米手机",
    "category": "小米",
    "imgage": "https://www.baidu.com",
    "price": 999
}
响应体:
{
    "_index": "shopping",
    "_id": "A7Vo_IIBly-E_1K3JkuU",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
创建文档(自定义ID GET https://127.0.0.1:9200/shopping/_doc/10001
请求体
{
    "title" : "小米手机",
    "category": "小米",
    "imgage": "https://www.baidu.com",
    "price": 999
}
响应体
{
    "_index": "shopping",
    "_id": "10001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}
按ID获取文档 GET https://127.0.0.1:9200/shopping/_doc/10001
响应体:
{
    "_index": "shopping",
    "_id": "10001",
    "_version": 3,
    "_seq_no": 4,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机",
        "category": "小米",
        "imgage": "https://www.baidu.com",
        "price": 999
    }
}
全量查询 GET https://127.0.0.1:9200/shopping/_search/
响应体:
{
    "took": 312,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_id": "A7Vo_IIBly-E_1K3JkuU",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            },
            {
                "_index": "shopping",
                "_id": "BLVt_IIBly-E_1K3qUuX",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            },
            {
                "_index": "shopping",
                "_id": "10001",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            },
            {
                "_index": "shopping",
                "_id": "BbUD_oIBly-E_1K3Tksz",
                "_score": 1.0,
                "_source": {
                    "title": "红米手机",
                    "category": "红米",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            },
            {
                "_index": "shopping",
                "_id": "BrUD_oIBly-E_1K3b0ui",
                "_score": 1.0,
                "_source": {
                    "title": "三星手机",
                    "category": "三星",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            }
        ]
    }
}
全量修改 POST/PUT https://127.0.0.1:9200/shopping/_doc/10001
请求体
{
    "title": "小米手机",
    "category": "小米",
    "imgage": "https://www.baidu.com",
    "price": 4999
}
响应体
{
    "_index": "shopping",
    "_id": "10001",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}
增量更新 POST https://127.0.0.1:9200/shopping/_update/10001
请求体
{
    "doc": {
        "imgage": "https://www.xiaomi.com"
    }
}
响应体
{
    "_index": "shopping",
    "_id": "10001",
    "_version": 9,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 12,
    "_primary_term": 1
}
删除文档 DELETE https://127.0.0.1:9200/shopping/_doc/10001
响应体
{
    "_index": "shopping",
    "_id": "10001",
    "_version": 14,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 17,
    "_primary_term": 1
}
条件查询 GET https://127.0.0.1:9200/shopping/_search/
请求参数
{
    "query": {
        "match": {
            "category" : "米"
        }
    }
}
响应体
{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 0.09237332,
        "hits": [
            {
                "_index": "shopping",
                "_id": "BbUD_oIBly-E_1K3Tksz",
                "_score": 0.09237332,
                "_source": {
                    "title": "红米手机",
                    "category": "红米",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            },
            {
                "_index": "shopping",
                "_id": "A7Vo_IIBly-E_1K3JkuU",
                "_score": 0.09237332,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            },
            {
                "_index": "shopping",
                "_id": "BLVt_IIBly-E_1K3qUuX",
                "_score": 0.09237332,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            },
            {
                "_index": "shopping",
                "_id": "10001",
                "_score": 0.09237332,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "imgage": "https://www.baidu.com",
                    "price": 4999
                }
            }
        ]
    }
}
分页查询 GET https://127.0.0.1:9200/shopping/_search/
请求体
{
    "query": {
        "match_all": {
        }
    },
    // 起始位置
    "from" : 0,
    // 页大小
    "size": 2
}
响应
{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_id": "BbUD_oIBly-E_1K3Tksz",
                "_score": 1.0,
                "_source": {
                    "title": "红米手机",
                    "category": "红米",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            },
            {
                "_index": "shopping",
                "_id": "BrUD_oIBly-E_1K3b0ui",
                "_score": 1.0,
                "_source": {
                    "title": "三星手机",
                    "category": "三星",
                    "imgage": "https://www.baidu.com",
                    "price": 999
                }
            }
        ]
    }
}
最后修改:2022 年 09 月 02 日
如果觉得我的文章对你有用,请随意赞赏