温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用SpringBoot如何实现对ElasticSearch进行整合

发布时间:2020-11-19 16:40:58 来源:亿速云 阅读:201 作者:Leah 栏目:编程语言

这篇文章给大家介绍使用SpringBoot如何实现对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{
 /**
 * 作者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提供了一些注解来帮助我们快速针对实体建立索引。

使用SpringBoot如何实现对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);
 }
 
}

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

使用SpringBoot如何实现对ElasticSearch进行整合

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

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

关于使用SpringBoot如何实现对ElasticSearch进行整合就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI