springboot之Elasticsearch(二)

本文介绍如何在SpringBoot项目中配置并使用Elasticsearch,包括依赖设置、实体类定义、CRUD操作及控制层示例。适用于初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明

在上一篇springboot之Elasticsearch(一)对Elasticsearch有了初步的了解,并学会了安装,这里我能将学习springboot中如何使用;
注意:Elasticsearch版本不要太高,目前springboot支持的Elasticsearch-client最高版本为6.4.3,建议安装Elasticsearch服务端版本7.0以下

依赖

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--实体工具包-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.2</version>
		</dependency>
		<!--集合工具包-->
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>19.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

配置文件(没有配置集群,都写的默认值)

#cluster-name要与elasticsearch.yml中使用的集群名称一致
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.repositories.enabled=true

实体类

/**
 * @auther 高松
 * @DATE 2019/8/10  11:59
 * demo-elasticsearch
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
//indexName代表所以名称,type代表表名称
@Document(indexName = "wantu_notice_info", type = "doc")
public class Notice {

    @Id
    @JsonProperty("id")
    private Long id;

    //标题
    @JsonProperty("title")
    private String title;

    //公告标签
    @JsonProperty("exchangeMc")
    private String exchangeMc;

    //公告发布时间
    @JsonProperty("originCreateTime")
    private String originCreateTime;

    //公告阅读数量
    @JsonProperty("readCount")
    private Integer readCount;

}

基础crud操作类(与spring-data-jpa类似)

@Component
public interface NoticeRepository extends ElasticsearchRepository<Notice, Long> {

}

crud控制层demo

/**
 * @auther 高松
 * @DATE 2019/8/10  12:04
 * demo-elasticsearch
 */

@RestController
public class NoticeController {
    @Autowired
    private NoticeRepository nticeRepository;
    @RequestMapping("save")
    public String save(@RequestParam("id") long id,@RequestParam("title") String title){
        Notice article = new Notice();
        article.setId(id);
        article.setReadCount(123);
        article.setTitle(title);
        nticeRepository.save(article);
        return "ok";
    }

    /**
     * @param title   搜索标题
     * @param pageable page = 第几页参数, value = 每页显示条数
     */
    @RequestMapping("search")
    public List<Notice> search(@RequestParam("title")String title,@RequestParam("page")  int page,@RequestParam("size")  int size ,@PageableDefault(page =0, size = 2) Pageable pageable){
        //按标题进行搜索
        QueryBuilder queryBuilder = QueryBuilders.matchPhrasePrefixQuery("title", title);
        //如果实体和数据的名称对应就会自动封装,pageable分页参数
        Pageable pageable1 = PageRequest.of(page, size, Sort.Direction.DESC,"id");
        Iterable<Notice> listIt =  nticeRepository.search(queryBuilder,pageable1);
        Iterable<Notice> all = nticeRepository.findAll();
        //Iterable转list
        List<Notice> list= Lists.newArrayList(listIt);


        //构造查询器.多条件查询
/*        NativeSearchQueryBuilder qb = new NativeSearchQueryBuilder();
        qb.withFilter(。。。)
                .withPageable(pageable1)
                .withSort(。。。);
        Page<Notice> search = nticeRepository.search(qb.build());
        List<Notice> content = search.getContent();*/
        return list;
    }

    @RequestMapping("del")
    public String del(@RequestParam("id") long id){
        nticeRepository.deleteById(id);
        return "ok";
    }
    @RequestMapping("update")
    public String update(@RequestParam("id") long id){
        Optional<Notice> byId = nticeRepository.findById(id);
        nticeRepository.delete(byId.get());
        return "ok";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值