Elasticsearch 学习笔记_巨輪的博客-CSDN 博客
索引操作
创建索引
# PUT 创建索引 (重复请求,返回失败,只能创建一次)
http://127.0.0.1:9200/shopping
---
{
"acknowledged": true,//响应结果
"shards_acknowledged": true,//分片结果
"index": "shopping"//索引名称
}
查询所有索引
# GET 查询索引
http://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 J0WlEhh4R7aDrfIc3AkwWQ 1 1 0 0 208b 208b查询单个索引
# GET 查看单个索引
http://127.0.0.1:9200/shopping
---
{
"shopping": {//索引名
"aliases": {},//别名
"mappings": {},//映射
"settings": {//设置
"index": {//设置 - 索引
"creation_date": "1617861426847",//设置 - 索引 - 创建时间
"number_of_shards": "1",//设置 - 索引 - 主分片数量
"number_of_replicas": "1",//设置 - 索引 - 主分片数量
"uuid": "J0WlEhh4R7aDrfIc3AkwWQ",//设置 - 索引 - 主分片数量
"version": {//设置 - 索引 - 主分片数量
"created": "7080099"
},
"provided_name": "shopping"//设置 - 索引 - 主分片数量
}
}
}
}删除索引
# DELETE 删除索引
http://127.0.0.1:9200/shopping
---
{ "acknowledged": true }查看索引中,各属性详情:
- health —— 当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)|
- status —— 索引打开、关闭状态
- index —— 索引名
- uuid —— 索统一编号
- pri —— 主分片数量
- rep —— 副本数量
- docs.count —— 可用文档数量
- docs.deleted —— 文档删除状态(逻辑删除)
- store.size —— 主分片和副分片整体占空间大小
- pri.store.size —— 主分片占空间大小
文档操作
创建文档
POST 请求 : http://127.0.0.1:9200/shopping/_doc
# 发送数据
{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
# 接受数据
{
"_index": "shopping",//索引
"_type": "_doc",//类型-文档
"_id": "ANQqsHgBaKNfVnMbhZYU",//唯一标识,可以类比为 MySQL 中的主键,随机生成
"_version": 1,//版本
"result": "created",//结果,这里的 create 表示创建成功
"_shards": {//
"total": 2,//分片 - 总数
"successful": 1,//分片 - 总数
"failed": 0//分片 - 总数
},
"_seq_no": 0,
"_primary_term": 1
}创建文档(指定 ID)
# 指定 ID,可以PUT,也可以 POST
http://127.0.0.1:9200/shopping/_doc/1
# 发送数据
{
"title":"小米手机",
"category":"小米",
"images":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
# 接受数据
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",//<------------------自定义唯一性标识
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}查询文档
查询单一文档:查看文档时,需要指明文档的唯一性标识,类似于 MySQL 中数据的主键查询。
GET 请求 : http://127.0.0.1:9200/shopping/_doc/1
# 返回数据
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "http://www.gulixueyuan.com/xm.jpg",
"price": 3999
}
}
查看不存在的文档
GET 请求 : http://127.0.0.1:9200/shopping/_doc/1001
# 返回结果
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"found": false
}
查询所有文档
GET 请求 : http://127.0.0.1:9200/shopping/_search
# 返回结果
{
"took": 133,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "ANQqsHgBaKNfVnMbhZYU",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 3999
}
}
]
}
}
修改文档
修改文档本质上和新增文档是一样的,如果存在就修改,如果不存在就新增。
全量修改:
// POST 请求 : http://127.0.0.1:9200/shopping/_doc/1
---
{
"title":"华为手机",
"category":"华为",
"images":" http://www.gulixueyuan.com/hw.jpg" ,
"price":1999.00
}
---
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated",//<-----------updated 表示数据被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
局部修改:修改数据时,也可以只修改某一给条数据的局部信息。
// POST 请求 : http://127.0.0.1:9200/shopping/_update/1
---
{
"doc": {
"title":"小米手机",
"category":"小米"
}
}
---
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 3,
"result": "updated",//<-----------updated 表示数据被更新
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}删除文档
删除:删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。
// DELETE 请求 : http://127.0.0.1:9200/shopping/_doc/1
---
{
"_index": "shopping",
"_type": "_doc",
"_id": "1",
"_version": 4,
"result": "deleted",//<---删除成功
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}数据搜索
- URL 带参数查询
- 请求体带参数查询
- 带请求体方式的查找所有内容
- 带请求体方式的查找指定内容
查询所有文档
// GET 请求 : http://127.0.0.1:9200/shopping/_search
---
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "ANQqsHgBaKNfVnMbhZYU",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "A9R5sHgBaKNfVnMb25Ya",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BNR5sHgBaKNfVnMb7pal",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BtR6sHgBaKNfVnMbX5Y5",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "B9R6sHgBaKNfVnMbZpZ6",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "CdR7sHgBaKNfVnMbsJb9",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
}
]
}
}
匹配查询文档
URL 带参数查询:查找 category 为小米的文档。
// GET 请求 : http://127.0.0.1:9200/shopping/_search?q=category:小米
---
{
"took": 94,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "ANQqsHgBaKNfVnMbhZYU",
"_score": 1.3862942,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "A9R5sHgBaKNfVnMb25Ya",
"_score": 1.3862942,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BNR5sHgBaKNfVnMb7pal",
"_score": 1.3862942,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
}
]
}
}
请求体带参查询:查找 category 为小米的文档
// GET 请求 : http://127.0.0.1:9200/shopping/_search
---
{
"query":{
"match":{
"category":"小米"
}
}
}
---
结果同上带请求体方式的查找所有内容:查找所有文档内容
// GET 请求 : http://127.0.0.1:9200/shopping/_search
---
{
"query":{
"match_all":{}
}
}
---
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "ANQqsHgBaKNfVnMbhZYU",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "A9R5sHgBaKNfVnMb25Ya",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BNR5sHgBaKNfVnMb7pal",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BtR6sHgBaKNfVnMbX5Y5",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "B9R6sHgBaKNfVnMbZpZ6",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "CdR7sHgBaKNfVnMbsJb9",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
}
]
}
}
匹配查询字段
// GET 请求 : http://127.0.0.1:9200/shopping/_search
---
{
"query":{
"match_all":{}
},
"_source":["title"]
}
---
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "ANQqsHgBaKNfVnMbhZYU",
"_score": 1,
"_source": {
"title": "小米手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "A9R5sHgBaKNfVnMb25Ya",
"_score": 1,
"_source": {
"title": "小米手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BNR5sHgBaKNfVnMb7pal",
"_score": 1,
"_source": {
"title": "小米手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BtR6sHgBaKNfVnMbX5Y5",
"_score": 1,
"_source": {
"title": "华为手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "B9R6sHgBaKNfVnMbZpZ6",
"_score": 1,
"_source": {
"title": "华为手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "CdR7sHgBaKNfVnMbsJb9",
"_score": 1,
"_source": {
"title": "华为手机"
}
}
]
}
}聚合查询
聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的 group by,当然还有很多其他的聚合,例如取最大值 max、平均值 avg 等等。
分组查询
接下来按 price 字段进行分组:
GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"aggs":{//聚合操作
"price_group":{//名称,随意起名
"terms":{//分组
"field":"price"//分组字段
}
}
}
}
---
{
"took": 63,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "ANQqsHgBaKNfVnMbhZYU",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "A9R5sHgBaKNfVnMb25Ya",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BNR5sHgBaKNfVnMb7pal",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BtR6sHgBaKNfVnMbX5Y5",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "B9R6sHgBaKNfVnMbZpZ6",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "CdR7sHgBaKNfVnMbsJb9",
"_score": 1,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
}
]
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1999,
"doc_count": 5
},
{
"key": 3999,
"doc_count": 1
}
]
}
}
}
上面返回结果会附带原始数据的。若不想要不附带原始数据的结果,GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"aggs":{
"price_group":{
"terms":{
"field":"price"
}
}
},
"size":0
}
---
{
"took": 60,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1999,
"doc_count": 5
},
{
"key": 3999,
"doc_count": 1
}
]
}
}
}
平均值
若想对所有手机价格求平均值,GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"aggs":{
"price_avg":{//名称,随意起名
"avg":{//求平均
"field":"price"
}
}
},
"size":0
}
---
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_avg": {
"value": 2332.3333333333335
}
}
}
TopN 查询

