当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot整合ElasticSearch的示例代码

SpringBoot整合ElasticSearch的示例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

elasticsearch作为基于lucene的搜索服务器,既可以作为一个独立的服务部署,也可以签入web应用中。springboot作为spring家族的全新框架,使得使用springboot开发spring应用变得非常简单。本文要介绍如何整合elasticsearch与springboot。

实体设计:

每一本书(book)都属于一个分类(classify),都有一个作者(author)。

生成这个三个实体类,并实现其get和set方法。

springboot配置修改:

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。

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

spring:
  data:
    elasticsearch: 
      cluster-name: #默认为elasticsearch
      cluster-nodes: #配置es节点信息,逗号分隔,如果没有指定,则启动clientnode
      properties:
        path:
         logs: ./elasticsearch/log #elasticsearch日志存储目录
         data: ./elasticsearch/data #elasticsearch数据存储目录

3.为实体添加elascticsearch注解

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

因为我们希望article作为我们文章的搜索入口,所以我们在article类上添加@document注解。

@document(indexname="projectname",type="article",indexstoretype="fs",shards=5,replicas=1,refreshinterval="-1")
public class book 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 booksearchrepository extends elasticsearchrepository<article, long>{

}

5.创建索引及搜索测试

创建索引

@runwith(springjunit4classrunner.class)
@springapplicationconfiguration(classes = application.class)
public class elasticsearchtest {

  @autowired
  private booksearchrepository articlesearchrepository;
  @test
  public void testsavearticleindex(){
    //初始化实体类,建立索引
    ......
  }

}

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

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

搜索测试

@test
  public void testsearch(){
    string querystring="springboot";//搜索关键字
    querystringquerybuilder builder=new querystringquerybuilder(querystring);
    iterable<book> searchresult = articlesearchrepository.search(builder);
    iterator<book> 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,
搜索结果

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

说明:

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网