数据格式
Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档。
为了方便大家理解,我们将 Elasticsearch 里存储文档数据和关系型数据库 MySQL 存储数据的概念进行一个类比
ES 里的 Index 可以看做一个库,而 Types 相当于表, Documents 则相当于表的行。这里 Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个type, Elasticsearch 7.X 中, Type 的概念已经被删除了。
创建索引
PUT http://localhost:9200/shopping
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "shopping"
}
查看当前索引
GET http://localhost:9200/shopping
{
"shopping": {//索引名
"aliases": {},//别名
"mappings": {},//映射
"settings": {//设置
"index": {//设置 - 索引
"creation_date": "1650959730840",//设置 - 索引 - 创建时间
"number_of_shards": "1",//设置 - 索引 - 主分片数量
"number_of_replicas": "1",//设置 - 索引 - 主分片数量
"uuid": "Y2GAupofQFiHc5nsAlDe1w",//设置 - 索引 - 主分片数量
"version": {//设置 - 索引 - 主分片数量
"created": "7080099"
},
"provided_name": "shopping"//设置 - 索引 - 主分片数量
}
}
}
}
查看当前ES中所有的索引
GET http://localhost:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open shopping Y2GAupofQFiHc5nsAlDe1w 1 1 0 0 208b 208b
表头 | 含义 |
---|---|
health | 当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) |
status | 索引打开、关闭状态 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主分片数量 |
rep | 副本数量 |
docs.count | 可用文档数量 |
docs.deleted | 文档删除状态(逻辑删除) |
store.size | 主分片和副分片整体占空间大小 |
pri.store.size | 主分片占空间大小 |
删除索引
DELETE http://localhost:9200/shopping
{
"acknowledged": true
}
增加文档
POST http://localhost:9200/shopping/_doc
请求体JSON内容为:
{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
注意,此处发送请求的方式必须为 POST,不能是 PUT,否则会发生错误 。
返回结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "k63tZIAB6CX3m_v3vfQI",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
上面的数据创建后,由于没有指定数据唯一性标识(ID),默认情况下, ES 服务器会随机生成一个。
如果想要自定义唯一性标识,需要在创建时指定: http://127.0.0.1:9200/shopping/_doc/1001,这样就可以指定 ID 为 1001 。
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001", //自定义唯一ID标识
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为 PUT。
查询文档
通过主键ID查询文档:
GET http://localhost:9200/shopping/_doc/1001
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 3,
"_seq_no": 4,
"_primary_term": 1,
"found": true,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
}
查询全部文档:
GET http://localhost:9200/shopping/_search
{
"took": 2, //查询花费时长(毫秒)
"timed_out": false, // 请求是否超时
"_shards": { //搜索了多少分片,成功、失败或者跳过了多个分片(明细)
"total": 1, //总分片数
"successful": 1, //成功分片数
"skipped": 0, //失败分片数
"failed": 0 // 跳过分片数
},
"hits": {
"total": {
"value": 3, // 找到的文档总数
"relation": "eq"
},
"max_score": 1, 最相关的文档分数
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kq3nZIAB6CX3m_v3APT6",
"_score": 1, //文档的相关性算分 (match_all 没有算分)
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "k63tZIAB6CX3m_v3vfQI",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
}
]
}
}
更新文档
全局更新
POST http://localhost:9200/shopping/_doc/1001
请求JSON数据:
{
"title":"华为手机",
"category":"华为",
"images":"http://www.gulixueyuan.com/hw.jpg",
"price":1999.00
}
返回结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 10,
"result": "updated", //updated 表示数据被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 13,
"_primary_term": 1
}
局部更新
PUT http://localhost:9200/shopping/_update/1001
请求JSON数据:
{
"doc": {
"title":"小米手机",
"category":"小米"
}
}
返回结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 11,
"result": "updated", //updated 表示数据被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 14,
"_primary_term": 1
}
删除文档
DELETE http://localhost:9200/shopping/_doc/1001
返回结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 12,
"result": "deleted", // deleted 表示数据被删除
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 15,
"_primary_term": 1
}
查询文档
查询全部
GET http://localhost:9200/shopping/_search
返回结果数据:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kq3nZIAB6CX3m_v3APT6",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "k63tZIAB6CX3m_v3vfQI",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "l636ZIAB6CX3m_v3q_R5",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/hw.jpg",
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "ma37ZIAB6CX3m_v3BvQc",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/hw.jpg",
"price": 1999
}
}
]
}
}
按条件查询(URL带参查询)
GET http://localhost:9200/shopping/_search?q=category:小米
返回结果:
{
"took": 19,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.8889232,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kq3nZIAB6CX3m_v3APT6",
"_score": 1.8889232,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "k63tZIAB6CX3m_v3vfQI",
"_score": 1.8889232,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
}
]
}
}
按条件查询(请求体带参查询)
POST http://localhost:9200/shopping/_search
请求体:
{
"query": {
"match": {
"category": "小米"
}
}
}
返回结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 0.99798226,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kq3nZIAB6CX3m_v3APT6",
"_score": 0.99798226,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "k63tZIAB6CX3m_v3vfQI",
"_score": 0.99798226,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "n60KZYAB6CX3m_v3q_Tc",
"_score": 0.99798226,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "oK0KZYAB6CX3m_v3rfQs",
"_score": 0.99798226,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "oa0KZYAB6CX3m_v3rvS_",
"_score": 0.99798226,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "oq0KZYAB6CX3m_v3sPQR",
"_score": 0.99798226,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "o60KZYAB6CX3m_v3sfT8",
"_score": 0.99798226,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
}
]
}
}
查询全部数据(请求体带参查询)
POST http://localhost:9200/shopping/_search
请求体:
{
"query": {
"match_all": {}
}
}
等同于下面此接口
GET http://127.0.0.1:9200/shopping/_search
返回结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 9,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "kq3nZIAB6CX3m_v3APT6",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "k63tZIAB6CX3m_v3vfQI",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "l636ZIAB6CX3m_v3q_R5",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/hw.jpg",
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "ma37ZIAB6CX3m_v3BvQc",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": "http://www.gulixueyuan.com/hw.jpg",
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "n60KZYAB6CX3m_v3q_Tc",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "oK0KZYAB6CX3m_v3rfQs",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "oa0KZYAB6CX3m_v3rvS_",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "oq0KZYAB6CX3m_v3sPQR",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "o60KZYAB6CX3m_v3sfT8",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
}
]
}
}