SpringBoot整合ElasticSearch实践

网友投稿 432 2023-05-13


SpringBoot整合ElasticSearch实践

本节我们基于一个发表文章的案例来说明SpringBoot如何elasticsearch集成。elasticsearch本身可以是一个独立的服务,也可以嵌入我们的web应用中,在本案例中,我们讲解如何将elasticsearch嵌入我们的应用中。

一、实体设计:

Tutorial.java

public class Tutorial implements Serializable{

private Long id;

private String name;//教程名称

//setters and getters

//toString

}

Author.java

public class Author implements Serializable{

/**

*/

private Long id;

/**

*/

private String name;

/**

*/

private String remark;

//setters and getters

//toString

}

Article.java

public class Article implements Serializable{

private Long id;

/**标题*/

private String title;

/**摘要*/

private String abstracts;

/**内容*/

private String content;

/**发表时间*/

private Date postTime;

/**点击率*/

private Long clickCount;

private Author author;

/**所属教程*/

private Tutorial tutorial;

//setters and getters

//toString

}

二、整合SpringBoot与ElasticSearch

1、引入相应的依赖

pom.xml

org.springframework.boot

spring-boot-starter-parent

1.3.0.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-data-elasticsearch

org.springframework.boot

spring-boot-starter-test

2、修改配置文件

application.yml

spring:

data:

elasticsearch:

cluster-name: #默认为elasticsearch

cluster-nodes: #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode

properties:

path:

logs: ./elasticsearch/log #elasticsearch日志存储目录

data: ./elasticsearch/data #elasticsearch数据存储目录

这些配置的属性,最终会设置到ElasticsearchProperties这个实体中。

3、为实体添加ElascticSearch的注解

Spring-data-elasticSearch提供了一些注解来帮助我们快速针对实体建立索引。

因为我们希望Article作为我们文章的搜索入口,所以我们在Articlhttp://e类上添加@Document注解。

@Document(indexName="projectname",type="article",indexStoreType="fs",shards=5,replicas=1,refreshInterval="-1")

public class Article implements Serializable{

....

}

默认情况下,添加@Document注解会对实体中的所有属性建立索引,由于本教程是讲解如何整合,并不是专门讲解ElasticSearch,故对于其他注解不再讲解。

4、建立搜索类

我们只要编写一个接口ArticleSearchRepository,来继承Spring-data-elasticSearch提供的ElasticsearchRepository即可。

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

import com.tianshouzhi.springbootstudy.domain.Article;

//泛型的参PHYtOt数分别是实体类型和主键类型

public interface ArticleSearchRepository extends ElasticsearchRepository

{

}

5、单元测试

5.1、创建索引

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes = Application.class)

public class ElasticSearchTest {

@Autowired

private ArticleSearchRepository articleSearchRepository;

@Test

public void testSaveArticleIndex(){

Author author=new Author();

author.setId(1L);

author.setName("tianshouzhi");

author.setRemark("java developer");

Tutorial tutorial=new Tutorial();

tutorial.setId(1L);

tutorial.setName("elastic search");

Article article =new Article();

article.setId(1L);

article.setTitle("springboot integreate elasticsearch");

article.setAbstracts("springboot integreate elasticsearch is very easy");

article.setTutorial(tutorial);

article.setAuthor(author);

article.setContent("elasticsearch based on lucene,"

+ "spring-data-elastichsearch based on elaticsearch"

+ ",this tutorial tell you how to integrete springboot with spring-data-elasticsearch");

article.setPostTime(new Date());

article.setClickCount(1L);

articleSearchRepository.save(article);

}

}

运行单元测试,项目根目录下出现:

说明我们索引已经创建成功。

5.2测试搜索:

@Test

public void testSearch(){

String queryString="springboot";//搜索关键字

QueryStringQueryBuilder builder=new QueryStringQueryBuilder(queryString);

Iterable

searchResult = articleSearchRepository.search(builder);

Iterator

iterator = searchResult.iterator();

while(iterator.hasNext()){

System.out.println(iterator.next());

}

}

运行单元测试,控制台输出

复制代码 代码如下:

Article [id=1, title=springboot integreate elasticsearch, abstracts=springboot integreate elasticsearch is very easy, content=elasticsearch based on lucene,spring-data-elastichsearch based on elaticsearch,this tutorial tell you how to integrete springboot with spring-data-elasticsearch, postTime=Sun Feb 21 16:01:37 CST 2016, clickCount=1, author=Author [id=1, name=tianshouzhi, remark=java developer], tutorial=Tutorial [id=1, name=elastiPHYtOtc search]]

说明搜索成功。读者可以尝试其他的搜索关键字进行搜索。

说明:以上方式是SpringBoot与ElasticSearch进行本地整合,即将ElasticSearch内嵌在应用,如果我们搭建了ElasticSearch集群,只需要将配置改为如下配置即可:

spring:

data:

elasticsearch:

cluster-nodes:115.28.65.149:9300 #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Netty学习教程之基础使用篇
下一篇:微信小程序新增的拖动组件movable
相关文章

 发表评论

暂时没有评论,来抢沙发吧~