ElasticSearch是基于Lucene框架的全文搜索引擎,是文档型数据库,索引(Index)定义了文档的逻辑存储和字段类型,文档类型是文档的集合,文档以索引定义的逻辑存储模型。
ElasticSearch的对象模型,本质也是存储数据,很多概念与MySQL类似的。
-
索引(Index):相当于MySQL 实例中的一个 Database,用于定义文档类型的存储;在同一个索引中,同一个字段只能定义一个数据类型;
-
文档类型(Type):相当于MySQL 中的 Table,描述文档中的各个字段的定义,ElasticSearch 7.0 以后已经废弃;
-
文档(Document):相当于MySQL 中的Row,存储数据,包含一个或多个存有数据的字段;
-
字段(Field):相当于MySQL 中的的Column,文档的一个属性;
-
映射配置(mappings):字段的数据类型、属性、是否索引、是否存储等特性
注意:从 ElasticSearch 7.0 开始,Type 被废弃!!!
在 7.0 以及之后的版本中,类型(Type)被废弃了。一个 index 中只有一个默认的 type,默认为_doc。索引(Index)既可以被认为对应 MySQL 的Database,也可以认为对应 table。
ES 实例:对应 MySQL 实例中的一个 Database。
Index 对应 MySQL 中的 Table 。
Document 对应 MySQL 中表的记录。
Elasticsearch采用RESTful API的调用风格,在管理和使用ElasticSearch服务时,常用的HTTP动词有下面五个:
-
GET 请求:获取对象
-
POST 请求:更新对象
-
PUT 请求:创建对象
-
DELETE 请求:删除对象
-
HEAD 请求:获取对象基础信息
一、索引(Index)
1.1、创建索引
-
settings:索引库的设置
-
number_of_replicas:副本数量
-
number_of_shards:分片数量
-
1、创建索引,不指定分片和副本信息
PUT /product
2、创建索引并指定分片和副本信息
PUT /product
{
"settings": {
"index":{
"number_of_shards":3,
"number_of_replicas":0
}
}
}
1.2、查看索引
1、查看所有索引
GET /_cat/indices/*?v&s=index
2、查看索引信息
GET /product
3、查看索引配置
GET /product/_settings
4、查看所有索引配置
GET /_all/_settings
1.3、删除索引
1、删除索引
DELETE /product
二、映射(Mapping)
-
String类型,分为:
-
text:可分词,不可参与聚合
-
keyword:不可分词,数据会作为完整字段进行匹配,可以参与聚合
-
-
Numerical:数值类型,分两类
-
基本数据类型:long、interger、short、byte、double、float、half_float
-
浮点数的高精度类型:scaled_float
-
需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
-
-
-
Date:日期类型
Elasticsearch可以对日期格式化为字符串存储,但是建议我们存储为毫秒值,存储为long,节省空间。
2.1、创建映射(Mapping)
2.1.1、Elasticsearch 8 先创建索引,再定义映射(Mapping)
1、创建索引
PUT /product
{
"settings": {
"index":{
"number_of_shards":3,
"number_of_replicas":0
}
}
}
2、创建映射的索引
PUT /product
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text",
"index": true
},
"desc": {
"type": "text",
"index": true
},
"price": {
"type": "double"
},
"brand": {
"type": "keyword",
"index": true
},
"create_time": {
"type": "date",
"index": true,
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
2.1.2、Elasticsearch8创建索引并指定mapping和settings
PUT /product
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text",
"index": true
},
"desc": {
"type": "text",
"index": true
},
"price": {
"type": "double"
},
"brand": {
"type": "keyword",
"index": true
},
"create_time": {
"type": "date",
"index": true,
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
2.2、查看映射
1、查看映射信息
GET /product/_mapping
三、文档(Document)
3.1、添加文档
3.1.1、POST方式添加文档
POST 用于更新数据,如果不存在,则会创建,如果存在,则更新。
1、指定id方式添加文档
PUT /product/_doc/10001
{
"id": 10001,
"name": "苹果iPhone 16 Pro Max",
"desc": "苹果iPhone 16 Pro Max(A3297)256GB 黑色钛金属 支持移动联通电信5G 双卡双待手机",
"brand": "苹果",
"price": 9999.00,
"create_time":"2024-09-10 00:00:00"
}
2、不指定id方式添加文档
POST /product/_doc/
{
"id": 10002,
"name": "华为Mate 70",
"desc": "华为Mate 70 12GB+512GB雪域白鸿蒙AI 红枫原色影像 超可靠玄武架构华为鸿蒙智能手机",
"brand": "华为",
"price": 5999.00,
"create_time":"2024-11-26 00:00:00"
}
3.1.2、PUT方式添加文档
PUT添加文档,如果不存在,则创建,如果存在,则更新,PUT方式添加文档,必须指定id。
PUT /product/_doc/10003
{
"id": 10003,
"name": "小米14Ultra",
"desc": "小米14Ultra 徕卡光学Summilux镜头 双向卫星通信 第三代骁龙8 16+512 黑色",
"brand": "小米",
"price": 5999.00,
"create_time":"2024-02-22 00:00:00"
}
3.1.3、_create方式创建文档
_create方式创建文档,必须指定id,如果id存在,则创建失败
POST /product/_create/10004
{
"id": 10004,
"name": "小米15Pro",
"desc": "小米15Pro 徕卡光学Summilux高速镜头 骁龙8至尊版移动平台 徕卡潜望长焦 16+512 白色",
"brand": "小米",
"price": 5699.00,
"create_time":"2024-10-29 00:00:00"
}
3.2、查询文档
1、根据id查询文档
GET /product/_doc/10001
2、查询文档部分属性
GET /product/_doc/10001?_source=name,price
3.3、更新文档
1、POST覆盖更新文档
POST /product/_doc/10002
{
"id": 10002,
"name": "华为Mate 70",
"desc": "华为Mate 70 12GB+512GB雪域白鸿蒙AI 红枫原色影像 超可靠玄武架构华为鸿蒙智能手机",
"brand": "华为",
"price": 5999.00,
"create_time":"2024-11-26 00:00:00"
}
2、部分更新文档部分字段
POST /product/_update/10004
{
"doc":{
"id": 4,
"name": "红米Redmi 10A"
}
}
3.4、删除文档
1、根据id删除文档
DELETE /product/_doc/10004