ES
一、创建映射字段的语法格式
需要先构建索引库,在构建索引库中的映射关系
PUT /索引库名/_mapping
{"properties": {"字段名": {"type": "类型","index": true,"store": false,"analyzer": "分词器"}}
}
#新增数据 id自增
POST /hl/_doc
{"title":"小米手机","images":"http://image.lano.com/12479122.jpg","price":2699.00
}#自己指定id信息
POST /hl/_doc/2
{"title":"OPPO手机","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#修改数据
POST /hl/_doc/2
{"title":"VIVO手机","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#智能判断 根据数据自动添加到映射,判断并指定数据类型
POST /hl/_doc/3
{"title":"超米手机","images":"http://image.lanou.com/12479122.jpg","price":2899.00,"stock": 200,"saleable":true
}put /hl/_doc/4
{"title":"小米电视AAA","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#查询数据
GET hl/_search
{"query": {"match_all": {}}
}#or查询数据 小米or电视
GET hl/_search
{"query": {"match": {"title": "小米电视"}}
}
# and查询
GET hl/_search
{"query": {"match": {"title": {"query": "小米电视","operator": "and"}}}
}PUT /hl/_doc/5
{"title":"乐视电视","images":"http://image.lanou.com/12479122.jpg","price":2899.00,"subTitle":"小米电视手机"
}#多字段查询
GET hl/_search
{"query": {"multi_match": {"query": "小米","fields": [ "title", "subTitle" ]}}
}
#单词条精准查询
GET /hl/_search
{"query":{"term":{"price":2699.00}}
}
#多词条精准查询
GET /hl/_search
{"query":{"terms":{"price":[2699.00,2899.00]}}
}
#只查询特定字段
GET /hl/_search
{"_source": ["title","price"],"query": {"term": {"price": 2699}}
}
#只查询特定字段 指定includes和excludes
GET hl/_search
{
"_source": {
"excludes": "images",
"includes": ["title","price"]
},
"query": {
"term": {
"price": 2899.00
}
}
}PUT /hl/_doc/6
{"title":"apple手机","images":"http://image.lanou.com/12479122.jpg","price":6899.00
}
#模糊半径为1查询
GET hl/_search
{"query": {"fuzzy": {"title": "app"}}
}#模糊半径为2查询
GET hl/_search
{"query": {"fuzzy": {"title": {"value": "app22","fuzziness": 2}}}
}
-
类型名称:映射的名称,字段名:任意填写。Elasticsearch7.0之后不支持类名名称写法所以需要添加include_type_name=true参数进行支持设置。
-
type:类型,可以是text、long、short、date、integer、object等
-
index:是否可以使用索引查询,默认为true
-
store:是否额外存储,默认为false
-
analyzer:分词器,这里的
ik_max_word
即使用ik分词器
二、了解数据类型
1、字符串
text: 可分词 不可聚合
keyword:不可分词 可聚合
2、数值
整数和浮点(float、double、half_float、scaled_float)
3、日期
date
三、使用springboot创建es项目
1、jar包
spring-boot-starter-data-elasticsearch
2、配置文件
spring:
elasticsearch:
uris: http://1.94.230.82:9200
3、使用esTemplate模版工具类
@RestController
@RequestMapping("/es")
public class EsController {@Autowiredprivate ElasticsearchRestTemplate restTemplate;
四、Es实现的功能
1、创建索引库
restTemplate.indexOps(User.class).create();
/*
@Document(indexName = "索引库名",shards = 分片数,replicas = 副本数)*/
@Document(indexName = "user",shards = 1,replicas = 0)
public class User {
}
package com.hl.es.web;
import com.hl.es.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/es")
public class EsController {@Autowiredprivate ElasticsearchRestTemplate restTemplate;
@RequestMapping("/test")public void getEsTemplate(){boolean flag = restTemplate.indexOps(User.class).exists();System.out.println("索引库是否存在:"+flag);if(!flag){//创建索引库boolean flag2 = restTemplate.indexOps(User.class).create();System.out.println("索引库创建结果:"+flag2);}}
}
2、创建映射
@Document(indexName = "user",shards = 1,replicas = 0)
@Data
public class User {@Idprivate Integer id;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String username;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String desc;@Field(type = FieldType.Keyword,index = false)private String password;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String address;@Field(type = FieldType.Double)private Double money;@Field(type = FieldType.Date,format = DateFormat.custom,pattern = "YYYY-MM-dd")private Date createTime;
}
@RequestMapping("/createMapping")
public Object createMapping(){Document document = restTemplate.indexOps(User.class).createMapping();boolean flag = restTemplate.indexOps(User.class).putMapping(document);System.out.println("创建映射:"+flag);return flag;
}
3、新增数据
@RequestMapping("/save")
public Object save(User user){User user2 = restTemplate.save(user);return user2;
}
4、查询数据
自定义查询
package com.hl.es.dao;
import com.hl.es.pojo.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface UserDaoextends ElasticsearchRepository<User, Integer> {//根据用户名查询集合//单字段public List<User> findByUsername(String username);public List<User> findByAddress(String address);//多字段public List<User> findByDescAndAddress(String desc, String address);public List<User> findByDescOrAddress(String desc, String address);//范围查询public List<User> findAllByIdGreaterThanEqual(Integer minId);public List<User> findByMoneyBetween(Double minPrice, Double maxPrice);//先根据范围查询,再降序排序public List<User> findByMoneyBetweenOrderByMoneyDesc(Double minPrice, Double maxPrice);
}
高级查询
1、分页查询
2、聚合查询
3、高亮查询
//高亮处理
@Highlight(fields = {@HighlightField(name = "desc"),@HighlightField(name = "address"),@HighlightField(name = "username")},parameters = @HighlightParameters(preTags = {"<span style='color:red'>"},postTags = {"</span>"})
)
public List<SearchHit<User>> findByDescOrAddressOrUsername(String desc, String address, String username);
五、es专业名词
节点
分片
副本
索引
六、mysq+es的数据同步
-
使用Logstash实现Mysql与ElasticSearch实时同步。
-
使用go-mysql-elasticsearch实现Mysql与ElasticSearch实时同步。
-
使用RabbitMQ实现Mysql与ElasticSearch实时同步。
-
使用阿里巴巴Canal实现Mysql与ElasticSearch实时同步。
当前项目
主数据库: mysql 事务机制
辅助数据库:redis (小数据量的频繁访问 内存 首页商品分类 推荐商品)
es(大量数据的检索 模糊查询 | 数据来自于多个字段 百度搜索 商城的搜索)
mysql:商品表
redis:查询mysql中的数据,新增到redis
mysql------------------》es
方法(saveToMysql -->mq--> saveToEs )(updateToMysql -->mq--->updateToEs ) (deleteToMysql deleteToEs)
项目上线:首次同步,批量同步
项目运行:增量同步