欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot整合Elasticsearch实现全文搜索引擎案例解析

程序员文章站 2024-04-02 09:47:40
简单说,elasticsearch(简称 es)是搜索引擎,是结构化数据的分布式搜索引擎。elastic search是一个开源的,分布式,实时搜索和分析引擎。spring...

简单说,elasticsearch(简称 es)是搜索引擎,是结构化数据的分布式搜索引擎。elastic search是一个开源的,分布式,实时搜索和分析引擎。spring boot为elasticsearch及spring data elasticsearch提供的基于它的抽象提供了基本的配置。spring boot提供了一个用于聚集依赖的spring-boot-starter-data-elasticsearch 'starterpom'。

引入spring-boot-starter-data-elasticsearch依赖,在pom.xml配置文件中增加如下内容(基于之前章节“spring boot 构建框架”中的pom.xml文件):

<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-data-elasticsearch</artifactid>
</dependency>

可以像其他spring beans那样注入一个自动配置的elasticsearchtemplate或elasticsearch客户端实例。默认情况下,该实例将尝试连接到一个本地内存服务器(在elasticsearch项目中的一个nodeclient),但可以通过设置spring.data.elasticsearch.clusternodes为一个以逗号分割的host:port列表来将其切换到一个远程服务器(比如,transportclient)。

@component
public class mybean {
 private elasticsearchtemplate template;
 
 @autowired
 public mybean(elasticsearchtemplate template) {
 this.template = template;
 }
 // ...
}

如果添加一个自己的elasticsearchtemplate类型的@bean,它将替换默认的。

应用集成elasticsearch案例

新建elasticsearch.properties配置文件,添加如下配置内容:

elasticsearch.host=localhost
elasticsearch.port=9300

elasticsearch配置,读取elasticsearch.properties配置文件信息,具体代码如下:

@configuration@propertysource(value = "classpath:elasticsearch.properties")
@enableelasticsearchrepositories(basepackages = "co.paan.repository")
public class elasticsearchconfiguration {
 @resource
private environment environment;
@bean
public client client() {
transportclient client = new transportclient();
transportaddress address = new inetsockettransportaddress(environment.getproperty("elasticsearch.host"), integer.parseint(environment.getproperty("elasticsearch.port")));
client.addtransportaddress(address); 
return client;
}
 @beanpublic elasticsearchoperations elasticsearchtemplate() {
 return new elasticsearchtemplate(client());
 }
}

两个实体类,具体代码如下:

@document(indexname = "post", type = "post", shards = 1, replicas = 0)
public class post {
@id
private string id;
 private string title;
@field(type= fieldtype.nested)
private list<tag> tags; 
 public string getid() {
return id;
}
 public void setid(string id) {
 this.id = id;
}
 public string gettitle() {
 return title;
}
 public void settitle(string title) {
 this.title = title;
}
 public list<tag> gettags() {
 return tags;
}
 public void settags(list<tag> tags) {
 this.tags = tags;
}
}
public class tag {
private string id; 
private string name; 
public string getid() {
 return id;
 }
 public void setid(string id) {
 this.id = id;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
}

数据源继承elasticsearchrepository类,封装接口代码如下:

public interface postrepository extends elasticsearchrepository<post, string>{
 page<post> findbytagsname(string name, pageable pageable);
}

数据服务接口及实现类,代码如下:

public interface postservice {
 post save(post post);
 post findone(string id);
 iterable<post> findall();
 page<post> findbytagsname(string tagname, pagerequest pagerequest);
}
@servicepublic class postserviceimpl implements postservice{
 @autowired
private postrepository postrepository;
@override
public post save(post post) {
 postrepository.save(post); 
 return post;
 }
 @overridepublic post findone(string id) {
 return postrepository.findone(id);
 }
 @overridepublic iterable<post> findall() {
 return postrepository.findall();
 }
 @overridepublic page<post> findbytagsname(string tagname, pagerequest pagerequest) {
 return postrepository.findbytagsname(tagname, pagerequest);
 }
}

测试代码如下:

@test
public void testfindbytagsname() throws exception {
 tag tag = new tag();
 tag.setid("1");
 tag.setname("tech");
 tag tag2 = new tag();
 tag2.setid("2");
 tag2.setname("elasticsearch");
 post post = new post();
 post.setid("1");
 post.settitle("bigining with spring boot application and elasticsearch");
 post.settags(arrays.aslist(tag, tag2));
 postservice.save(post);
 post post2 = new post();
 post2.setid("1");
 post2.settitle("bigining with spring boot application");
 post2.settags(arrays.aslist(tag));
 postservice.save(post);
 page<post> posts = postservice.findbytagsname("tech", new pagerequest(0,10));
 page<post> posts2 = postservice.findbytagsname("tech", new pagerequest(0,10));
 page<post> posts3 = postservice.findbytagsname("maz", new pagerequest(0,10));
 assertthat(posts.gettotalelements(), is(1l));
 assertthat(posts2.gettotalelements(), is(1l));
 assertthat(posts3.gettotalelements(), is(0l));
}

总结

以上所述是小编给大家介绍的spring boot整合elasticsearch实现全文搜索引擎案例解析,希望对大家有所帮助