安装ElasticSearch
官方下载地址:https://www.elastic.co/downloads/elasticsearch
解压:elasticsearch-5.2.2.zip
启动:bin目录 elasticsearch.bat
config目录中jvm.options
占用内存大小设置
-Xms1g
-Xmx1g
验证:访问http://localhost:9200/
安装IK分词器
https://github.com/medcl/elasticsearch-analysis-ik
解压elasticsearch-analysis-ik-5.2.2.zip里面的东西放到ElasticSearch的plugins/ik中
测试分词器:
测试分词器
POST _analyze
{
“analyzer”:“ik_smart”,
“text”:“在全文检索理论中,文档的查询是通过关键字查询文档索引来进行匹配”
}
注意:IK分词器有两种类型,分别是ik_smart分词器和ik_max_word分词器。
ik_smart: 会做最粗粒度的拆分。
ik_max_word: 会将文本做最细粒度的拆分。
安装Kibana
可视化工具:kibana客户端
Kibana5.2.2下载地址:https://www.elastic.co/downloads/kibana
解压:kibana-5.2.2-windows-x86.zip
启动:bin目录kibana.bat ----自动连上ElasticSearch 127.0.0.1:9200
访问:http://localhost:5601
SpringBoot集成ElasticSearch
1、导包
<!--springboot 对spring data es支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、application.yml
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300 #9200是图形界面端,9300代码端
3、文档对象
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
//查询展示的列,查询条件的列,排序的列
@Document(indexName = "hrm",type = "course")
public class CourseDoc {
//文档id,数据id
@Id
private Long id;
//标题
@Field(type =FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Keyword)
private String gradeName;
}
4、自定义接口继承ElasticsearchRepository
就相当于Dao接口
import org.springframework.data.elasticsearch.