分页查询
// GET 请求 : http://127.0.0.1:9200/shopping/_search
---
{
"query":{
"match_all":{}
},
"from":0,
"size":2
}
---
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "ANQqsHgBaKNfVnMbhZYU",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 3999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "A9R5sHgBaKNfVnMb25Ya",
"_score": 1,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
}
}
]
}
}
有序查询
GET 请求 : http://127.0.0.1:9200/shopping/_search
---
{
"query":{
"match_all":{}
},
"sort":{
"price":{
"order":"desc"
}
}
}
---
{
"took": 96,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "ANQqsHgBaKNfVnMbhZYU",
"_score": null,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 3999
},
"sort": [
3999
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "A9R5sHgBaKNfVnMb25Ya",
"_score": null,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
},
"sort": [
1999
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BNR5sHgBaKNfVnMb7pal",
"_score": null,
"_source": {
"title": "小米手机",
"category": "小米",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
},
"sort": [
1999
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "BtR6sHgBaKNfVnMbX5Y5",
"_score": null,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
},
"sort": [
1999
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "B9R6sHgBaKNfVnMbZpZ6",
"_score": null,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
},
"sort": [
1999
]
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "CdR7sHgBaKNfVnMbsJb9",
"_score": null,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
},
"sort": [
1999
]
}
]
}
}多条件查询
假设想找出小米牌子,价格为 3999 元的。(must 相当于数据库的&&)
GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"category": "小米"
}
},
{
"match": {
"price": 3999.0
}
}
]
}
}
}假设想找出小米和华为的牌子。(should 相当于数据库的||)
GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"category": "小米"
}
},
{
"match": {
"category": "华为"
}
}
]
},
"filter": {
"range": {
"price": {
"gt": 2000
}
}
}
}
}范围查询
假设想找出小米和华为的牌子,价格大于 2000 元的手机。
GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"category": "小米"
}
},
{
"match": {
"category": "华为"
}
}
],
"filter": {
"range": {
"price": {
"gt": 2000
}
}
}
}
}
}全文检索
这功能像搜索引擎那样,如品牌输入“小华”,返回结果带回品牌有“小米”和华为的。
GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"query": {
"match": {
"category": "小华"
}
}
}完全匹配
在 Postman 中,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"query": {
"match_phrase": {
"category": "为"
}
}
}高亮查询
GET 请求 : http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_phrase":{
"category" : "为"
}
},
"highlight":{
"fields":{
"category":{}//<----高亮这字段
}
}
}
---
{
"took": 100,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 0.6931471,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "BtR6sHgBaKNfVnMbX5Y5",
"_score": 0.6931471,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
},
"highlight": {
"category": [
"华<em>为</em>"//<------高亮一个为字。
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "B9R6sHgBaKNfVnMbZpZ6",
"_score": 0.6931471,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
},
"highlight": {
"category": [
"华<em>为</em>"
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "CdR7sHgBaKNfVnMbsJb9",
"_score": 0.6931471,
"_source": {
"title": "华为手机",
"category": "华为",
"images": " http://www.gulixueyuan.com/xm.jpg" ,
"price": 1999
},
"highlight": {
"category": [
"华<em>为</em>"
]
}
}
]
}
}
映射关系
有了索引库,等于有了数据库中的 database。接下来就需要建索引库(index)中的映射了,类似于数据库(database)中的表结构(table)。
创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)。
先创建一个索引
// PUT http://127.0.0.1:9200/user
---
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "user"
}
创建映射
// PUT http://127.0.0.1:9200/user/_mapping
---
{
"properties": {
"name":{
"type": "text",
"index": true
},
"sex":{
"type": "keyword",
"index": true
},
"tel":{
"type": "keyword",
"index": false
}
}
}
---
{
"acknowledged": true
}查询映射
// GET http://127.0.0.1:9200/user/_mapping
---
{
"user": {
"mappings": {
"properties": {
"name": {
"type": "text"
},
"sex": {
"type": "keyword"
},
"tel": {
"type": "keyword",
"index": false
}
}
}
}
}
增加映射
// PUT http://127.0.0.1:9200/user/_create/1001
---
{
"name":"小米",
"sex":"男的",
"tel":"1111"
}
---
{
"_index": "user",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}查找 name 含有”小“数据
// GET http://127.0.0.1:9200/user/_search
---
{
"query":{
"match":{
"name":"小"
}
}
}
---
{
"took": 495,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "1001",
"_score": 0.2876821,
"_source": {
"name": "小米",
"sex": "男的",
"tel": "1111"
}
}
]
}
}查找 sex 含有”男“数据
找不想要的结果,只因创建映射时”sex”的类型为”keyword”,“sex”只能完全为”男的“,才能得出原数据。
// GET http://127.0.0.1:9200/user/_search
{
"query":{
"match":{
"sex":"男"
}
}
}
---
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}查找 sex 含有”男的“数据
// GET http://127.0.0.1:9200/user/_search
{
"query":{
"match":{
"sex":"男的"
}
}
}
---
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.2876821,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "1001",
"_score": 0.2876821,
"_source": {
"name": "小米",
"sex": "男的",
"tel": "1111"
}
}
]
}
}
查询电话
报错只因创建映射时”tel”的”index”为 false。
// GET http://127.0.0.1:9200/user/_search
{
"query":{
"match":{
"tel":"11"
}
}
}
---
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
"index_uuid": "ivLnMfQKROS7Skb2MTFOew",
"index": "user"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "user",
"node": "4P7dIRfXSbezE5JTiuylew",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
"index_uuid": "ivLnMfQKROS7Skb2MTFOew",
"index": "user",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Cannot search on field [tel] since it is not indexed."
}
}
}
]
},
"status": 400
}
索引模板
我们之前对索引进行一些配置信息设置,但是都是在单个索引上进行设置。在实际开发中,我们可能需要创建不止一个索引,但是每个索引或多或少都有一些共性。比如我们在设计关系型数据库时,一般都会为每个表结构设计一些常用的字段,比如:创建时间,更新时间,备注信息等。elasticsearch 在创建索引的时候,就引入了模板的概念,你可以先设置一些通用的模板,在创建索引的时候,elasticsearch 会先根据你创建的模板对索引进行设置。elasticsearch 中提供了很多的默认设置模板,这就是为什么我们在新建文档的时候,可以为你自动设置一些信息,做一些字段转换等。
索引可使用预定义的模板进行创建,这个模板称作 Index templates。模板设置包括 settings 和 mappingselasticsearch 中提供了很多的默认设置模板,这就是为什么我们在新建文档的时候,可以为你自动设置一些信息,做一些字段转换等。
索引可使用预定义的模板进行创建,这个模板称作 Index templates。模板设置包括 settings 和 mappings。
创建模板
// 模板名称小写
PUT _template/mytemplate
{
"index_patterns" : [
"my*"
],
"settings" : {
"index" : {
"number_of_shards" : "1"
}
},
"mappings" : {
"properties" : {
"now": {
"type" : "date",
"format" : "yyyy/MM/dd"
}
}
}
}查看模板
// GET /_template/mytemplate
---
{
"mytemplate": {
"order": 0,
"index_patterns" : [
"my*"
],
"settings" : {
"index" : {
"number_of_shards" : "1"
}
},
"mappings" : {
"properties" : {
"now": {
"type" : "date",
"format" : "yyyy/MM/dd"
}
}
},
"aliases": {}
}
}验证模板存在
HEAD /_template/mytemplate创建索引
PUT mytest
---
{
"mytest": {
"aliases": {},
"mappings" : {
"properties" : {
"now": {
"type" : "date",
"format" : "yyyy/MM/dd"
}
}
},
"settings" : {
"index" : {
"routing": {
"allocation" : {
"include" : {
"_tier_preference": "data_content"
}
}
}
"number_of_shards" : "1"
// .....
}
},
}
}删除模板
DELETE /_template/mytemplate