Spring Boot整合Elasticsearch实现全文搜索引擎案例解析
简单说,ElasticSearch(简称ES)是搜索引擎,是结构化数据的分布式搜索引擎。ElasticSearch是一个开源的,分布式,实时搜索和分析引擎。SpringBoot为Elasticsearch及SpringDataElasticsearch提供的基于它的抽象提供了基本的配置。SpringBoot提供了一个用于聚集依赖的spring-boot-starter-data-elasticsearch'StarterPOM'。
引入spring-boot-starter-data-elasticsearch依赖,在pom.xml配置文件中增加如下内容(基于之前章节“SpringBoot构建框架”中的pom.xml文件):
org.springframework.boot spring-boot-starter-data-elasticsearch
可以像其他Springbeans那样注入一个自动配置的ElasticsearchTemplate或Elasticsearch客户端实例。默认情况下,该实例将尝试连接到一个本地内存服务器(在Elasticsearch项目中的一个NodeClient),但可以通过设置spring.data.elasticsearch.clusterNodes为一个以逗号分割的host:port列表来将其切换到一个远程服务器(比如,TransportClient)。
@Component publicclassMyBean{ privateElasticsearchTemplatetemplate; @Autowired publicMyBean(ElasticsearchTemplatetemplate){ 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") publicclassElasticsearchConfiguration{ @Resource privateEnvironmentenvironment; @Bean publicClientclient(){ TransportClientclient=newTransportClient(); TransportAddressaddress=newInetSocketTransportAddress(environment.getProperty("elasticsearch.host"),Integer.parseInt(environment.getProperty("elasticsearch.port"))); client.addTransportAddress(address); returnclient; } @BeanpublicElasticsearchOperationselasticsearchTemplate(){ returnnewElasticsearchTemplate(client()); } }
两个实体类,具体代码如下:
@Document(indexName="post",type="post",shards=1,replicas=0) publicclassPost{ @Id privateStringid; privateStringtitle; @Field(type=FieldType.Nested) privateListtags; publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetTitle(){ returntitle; } publicvoidsetTitle(Stringtitle){ this.title=title; } publicList getTags(){ returntags; } publicvoidsetTags(List tags){ this.tags=tags; } } publicclassTag{ privateStringid; privateStringname; publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } }
数据源继承ElasticsearchRepository类,封装接口代码如下:
publicinterfacePostRepositoryextendsElasticsearchRepository{ Page findByTagsName(Stringname,Pageablepageable); }
数据服务接口及实现类,代码如下:
publicinterfacePostService{ Postsave(Postpost); PostfindOne(Stringid); IterablefindAll(); Page findByTagsName(StringtagName,PageRequestpageRequest); } @ServicepublicclassPostServiceImplimplementsPostService{ @Autowired privatePostRepositorypostRepository; @Override publicPostsave(Postpost){ postRepository.save(post); returnpost; } @OverridepublicPostfindOne(Stringid){ returnpostRepository.findOne(id); } @OverridepublicIterable findAll(){ returnpostRepository.findAll(); } @OverridepublicPage findByTagsName(StringtagName,PageRequestpageRequest){ returnpostRepository.findByTagsName(tagName,pageRequest); } }
测试代码如下:
@Test publicvoidtestFindByTagsName()throwsException{ Tagtag=newTag(); tag.setId("1"); tag.setName("tech"); Tagtag2=newTag(); tag2.setId("2"); tag2.setName("elasticsearch"); Postpost=newPost(); post.setId("1"); post.setTitle("Biginingwithspringbootapplicationandelasticsearch"); post.setTags(Arrays.asList(tag,tag2)); postService.save(post); Postpost2=newPost(); post2.setId("1"); post2.setTitle("Biginingwithspringbootapplication"); post2.setTags(Arrays.asList(tag)); postService.save(post); Pageposts=postService.findByTagsName("tech",newPageRequest(0,10)); Page posts2=postService.findByTagsName("tech",newPageRequest(0,10)); Page posts3=postService.findByTagsName("maz",newPageRequest(0,10)); assertThat(posts.getTotalElements(),is(1L)); assertThat(posts2.getTotalElements(),is(1L)); assertThat(posts3.getTotalElements(),is(0L)); }
总结
以上所述是小编给大家介绍的SpringBoot整合Elasticsearch实现全文搜索引擎案例解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!