2023-01-04 00:59:35 +08:00
|
|
|
|
# 基础索引库、文档ES-DSL语句
|
|
|
|
|
|
2023-01-04 23:43:35 +08:00
|
|
|
|
[TOC]
|
|
|
|
|
|
|
|
|
|
# 基础概念
|
2023-01-04 00:59:35 +08:00
|
|
|
|
|
|
|
|
|
我们把mysql与elasticsearch的概念做一下对比:
|
|
|
|
|
|
|
|
|
|
| **MySQL** | **Elasticsearch** | **说明** |
|
|
|
|
|
| --------- | ----------------- | ------------------------------------------------------------ |
|
|
|
|
|
| Table | Index | 索引(index),就是文档的集合,类似数据库的表(table) |
|
|
|
|
|
| Row | Document | 文档(Document),就是一条条的数据,类似数据库中的行(Row),文档都是JSON格式 |
|
|
|
|
|
| Column | Field | 字段(Field),就是JSON文档中的字段,类似数据库中的列(Column) |
|
|
|
|
|
| Schema | Mapping | Mapping(映射)是索引中文档的约束,例如字段类型约束。类似数据库的表结构(Schema) |
|
|
|
|
|
| SQL | DSL | DSL是elasticsearch提供的JSON风格的请求语句,用来操作elasticsearch,实现CRUD |
|
|
|
|
|
|
|
|
|
|
是不是说,我们学习了elasticsearch就不再需要mysql了呢?
|
|
|
|
|
|
|
|
|
|
并不是如此,两者各自有自己的擅长支出:
|
|
|
|
|
|
|
|
|
|
- Mysql:擅长事务类型操作,可以确保数据的安全和一致性
|
|
|
|
|
|
|
|
|
|
- Elasticsearch:擅长海量数据的搜索、分析、计算
|
|
|
|
|
|
|
|
|
|
因此在企业中,往往是两者结合使用:
|
|
|
|
|
|
|
|
|
|
- 对安全性要求较高的写操作,使用mysql实现
|
|
|
|
|
- 对查询性能要求较高的搜索需求,使用elasticsearch实现
|
|
|
|
|
- 两者再基于某种方式,实现数据的同步,保证一致性
|
|
|
|
|
|
2023-01-04 23:43:35 +08:00
|
|
|
|
# 基础语法
|
2023-01-04 00:59:35 +08:00
|
|
|
|
|
2023-01-04 23:43:35 +08:00
|
|
|
|
## 索引基础语法
|
2023-01-04 00:59:35 +08:00
|
|
|
|
|
|
|
|
|
- 创建索引库:PUT /索引库名
|
|
|
|
|
- 查询索引库:GET /索引库名
|
|
|
|
|
- 删除索引库:DELETE /索引库名
|
|
|
|
|
- 添加字段:
|
|
|
|
|
- PUT /索引库名/_mapping
|
|
|
|
|
- POST /索引库名/_mapping
|
|
|
|
|
|
2023-01-04 23:43:35 +08:00
|
|
|
|
## 文档基础语法
|
2023-01-04 00:59:35 +08:00
|
|
|
|
|
|
|
|
|
- 创建文档:POST /{索引库名}/_doc/文档id { json文档 }
|
|
|
|
|
- 查询文档:GET /{索引库名}/_doc/文档id
|
|
|
|
|
- 删除文档:DELETE /{索引库名}/_doc/文档id
|
|
|
|
|
- 修改文档:
|
|
|
|
|
- 全量修改 (如果索引库中不存在则新增):PUT /{索引库名}/_doc/文档id { json文档 }
|
|
|
|
|
- 局部修改(如果索引库中不存在则修改失败):POST /{索引库名}/_update/文档id { "doc": {字段}}
|
|
|
|
|
|
2023-01-04 23:43:35 +08:00
|
|
|
|
# 示例
|
2023-01-04 00:59:35 +08:00
|
|
|
|
|
|
|
|
|
```apl
|
|
|
|
|
#分词器
|
|
|
|
|
#默认分词器
|
|
|
|
|
GET _analyze
|
|
|
|
|
{
|
|
|
|
|
"analyzer": "standard",
|
|
|
|
|
"text": "hello world"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ik_max_word ik分词器-细粒度
|
|
|
|
|
GET _analyze
|
|
|
|
|
{
|
|
|
|
|
"analyzer": "ik_max_word",
|
|
|
|
|
"text": "我是云南的嘤嘤怪,你是大傻逼还是大聪明"
|
|
|
|
|
}
|
|
|
|
|
#ik_smart ik分词器-粗粒度
|
|
|
|
|
GET _analyze
|
|
|
|
|
{
|
|
|
|
|
"analyzer": "ik_smart",
|
|
|
|
|
"text": "我是云南的"
|
|
|
|
|
}
|
|
|
|
|
#ik_max_word
|
|
|
|
|
GET _analyze
|
|
|
|
|
{
|
|
|
|
|
"analyzer": "ik_max_word",
|
|
|
|
|
"text": "我是云南的嘤嘤怪,你是大傻逼还是大聪明"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 索引库操作
|
|
|
|
|
#新增索引
|
|
|
|
|
PUT /heima
|
|
|
|
|
{
|
|
|
|
|
"mappings": {
|
|
|
|
|
"properties": {
|
|
|
|
|
"info":{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"analyzer": "ik_smart"
|
|
|
|
|
},
|
|
|
|
|
"email":{
|
|
|
|
|
"type": "keyword",
|
|
|
|
|
"index": false
|
|
|
|
|
},
|
|
|
|
|
"name":{
|
|
|
|
|
"properties": {
|
|
|
|
|
"firstName":{
|
|
|
|
|
"type":"keyword"
|
|
|
|
|
},
|
|
|
|
|
"lastName":{
|
|
|
|
|
"type":"keyword"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#查询索引
|
|
|
|
|
GET /heima
|
|
|
|
|
|
|
|
|
|
#修改索引(为索引新增加字段)
|
|
|
|
|
POST /heima/_mapping
|
|
|
|
|
{
|
|
|
|
|
"properties":{
|
|
|
|
|
"name2":{
|
|
|
|
|
"type":"keyword"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#修改索引(为索引新增加字段)
|
|
|
|
|
PUT /heima/_mapping
|
|
|
|
|
{
|
|
|
|
|
"properties":{
|
|
|
|
|
"name3":{
|
|
|
|
|
"type":"keyword"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#删除索引
|
|
|
|
|
DELETE /heima
|
|
|
|
|
|
|
|
|
|
#文档操作
|
|
|
|
|
#新增文档
|
|
|
|
|
POST /heima/_doc/1
|
|
|
|
|
{
|
|
|
|
|
"info":"大聪明JAVA程序员",
|
|
|
|
|
"email":"2111319672@qq.com",
|
|
|
|
|
"name":{
|
|
|
|
|
"firstName":"代",
|
|
|
|
|
"lastName":"帅"
|
|
|
|
|
},
|
|
|
|
|
"name2":"大聪明"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#查询文档
|
|
|
|
|
GET /heima/_doc/1
|
|
|
|
|
|
|
|
|
|
#删除文档
|
|
|
|
|
DELETE /heima/_doc/1
|
|
|
|
|
|
|
|
|
|
#全量修改
|
|
|
|
|
PUT /heima/_doc/1
|
|
|
|
|
{
|
|
|
|
|
"info":"大聪明JAVA程序员",
|
|
|
|
|
"email":"2111319672@qq.com",
|
|
|
|
|
"name":{
|
|
|
|
|
"firstName":"代",
|
|
|
|
|
"lastName":"帅"
|
|
|
|
|
},
|
|
|
|
|
"name2":"大聪明"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#局部修改
|
|
|
|
|
POST /heima/_update/1
|
|
|
|
|
{
|
|
|
|
|
"doc": {
|
|
|
|
|
"email":"dcm@qq.com",
|
|
|
|
|
"name":{
|
|
|
|
|
"firstName":"大",
|
|
|
|
|
"lastName":"聪明"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#常见字段演示
|
|
|
|
|
# location:地理坐标,里面包含精度、纬度
|
|
|
|
|
# all:一个组合字段,其目的是将多字段的值 利用copy_to合并,提供给用户搜索
|
|
|
|
|
PUT /hotel
|
|
|
|
|
{
|
|
|
|
|
"mappings": {
|
|
|
|
|
"properties": {
|
|
|
|
|
"id": {
|
|
|
|
|
"type": "keyword"
|
|
|
|
|
},
|
|
|
|
|
"name":{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"analyzer": "ik_max_word",
|
|
|
|
|
"copy_to": "all"
|
|
|
|
|
},
|
|
|
|
|
"address":{
|
|
|
|
|
"type": "keyword",
|
|
|
|
|
"index": false
|
|
|
|
|
},
|
|
|
|
|
"price":{
|
|
|
|
|
"type": "integer"
|
|
|
|
|
},
|
|
|
|
|
"score":{
|
|
|
|
|
"type": "integer"
|
|
|
|
|
},
|
|
|
|
|
"brand":{
|
|
|
|
|
"type": "keyword",
|
|
|
|
|
"copy_to": "all"
|
|
|
|
|
},
|
|
|
|
|
"city":{
|
|
|
|
|
"type": "keyword",
|
|
|
|
|
"copy_to": "all"
|
|
|
|
|
},
|
|
|
|
|
"starName":{
|
|
|
|
|
"type": "keyword"
|
|
|
|
|
},
|
|
|
|
|
"business":{
|
|
|
|
|
"type": "keyword"
|
|
|
|
|
},
|
|
|
|
|
"location":{
|
|
|
|
|
"type": "geo_point"
|
|
|
|
|
},
|
|
|
|
|
"pic":{
|
|
|
|
|
"type": "keyword",
|
|
|
|
|
"index": false
|
|
|
|
|
},
|
|
|
|
|
"all":{
|
|
|
|
|
"type": "text",
|
|
|
|
|
"analyzer": "ik_max_word"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|