Elasticsearch 快速入门
Elasticsearch 基本操作
基本概念
集群,节点,索引,类型,文档,分片和副本
基本操作
# 查看分片大小
GET _cat/shards
# 查看集群运行状况
GET _cat/health?v
# 查看集群中的节点列表
GET _cat/nodes?v
# 查询集群中的索引
GET _cat/indices?v
elasticsearch支持restApi调用,执行http请求即可执行方法调用
索引(index)
创建索引
新建一个index:goods
curl -X PUT 'localhost:9200/goods'
对应的elasticsearch语法为:
# 创建索引(goods)
PUT /goods
响应为:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "goods"
}
查询index
curl -X GET 'localhost:9200/goods'
删除index
curl -X DELETE 'localhost:9200/goods'
document(文档,数据)
新增数据
向指定的/goods/book发送请求
- goods:index
- book:type
- 1:id
PUT /goods/book/1
{
"title":"elastic",
"name":"elasticsearch学习笔记文档记录",
"img":"img3.pic.com/56/8555.jpg",
"price":"253.00"
}
指定id创建文档,id就是自定义的,如果不指定id,则会随机生成一个。
不指定id,创建数据
POST /goods/book
{
"title":"elastic2",
"name":"elasticsearch学习笔记文档记录",
"img":"img3.pic.com/56/8555.jpg",
"price":"253.00"
}
响应:
{
"_index" : "goods",
"_type" : "book",
"_id" : "YU10yHoBrDuES6WZBDrL",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
添加数据时,如果index不存在,就会创建对应的index
查看记录
curl localhost:9200/goods/book/1?pretty=true
{
"_index" : "goods",
"_type" : "book",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "elastic",
"name" : "elasticsearch学习笔记文档记录",
"img" : "img3.pic.com/56/8555.jpg",
"price" : "253.00"
}
}
删除数据
curl -X DELETE localhost:9200/goods/book/1
更新数据
PUT /goods/book/1
{
"title":"elastic33",
"name":"elasticsearch学习笔记文档记录",
"img":"img3.pic.com/56/8555.jpg",
"price":"253.00"
}
{
"_index" : "goods",
"_type" : "book",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
变更后,_version会+1
查询所有记录
curl localhost:9200/goods/book/_search
条件查询(全查)
GET /dang-dang-goods/_search
{
"query": {"match_all": {}}
}
分页查询
- size 查询数量
- from 偏移量
如果不指定size,默认为10
GET /goods/_search
{
"query": {"match_all": {}},
"from": 10,
"size": 5
}
排序
text类型不支持排序
对price进行倒排序
GET /goods/_search
{
"query": {"match_all": {}},
"sort": [
{
"price": {
"order": "desc"
}
}
],
"from": 0,
"size": 10
}
返回特定字段
类似于SQL中的 SELECT sku,title FROM ...
只查询title,sku
GET goods/_search
{
"query": {"match_all": {}},
"_source": ["title","sku"]
}
条件查询(匹配)
查询title中包含背包的数据
GET /goods/_search
{
"query": {"match": {
"title": "背包"
}}
}
查询title中包含背包或spring的数据
GET /goods/_search
{
"query": {"match": {
"title": "背包 spring"
}}
}
布尔查询 (&&)
sku=1822023502 && price=¥79.00
GET /goods/_search
{
"query": {
"bool": {
"must": [
{"match": {"sku":"1822023502"}},
{"match": {"price":"¥79.00"}}
]
}
}
}
布尔查询 (||)
sku=1822023502 || price=¥79.00
GET /goods/_search
{
"query": {
"bool": {
"should": [
{"match": {"sku":"1822023502"}},
{"match": {"price":"¥79.00"}}
]
}
}
}
布尔查询 (!)
查询 !( sku=1822023502) && !(price=¥79.00)
GET /goods/_search
{
"query": {
"bool": {
"must_not": [
{"match": {"sku":"1822023502"}},
{"match": {"price":"¥79.00"}}
]
}
}
}
条件查询(范围过滤)
查询price>100的数据
GET /goods/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": [
{
"range": {
"price": {
"gte": 100
}
}
}
]
}
}
}
统计查询(分组)
根据price分组
GET /goods/_search
{
"size": 2,
"aggs": {
"group_by_price": {
"terms": {
"field": "price",
"size": 100
}
}
}
}
聚合查询(平均数)
按time求平均数
GET /goods/_search
{
"size": 2,
"aggs": {
"group_by_price": {
"terms": {
"field": "price",
"size": 100
},
"aggs": {
"average_time": {
"avg": {
"field": "time"
}
}
}
}
}
}
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步