SpringBoot 集成 Elasticsearch

一、版本

复制代码
spring-boot版本:2.3.7.RELEASE

Elasticsearch7.8.0

版本说明详见

二、Elasticsearch 下载和安装

Elasticsearch 下载
kibana下载
ik分词器下载

配置IK分词器

2.1 解压,在elasticsearch-7.8.0\plugins 路径下新建ik目录

2.2 将ik分词器解压放入ik目录

2.3 扩展词汇测试示例

2.3.1 ik/config 目录下新建custom.dic文件

2.3.2 编辑custom.dic文件,加入新词汇

注意:custom.dic文件内容的格式的编码为UTF-8格式编码,否则会导致扩展词汇失效。

2.3.3 打开 IKAnalyzer.cfg.xml 文件,将新建的 custom.dic 配置其中,如下图

2.4 编辑kibana.yml ,修改kibana配置

三、新建Springboot项目,整合Elasticsearch并测试

3.1 加入依赖,配置

shell 复制代码
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
shell 复制代码
spring: 
  main: 
    allow-bean-definition-overriding: true 
  elasticsearch:
    rest:
      uris: http://localhost:9200

3.2 新建实体类SkuEs

java 复制代码
@Data
@Document(indexName = "skues" ,shards = 3,replicas = 1)
public class SkuEs {

    // 商品Id= skuId
    @Id
    private Long id;

    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String keyword;

    @Field(type = FieldType.Integer, index = false)
    private Integer skuType;

    @Field(type = FieldType.Integer, index = false)
    private Integer isNewPerson;

    @Field(type = FieldType.Long)
    private Long categoryId;

    @Field(type = FieldType.Text)
    private String categoryName;

    @Field(type = FieldType.Keyword, index = false)
    private String imgUrl;

    //  es 中能分词的字段,这个字段数据类型必须是 text!keyword 不分词!
    @Field(type = FieldType.Text)
    private String title;

    @Field(type = FieldType.Double)
    private Double price;

    @Field(type = FieldType.Integer, index = false)
    private Integer stock;

    @Field(type = FieldType.Integer, index = false)
    private Integer perLimit;

    @Field(type = FieldType.Integer, index = false)
    private Integer sale;

    @Field(type = FieldType.Long)
    private Long wareId;

    //  商品的热度!
    @Field(type = FieldType.Long)
    private Long hotScore = 0L;

    @Field(type = FieldType.Object, index = false)
    private List<String> ruleList;

}

3.2 新建接口SkuRepository

java 复制代码
public interface SkuRepository extends ElasticsearchRepository<SkuEs,Long> {


}

3.3 测试

java 复制代码
@Service
public class SkuServiceImpl implements SkuService {

    @Autowired
    private SkuRepository skuRepository;

    @Override
    public void upperSku(Long skuId) {
        //  3 调用方法添加ES
        SkuEs skuEs = new SkuEs();
        skuEs.setCategoryId(11111l);
        skuEs.setCategoryName("name");
        skuEs.setId(1123l);
        skuEs.setKeyword("keyword");
        skuEs.setWareId(1123l);
        skuEs.setIsNewPerson(12);
        skuEs.setImgUrl("url");
        skuEs.setTitle("SkuName");
        skuEs.setSkuType(0);
        skuEs.setPrice(123.1);
        skuEs.setStock(11);
        skuEs.setSale(21);
        skuEs.setPerLimit(123);

        skuRepository.save(skuEs);
    }
}

3.4 运行结果

打开kibana控制台:http://localhost:5601/app/kibana#/dev_tools/console

相关推荐
我真的是大笨蛋18 分钟前
MVCC解析
java·数据库·spring boot·sql·mysql·设计模式·设计规范
秃头续命码农人20 分钟前
谈谈对Spring、Spring MVC、SpringBoot、SpringCloud,Mybatis框架的理解
java·spring boot·spring·mvc·maven·mybatis
ahauedu22 分钟前
SpringBoot 3.5.10引入springdoc-openapi-starter-webmvc-ui版本
java·spring boot·后端
会游泳的石头33 分钟前
构建企业级知识库智能问答系统:基于 Java 与 Spring Boot 的轻量实现
java·开发语言·spring boot·ai
澄风43 分钟前
Redis ZSet+Lua脚本+SpringBoot实战:滑动窗口限流方案从原理到落地
spring boot·redis·lua
是梦终空1 小时前
计算机毕业设计263—基于Springboot+Vue的影视推荐和评分系统(源代码+数据库)
spring boot·vue·毕业设计·课程设计·协同过滤算法·影评系统·影视推荐系统
一 乐1 小时前
在线考试|基于springboot + vue在线考试系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
GeminiJM1 小时前
亿级向量检索:Elasticsearch vs. Milvus,性能鸿沟与架构抉择
elasticsearch·架构·milvus
qq_12498707531 小时前
基于spring boot的调查问卷系统的设计与实现(源码+论文+部署+安装)
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
小冷coding2 小时前
【ES】 Elasticsearch在电商系统中的核心应用场景与实践案例
大数据·elasticsearch·搜索引擎