Mac OS 安装 [注:系统需先安装 brew 工具]:
brew install --cask docker
查看docker是否完成安装
docker -v安装-启动Es
拉取 Es 镜像
docker pull elasticsearch:7.17.1
启动 Es – 单节点启动
编写 docker-compose.yml 文档
version: '3.7'
services:
elasticsearch:
image: elasticsearch:7.17.1
container_name: elasticsearch
environment:
- xpack.security.enabled=false
- discovery.type=single-node # 单节点
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65536
hard: 65536
cap_add:
- IPC_LOCK
volumes:
- ./data:/usr/share/elasticsearch/data
- ./plugins:/usr/share/elasticsearch/plugins
ports:
- 9200:9200
- 9300:9300
启动Es
docker-compose up -d
查看Es 运行状态
docker psEs 基础用法 索引操作 新增索引-默认索引配置
新增索引-有类型、映射的索引put{{host}}/20220325001
获取索引的mapput{{host}}/20220325002
{ "settings":{ "number_of_shards":5, "number_of_replicas":3 }, "mappings":{ "properties":{ "name":{ "type":"keyword" } } } }
查询索引
get
{{host}}/20220325002/_mapping
删除索引get{{host}}/index_name
获取所有的索引delete{{host}}/index_name
添加数据 添加数据{{host}}/_cat/indices?v
自定义数据id
post
{{host}}/20220325/_doc/
{ "shoppingName":"fudaopin", "price":45.9, "desc":"测试" }
删除数据 删除数据
post || put
{{host}}/20220325/_doc/20220324001
{ "shoppingName":"fudaopin", "price":45.9, "desc":"测试" }
数据更新 更新数据 put 全量更新delete{{host}}/20220325/_doc/20220325001
更新数据 post非全量更新
put
{{host}}/20220325/_doc/202203001
{ "shoppingName":"fudaopin", "price":45.9, "desc":"运维" }
查询数据 查询索引下的所有数据post{{host}}/20220325/_update/202203001
{ "doc":{ "desc":"运维" } }
根据id查询数据get{{host}}/20220325/_search
根据自定义条件查询数据(关键字拼接模式)
get
{{host}}/20220325/_doc/20220325001
根据自定义条件查询数据(json请求体模式)get{{host}}/20220325/_search?q=desc:运维
全量查询get{{host}}/20220325/_search
{ "query":{ "match":{ "desc":"运维" } } }
分页查询get{{host}}/20220325/_search
{ "query":{ "match_all":{ } } }
查询特定字段get{{host}}/20220325/_search
{ "query":{ "match_all":{ } }, "from":1,// 当前页 "size":2 //每页数量 }
对查询结果进行排序get{{host}}/20220325/_search
{ "query":{ "match_all":{ } }, "from":1,// 当前页 "size":2, //每页数量 "_source":["shoppingName"] }
多条件查询-andget{{host}}/20220325/_search
{ "from":0, "size":4, "sort":{ "price":{ "order":"desc" } }, "query":{ "match_all":{} } }
多条件查询-orget{{host}}/20220325/_search
{ "query":{ "bool":{ "must":[ { "match":{ "shoppingName":"fudaopin" } },{ "match":{ "desc":"运维" } }]//多条件必须同事成立 } } }
范围查询get{{host}}/20220325/_search
{ "query":{ "bool":{ "should":[ { "match":{ "shoppingName":"fudaopin" } }, { "match":{ "desc":"运维" } } ] } } }
全量匹配get{{host}}/20220325/_search
{ "from":0, "size":1, "query":{ "bool":{ "should":[], "filter":{ "range":{ "price":{ "gt":30 // 大于 30 } } } } } }
高亮显示get{{host}}/20220325/_search
{ "query":{ "match_phrase":{ "desc":"运维" } } }
聚合查询
get
{{host}}/20220325/_search
{ "query":{ "match_phrase":{ "shoppingName":"fudaopin" } }, "highlight":{ "fields":{ "shoppingName":{} } } }
去掉多余数据–只取聚合结果get{{host}}/20220325/_search
{ "aggs":{ "price_group":{//名称--随意 "terms":{// 分组关键字 "field":"price"//分组的字段 } } } }
求平均值get{{host}}/20220325/_search
{ "aggs":{ "price_group":{//名称--随意 "terms":{// 分组关键字 "field":"price"//分组的字段 } } }, "size":0 }
get{{host}/20220325/_search
{ "aggs":{ "price_avg":{//名称--随意 "avg":{// 分组关键字 "field":"price"//分组的字段 } } }, "size":0 }
##最近正在做一个搜索的项目,后续会继续更新Es相关技术



