SpringBoot项目集成全文搜索引擎Elasticsearch

 

首先安装 Elasticsearch和Kibana

Download Elasticsearch | Elastic       Download Kibana Free | Get Started Now | Elastic

导入相关maven依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

yml中配置相关参数

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 127.0.0.1:9300 #9200是图形界面端,9300代码端

创建文档对象

该文档对象可用来做如下几个事情

  • 索引库的创建
  • 文档的映射
  • 存储到ES的数据封装
/**
 * 针对于student表的文档映射
 * indexName:索引库
 * type:类型(表类型)
 */
@Document(indexName = "stu" , type = "student")
public class StudentDoc {

    //对应文档的id  PUT  /index/type/id
    @Id
    private Long id;

    //指定为 不分词
    @Field(type = FieldType.Keyword)    
    private String userName;

    private int age;

    //指定为 分词
    @Field(type =FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String intro;
   

初始化索引库和文档映射

@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsServiceApplication.class)
public class ElasticsearchTest {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Test
    public void testCreateIndex() {
        //创建索引
        elasticsearchTemplate.createIndex(StudentDoc.class);
        //做文档映射
        elasticsearchTemplate.putMapping(StudentDoc.class);
    }
}

到此为止基础的集成就已经完成,可以通过Kibana测试索引是否创建成功。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会理财的程序员不是好摄影师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值