博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot--整合Elasticsearch
阅读量:2442 次
发布时间:2019-05-10

本文共 5397 字,大约阅读时间需要 17 分钟。

引入依赖

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
pers.zhang
sb_elasticsearch
0.0.1-SNAPSHOT
sb_elasticsearch
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-test
test
junit
junit
4.12
org.springframework.boot
spring-boot-maven-plugin

配置

application.yml

spring:  data:    elasticsearch:      cluster-name: elasticsearch      cluster-nodes: 127.0.0.1:9300      repositories:        enabled: true

启动类

@SpringBootApplication@EnableElasticsearchRepositories(basePackages = "pers.zhang")public class SbElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(SbElasticsearchApplication.class, args); }}

实体类

package pers.zhang.sb_elasticsearch.entity;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;/** * @Author: acton_zhang * @Date: 2020/3/14 3:30 下午 * @Version 1.0 */@Document(indexName = "index-article", type = "article")public class Article {
@Id @Field(type = FieldType.Long, store = true) private Long id; @Field(type = FieldType.text, store = true, analyzer = "ik_smart") private String title; @Field(type = FieldType.text, store = true, analyzer = "ik_smart") private String content; public Long getId() {
return id; } public void setId(Long id) {
this.id = id; } public String getTitle() {
return title; } public void setTitle(String title) {
this.title = title; } public String getContent() {
return content; } public void setContent(String content) {
this.content = content; } @Override public String toString() {
return "Article{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + '}'; }}

Repository

public interface ArticleRepository extends ElasticsearchRepository
{
//根据标题进行查询 List
findByTitle(String title); List
findByTitleOrContent(String title, String context); List
findByTitleOrContent(String title, String context, Pageable pageable);}

测试

@SpringBootTest(classes = SbElasticsearchApplication.class)@RunWith(SpringJUnit4ClassRunner.class)public class SbElasticsearchApplicationTests {
@Autowired ArticleRepository articleRepository; @Autowired ElasticsearchTemplate elasticsearchTemplate; @Test public void createIndex() {
elasticsearchTemplate.createIndex(Article.class); } @Test public void addDocument () {
Article article = new Article(); article.setId(3l); article.setTitle("Maven对象模型"); article.setContent("佛教圣地六块腹肌塑料袋放假了撒"); articleRepository.save(article); } @Test //查询所有 public void findAll () {
Iterable
all = articleRepository.findAll(); all.forEach( a -> System.out.println(a)); } @Test //根据id查询 public void findById () {
Optional
byId = articleRepository.findById(1l); System.out.println(byId.get()); } @Test public void findByTitle () {
List
list = articleRepository.findByTitle("对象"); list.forEach( a -> System.out.println(a)); } @Test //多条件查询 public void findByTitleOrContent () {
List
list = articleRepository.findByTitleOrContent("ddd", "放假"); list.forEach( a -> System.out.println(a)); } @Test //分页查询 public void findByPage () {
Pageable pageable = PageRequest.of(0, 2); List
list = articleRepository.findByTitleOrContent("ddd", "放假", pageable); list.forEach( a -> System.out.println(a)); } //原生查询 @Test public void testNativeSearchQuery () {
NativeSearchQuery query = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.queryStringQuery("佛放假").defaultField("content")) .withPageable(PageRequest.of(0, 5)) .build(); List
articles = elasticsearchTemplate.queryForList(query, Article.class); articles.forEach(a -> System.out.println(a)); } @Test public void deleteById () { articleRepository.deleteById(1l); }}

转载地址:http://pspqb.baihongyu.com/

你可能感兴趣的文章
gpu驱动程序_如何从错误的GPU驱动程序更新中恢复
查看>>
esp now_Google带回Google Now(内部)排序助手
查看>>
如何防止视频在Chrome中自动播放
查看>>
如何使用Synology NAS下载文件(并避免在夜间开启计算机)
查看>>
使用批处理脚本上传ftp_通过批处理脚本将文件上传到FTP站点
查看>>
linux下如何更改主机名_如何在不重新启动的情况下更改Linux主机名
查看>>
pxe网络启动引导程序_如何使用PXE设置网络可引导实用程序光盘
查看>>
凌乱的yyy_如何清理凌乱的Internet Explorer上下文菜单
查看>>
Laravel Eloquent:API资源
查看>>
在React中使用Font Awesome 5
查看>>
React Hooks入门
查看>>
盖茨比乔布斯_用盖茨比快速浏览WordPress站点
查看>>
docker react_10分钟内即可实现具有安全性的React + Docker
查看>>
vue.js表单验证_Vue.js中的模板驱动表单验证
查看>>
软件测试结束标志_使用功能标志进行生产中的测试
查看>>
css网格_在CSS网格中放置,跨度和密度
查看>>
火狐动态调试css_使用Firefox开发工具调试CSS网格
查看>>
服务周期性工作内容_使服务工作者生命周期神秘化
查看>>
响应式屏幕_检测角度的响应式屏幕尺寸
查看>>
vue中使用vuex_使用Vuex在Vue中处理身份验证
查看>>