SpringBoot整合ElasticSearch实践
本节我们基于一个发表文章的案例来说明SpringBoot如何elasticsearch集成。elasticsearch本身可以是一个独立的服务,也可以嵌入我们的web应用中,在本案例中,我们讲解如何将elasticsearch嵌入我们的应用中。
案例背景:每个文章(Article)都要属于一个教程(Tutorial),而且每个文章都要有一个作者(Author)。
一、实体设计:
Tutorial.java
public class Tutorial implements Serializable{ private Long id; private String name;//教程名称 //setters and getters //toString }
Author.java
public class Author implements Serializable{ /** * 作者id */ 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
<parent> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 1.3.0.RELEASE </version> </parent> <dependencies> <!-- 添加 web 应用的依赖 --> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-web </artifactId> </dependency> <!-- 添加 spring-data-elasticsearch的依赖 --> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-data-elasticsearch </artifactId> </dependency> <dependency> <groupId> org.springframework.boot</groupId> <artifactId> spring-boot-starter-test </artifactId> </dependency> </dependencies>
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作为我们文章的搜索入口,所以我们在Article类上添加@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; //泛型的参数分别是实体类型和主键类型 public interface ArticleSearchRepository extends ElasticsearchRepository<Article, Long>{ }
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<Article> searchResult = articleSearchRepository.search(builder); Iterator<Article> 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=elastic search]]
说明搜索成功。读者可以尝试其他的搜索关键字进行搜索。
说明:以上方式是SpringBoot与ElasticSearch进行本地整合,即将ElasticSearch内嵌在应用,如果我们搭建了ElasticSearch集群,只需要将配置改为如下配置即可:
spring: data: elasticsearch: cluster-nodes:115.28.65.149:9300 #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。