目录:
1.安装Elasticsearch
2.Elasticsearch概念
3.Elasticsearch索引
4.Elasticsearch查询
5.Elasticsearch聚合aggregations
6.Spring Data Elasticsearch入门
7.Repository文档操作
一、创建索引(工具kibana)
1、创建索引
语法
- 请求方式:PUT
- 请求路径:/索引库名
- 请求参数:json格式:
- settings:索引库的设置
- number_of_shards:分片数量
- number_of_replicas:副本数量
示例
PUT dxbindex1
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
返回结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "dxbindex1"
}
2、查看索引
GET dxbindex1
返回结果
{
"dxbindex1": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1574905917216",
"number_of_shards": "3",
"number_of_replicas": "2",
"uuid": "sh9lXvtoQ3aVu0vCfVk6sQ",
"version": {
"created": "6020499"
},
"provided_name": "dxbindex1"
}
}
}
}
3、删除索引
DELETE dxbindex2
返回结果
{
"acknowledged": true
}
4、映射配置
4.1、创建映射字段
语法
PUT /索引库名/_mapping/类型名称
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}
- 类型名称:就是前面讲的type的概念,类似于数据库中的不同表
字段名:任意填写 ,可以指定许多属性,例如: - type:类型,可以是text、keyword、long、short、date、integer、object等
- index:是否索引,默认为true
- store:是否存储,默认为false
- analyzer:分词器,这里的
ik_max_word
是ik分词器的一种
示例
PUT dxbindex1/_mapping/goods
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword",
"index": "false"
},
"price": {
"type": "float"
}
}
}
返回结果
{
"acknowledged": true
}
4.2、查看映射关系
语法
GET /索引库名/_mapping
示例
GET /dxbindex1/_mapping
返回结果
{
"dxbindex1": {
"mappings": {
"goods": {
"properties": {
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "float"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
}
5、新增数据
5.1、随机生成id
语法
POST /索引库名/类型名
{
"key":"value"
}
示例
POST /dxbindex1/goods/
{
"title":"小米手机",
"images":"1.jpg",
"price":2699.00
}
返回结果
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "69HktG4BCtNGGQZP1ZsF",
"_version": 1,
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 3
}
说明:
_source
:源文档信息,所有的数据都在里面。_id
:这条文档的唯一标示,与文档自己的id字段没有关联
5.2、自定义id
语法
POST /索引库名/类型名/id值
{
"key":"value"
}
示例
POST /dxbindex1/goods/1
{
"title":"大米手机",
"images":"2.jpg",
"price":2899.00
}
返回结果
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 3
}
5.3、智能判断
不需要给索引库设置任何mapping映射,它也可以根据你输入的数据来判断类型,动态添加数据映射。
示例
POST /dxbindex1/goods/3
{
"title":"超米手机",
"images":"3.jpg",
"price":2999.00,
"stock": 200,
"saleable":true
}
返回结果
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 3
}
查看映射关系
GET /dxbindex1/_mapping
返回结果
{
"dxbindex1": {
"mappings": {
"goods": {
"properties": {
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "float"
},
"saleable": {
"type": "boolean"
},
"stock": {
"type": "long"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
}
6、修改数据
请求方式改为PUT,修改必须指定id。
- id对应文档存在,则修改
- id对应文档不存在,则新增
示例
PUT /dxbindex1/goods/1
{
"title":"超大米手机",
"images":"2.jpg",
"price":3899.00,
"stock": 100,
"saleable":true
}
返回结果
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 3
}
7、删除数据
语法
DELETE /索引库名/类型名/id值
示例
DELETE dxbindex1/goods/3
返回结果
{
"_index": "dxbindex1",
"_type": "goods",
"_id": "3",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 4
}