准备工作
安装ElasticSearch
安装分词器analyzer
安装可视化插件elasticSearch-head
可从网上资源下载安装即可,我这里演示环境为6.2.4
核心依赖
<!--SpringBoot启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--实体支持依赖-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
配置文件
注意:我这里是配置了集群elasticSearch,如需集群搭建请参考以下步骤
- 赋值三份ElasticSearch,改变端口即可,核心代码如下
elasticSearch 三份配置文件根据该ip端口依次修改
#允许跨域访问
http.cors.enabled: true
#任何请求都有权限访问
http.cors.allow-origin: /.*/
#集群名称
cluster.name: my-elasticsearch
#版本
node.name: node-1
#服务器ip
network.host: 127.0.0.1
#服务器端口
http.port: 9201
#集群节点单服务器端口
transport.tcp.port: 9301
#集群下属ip服务,顺序最前表示默认9301
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9301","127.0.0.1:9302","127.0.0.1:9303"]
SpringBoot核心配置文件
server:
port: 8765
spring:
application:
name: elastic-server
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/myservers?characterEncoding=utf-8
# sql is update
jpa:
hibernate:
ddl-auto: update
show-sql: true
data:
elasticsearch:
cluster-name: my-elasticsearch
cluster-nodes: 127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303
eureka:
instance:
hostname: localhost
defaultZone:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
基本操作步骤
创建索引库实体
ElasticSearch会根据实体属性字段映射索引库并完成创建
//indexName:索引名称
//type:文档名称
@Document( indexName = "indexname",type = "myindex")
//jdk1.8 lombok插件
@Data
public class MyNameList {
//主键
@Id
//字段属性支持,
// value:对应字段名称,store:返回响应数据时是否返回默认true
@Field(value = "id",store = true)
private Long id;
//type:字段类型Text为文本
//analyzer:分词器,ik_max_word更细制分词查询,ik_smart精准查询
@Field(value = "name", type = FieldType.Text, analyzer = "ik_max_word",store = true)
private String name;
@Field(value = "soudu", type = FieldType.Text, analyzer = "ik_max_word",store = true)
private String soudu;
根据实体创建索引库并映射
SpringBoot-ElasticSearch提供类
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
方法:
创建索引
elasticsearchTemplate.createIndex(MyNameList.class);
//映射索引属性类型
elasticsearchTemplate.putMapping(MyNameList.class);
索引库CRUD
定义接口继承操作ElasticSearch的提供类
public interface XwLElasticDao extends ElasticsearchRepository<MyNameList,Long> {
}
@Autowired
private XwLElasticDao xwLElasticDao;
@Autowired
private JdbcTemplate jdbcTemplate;
MyNameList myNameList = new MyNameList();
myNameList.setId(21l);
myNameList.setName("张");
int x = (int) (Math.random() * 50) + 1;
myNameList.setSoudu(String.valueOf(x));
xwLElasticDao.save(myNameList);
- 查看地址:http://localhost:9100/
- 数据浏览
- 删除索引
elasticsearchTemplate.deleteIndex(MyNameList.class)
- 修改索引
- elasticSearch修改是先进行删除然后再进行新增,所以在修改时根据id先删除当前内容,然后再新增最新数据即可
查询举例
需求:用户输入中文字符检索姓名并进行分页处理,在响应结果中只展示姓名,其它字段不进行显示
//封装查询条件
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
//分页
//分页 queryBuilder.withPageable(PageRequest.of(current<=1?0:current-1, size));
//过滤条件
MatchQueryBuilder matchQueryBuilder = null;
//匹配查找数据
matchQueryBuilder = QueryBuilders.matchQuery("name",nameCode);
queryBuilder.withQuery(matchQueryBuilder);
//只显示有关信息-姓名
queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{"name"}, null));
//查询
Page<MyNameList> search = xwLElasticDao.search(queryBuilder.build());
更多查询请参考官方文档~