logo资料库

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

第1页 / 共2页
第2页 / 共2页
资料共2页,全文预览结束
Spring Boot整合整合Elasticsearch实现全文搜索引擎案例解析 实现全文搜索引擎案例解析 ElasticSearch作为基于Lucene的搜索服务器,既可以作为一个独立的服务部署,也可以签入Web应用中。SpringBoot作为 Spring家族的全新框架,使得使用SpringBoot开发Spring应用变得非常简单,在本案例中我们给大家介绍Spring Boot整合 Elasticsearch实现全文搜索引擎 简单说,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文件): org.springframework.boot spring-boot-starter-data-elasticsearch 可以像其他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 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 getTags() { return tags; } public void setTags(List 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{ Page findByTagsName(String name, Pageable pageable); } 数据服务接口及实现类,代码如下: public interface PostService { Post save(Post post); Post findOne(String id); Iterable findAll(); Page 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 findAll() { return postRepository.findAll(); } @Overridepublic Page 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 posts = postService.findByTagsName("tech", new PageRequest(0,10)); Page posts2 = postService.findByTagsName("tech", new PageRequest(0,10)); Page 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实现全文搜索引擎案例解析,希望对大家有所帮助,如果大家有任何疑问请给我留 言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
分享到:
收藏