
目录
[1.1 映射关系 (mappings)](#1.1 映射关系 (mappings))
[1.2 映射关系查看](#1.2 映射关系查看)
[2.1 删除文档](#2.1 删除文档)
[2.2 更新文档](#2.2 更新文档)
[2.3 批量操作](#2.3 批量操作)
[🍹三、高级查询(Query 检索)](#🍹三、高级查询(Query 检索))
🍸前言
继本地完成 Elasticsearch , 浏览器插件, kibana 管理工具,IK 分词器 安装之后,初步了解了 ES 的基本数据 以及 IK 分词器的操作原理,下面再来学习下其他的基本属性,文章链接如下:
【Elasticsearch<一>✈️✈️】简单安装使用以及各种踩坑-CSDN博客
【Elasticsearch<二>✈️✈️】基本属性概念与MySQL数据库的不同之处-CSDN博客
🍻一、索引
1.1 映射关系 (mappings)
示例:创建一个索引并指定索引的映射规则,意思是指创建一个索引 testmapping ,该索引有四个字段,分别是 "keyword" 类型的 "title" 字段,"double" 类型的 "price" 字段,"date" 类型的 "create_time" 字段,...,后续再插入数据的时候,如果数据格式不符合向指定的类型则报错,报错如下:
还有更多映射字段类型,可以参考官网
PUT /testmapping
{
"mappings": {
"properties": {
"title":{
"type": "keyword"
},
"price":{
"type": "double"
},
"create_time":{
"type": "date"
},
"description":{
"type": "text"
}
}
}
}


1.2 映射关系查看
使用 GET 命令,GET /索引名称/_mapping
GET /testmapping/_mapping

🍺二、文档
上篇文章使用了文档的基础操作,比如指定文档 id 生成用 put , 自动生成文档 id 使用 post
2.1 删除文档
使用 delete 方法,比如 : delete /索引名称/_doc/id
DELETE /testmapping/_doc/1

2.2 更新文档
更新文档有两种方式,一种是 删除原先的再插入,另一种是在原有基础上修改
2.2.1 先删后改
比如先创建一个文档,再指定相同 id 进行覆盖,结果如下
PUT /testmapping/_doc/1
{
"title":"海鲜类商品",
"price":888,
"create_time":"2024-04-28T13:19:00Z",
"description":"商品描述:这是海鲜类商品"
}
PUT /testmapping/_doc/1
{
"price":999
}

2.2.2 原有基础上修改
执行操作先插入一个文档,再指定文档 id 进行修改
PUT /testmapping/_doc/1
{
"title":"海鲜类商品",
"price":888,
"create_time":"2024-04-28T13:19:00Z",
"description":"商品描述:这是海鲜类商品"
}
POST /testmapping/_doc/1/_update
{
"doc":{
"price":666
}
}

2.3 批量操作
注:批量插入时,每条插入语句的成功与否不会影响到别的的数据插入以及结果展示,不会像数据库原子性操作那样,有错误全部回滚
POST /testmapping/_doc/_bulk
{"index":{"_id":"2"}}
{"title":"零食类商品","price":222,"create_time":"2024-04-28T13:19:00Z","description":"商品描述:这是零食类商品"}
{"index":{"_id":"3"}}
{"title":"运动类商品","price":222,"create_time":"2024-04-28T13:19:00Z","description":"商品描述:这是运动类商品"}


🍹三、高级查询(Query 检索)
3.1. 查询全部文档 【match_all】
结果如下:
GET /testmapping/_search
{
"query":{
"match_all": {}
}
}

3.2 关键字查询 【term】
GET /testmapping/_search
{
"query":{
"term":{
"title":{
"value": "服饰类"
}
}
}
}
3.3 范围查询【range】
用于指定查询范围内的文档,比如查询 商品价格位于 666-888之间的商品
GET /testmapping/_search
{
"query":{
"range":{
"price":{
"gte": 600,
"lte":888
}
}
}
}

3.4 前缀查询【prefix】
用来检索含有指定关键词的文档,比如查询前缀为"零食类"的相关文档
GET /testmapping/_search
{
"query":{
"prefix":{
"title":{
"value":"零食类"
}
}
}
}

3.5 多 文档 id 查询 【ids】
用来查询一组 id 对应的文档,比如获取文档 id 为 1和2 的文档
GET /testmapping/_search
{
"query":{
"ids":{
"values": [1,2]
}
}
}

3.6 布尔查询【bool】
布尔查询相当于组合多个条件实现复杂查询
must : 多个条件要同时成立,相当于 &&
should : 相当于 || 有一个成立就行
must_not : 相当于 ! 都不能满足
比如 should 查询 title 字段为 "零食类商品" 或者 "运动类商品"
GET /testmapping/_search
{
"query":{
"bool":{
"should": [
{"terms": {
"title": [
"零食类商品",
"运动类商品"
]
}}
]
}
}
}
3.7 高亮查询 【highlight】
3.7.1 使用 highlight 关键字,通过对结果添加高亮标签实现,比如查询出 title 为"运动类商品"的文档并给 title 标注为高亮
GET /testmapping/_search
{
"query": {
"match": {
"title": "运动类商品"
}
},
"highlight": {
"fields": {
"*":{}
}
}
}

3.7.2 自定义高亮标签
pre_tags : 前置标签
post_tags : 后置标签
GET /testmapping/_search
{
"query": {
"match": {
"title": "运动类商品"
}
},
"highlight": {
"pre_tags": ["<span style='color:red'>"],
"post_tags": ["</span>"],
"fields": {
"*":{}
}
}
}

3.8 返回指定的条数
类似于 SQL 中的 limit 关键字,比如所有查询结果中,只取一条
GET /testmapping/_search
{
"query" : {
"match_all":{}
},
"size":1
}

3.9 分页查询【from】
from 关键字指定当前从第几页开始,比如 size 为1, from 1 就是指总共数据的第二条
3.10 排序【sort】
类似于 MySQL 中的 order by,指定根据某个字段排序,正向排序: asc , 倒向排序 : desc
比如说:按照 price 价格 倒排,也就是说价格从高到低排列
GET /testmapping/_search
{
"query" : {
"match_all":{}
},
"sort":[
{
"price":{
"order":"desc"
}
}]
}

3.11 指定查询字段 【source】
类似于 MySQL 指定返回的字段,比如:基于上面的排序数据,只返回 "price" 和 "title" 这两个字段的值
GET /testmapping/_search
{
"query" : {
"match_all":{}
},
"sort":[
{
"price":{
"order":"desc"
}
}],
"_source":["price","title"]
}

🍷四、章末
文章到这里就结束了~
