Springboot 整合 ElasticSearch 入门教学必看

https://pan.baidu.com/s/1jvHtoBcH65NW7KMz-XZqPw

提取码:6jg6

下载完解压,然后进到bin目录,运行elasticsearch.bat 即可,

2.运行后:

第二步


开始创建springboot项目,

创建完后,记得把springboot版本改成2.X版本,这里我使用的是2.2.0 RELEASE

然后是在pom.xml文件加入需要用到的依赖包,

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-data-elasticsearch

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

然后是application.yml 文件:

spring:

data:

elasticsearch:

cluster-nodes: 127.0.0.1:9300

server:

port: 8066

创建一个Blog.class 类,用于测试例子的数据的存取:

注意 这里的@Document(indexName = “testdata”, type = “blogs”)

indexName  索引名称,其实相当于咱们的数据库名称 ,必须为小写, 不然会报org.elasticsearch.indices.InvalidIndexNameException异常

type:类型 ,其实相当于咱们的数据库表的名称

import lombok.Data;

import org.springframework.data.elasticsearch.annotations.Document;

/**

  • @Author : JCccc

  • @CreateTime : 2020/3/13

  • @Description :

**/

@Data

@Document(indexName = “testdata”, type = “blogs”)

public class Blog {

private Long id;

private String masterName;

private Integer articleNum;

private Integer commentNum;

private Integer thumbNum;

private String description;

}

接着创建一个DataTestController.class,写一些测试的接口:

import com.example.elastucsearchdemo.pojo.Blog;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import org.spring
framework.data.elasticsearch.core.query.IndexQuery;

import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

/**

  • @Author : JCccc

  • @CreateTime : 2020/3/13

  • @Description :

**/

@RestController

public class DataTestController {

@Autowired

private ElasticsearchTemplate elasticsearchTemplate;

@GetMapping(“/addTest”)

public String testElSearch() {

//该list用于添加需要存入的数据

List indexQueryList = new ArrayList<>();

//模拟一些数据

Blog blog = new Blog();

blog.setId((long) new Random().nextInt(1500));

blog.setMasterName(“JCccc”);

blog.setArticleNum(10);

blog.setCommentNum(29);

blog.setThumbNum(100);

blog.setDescription(“分享不仅为了别人,也是为了自己”);

//把这个数据放入indexQueryList

indexQueryList.add(new IndexQueryBuilder().withObject(blog).build());

//循环模拟一些数据

for (int i = 1; i <= 6; i++) {

Blog blog2 = new Blog();

blog2.setId((long) new Random().nextInt(1500));

blog2.setMasterName(“Test”);

blog2.setArticleNum(i*60);

blog2.setCommentNum(i*16);

blog2.setThumbNum(i*500);

blog2.setDescription(“测试添加”+i);

indexQueryList.add(new IndexQueryBuilder().withObject(blog2).build());

}

elasticsearchTemplate.bulkIndex(indexQueryList);

return "add success ";

}

}

运行项目,调用一下这个插入数据的模拟接口:

添加完毕了,但是到底是否真的添加进去了呢? 接下来暂时使用 elasticsearchTemplate提供的查询方法来验证下(后面会有相关的ElasticHD可视化客户端下载安装,用来看数据)

写个查询接口:

@GetMapping(“/getTestData”)

public List getTestData(){

//精确查询

SearchQuery searchQuery = new NativeSearchQueryBuilder()

.withQuery(QueryBuilders.matchPhraseQuery(“masterName”, “JCccc”))

.build();

List list = elasticsearchTemplate.queryForList(searchQuery, Blog.class);

return list;

}

调用下接口看下,查询正常:

那么接下来我们来增加多一些数据,进行模糊查询,

//该list用于添加需要存入的数据

List indexQueryList = new ArrayList<>();

//循环模拟一些数据

for (int i = 1; i <= 3; i++) {

Blog blog2 = new Blog();

blog2.setId((long) new Random().nextInt(1500));

blog2.setMasterName(“Test”+i23);

blog2.setArticleNum(i*60);

blog2.setCommentNum(i*16);

blog2.setThumbNum(i*500);

blog2.setDescription(“测试添加”+i);

indexQueryList.add(new IndexQueryBuilder().withObject(blog2).build());

}

elasticsearchTemplate.bulkIndex(indexQueryList);

然后在验证下这个模糊搜索:

@GetMapping(“/queryTestData”)

public List getTestData(){

SearchQuery searchQuery = new NativeSearchQueryBuilder()

.withQuery(QueryBuilders.wildcardQuery(“masterName”, (“" + “Test” + "”).toLowerCase()))

.build();

List list = elasticsearchTemplate.queryForList(searchQuery, Blog.class);

return list;

}

再调用一下,就可以看到模糊查询出所有包含Test的数据了。

那么假设已经知道了id,单个数据查询又是如何操作呢?如下,单个根据id查询,

@GetMapping(“/queryTestDataOne”)

public String queryTestDataOne(){

GetQuery query = new GetQuery();

query.setId(“114”);

Blog blog = elasticsearchTemplate.queryForObject(query, Blog.class);

return blog.toString();

}

单个查询结果:

那么假如我们不知道主键id,我们只知道一些条件,例如想查询 点赞数  thumbNum =1000的 数据,

也就是单条件查询:

这时候我们需要实现自定义查询类ElasticsearchOptionSearchRepository.class,

import com.example.elastucsearchdemo.pojo.Blog;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import org.springframework.stereotype.Repository;

/**

  • @Author : JCccc

  • @CreateTime : 2020/3/13

  • @Description :

**/

@Repository

public interface ElasticsearchOptionSearchRepository extends ElasticsearchRepository<Blog,String> {

}

然后写个接口:

@Autowired

private ElasticsearchOptionSearchRepository elasticsearchOptionSearchRepository;

//根据单条件查询

@GetMapping(“/queryTestDataCondition”)

public List queryTestDataCondition(){

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值