Spring Boot整合Elasticsearch實(shí)現(xiàn)全文搜索引擎案例解析
簡單說,ElasticSearch(簡稱 ES)是搜索引擎,是結(jié)構(gòu)化數(shù)據(jù)的分布式搜索引擎。Elastic Search是一個(gè)開源的,分布式,實(shí)時(shí)搜索和分析引擎。Spring Boot為Elasticsearch及Spring Data Elasticsearch提供的基于它的抽象提供了基本的配置。Spring Boot提供了一個(gè)用于聚集依賴的spring-boot-starter-data-elasticsearch 'StarterPOM'。
引入spring-boot-starter-data-elasticsearch
依賴,在pom.xml配置文件中增加如下內(nèi)容(基于之前章節(jié)“Spring Boot 構(gòu)建框架”中的pom.xml文件):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
可以像其他Spring beans那樣注入一個(gè)自動(dòng)配置的ElasticsearchTemplate或Elasticsearch客戶端實(shí)例。默認(rèn)情況下,該實(shí)例將嘗試連接到一個(gè)本地內(nèi)存服務(wù)器(在Elasticsearch項(xiàng)目中的一個(gè)NodeClient),但可以通過設(shè)置spring.data.elasticsearch.clusterNodes為一個(gè)以逗號(hào)分割的host:port列表來將其切換到一個(gè)遠(yuǎn)程服務(wù)器(比如,TransportClient)。
@Component public class MyBean { private ElasticsearchTemplate template; @Autowired public MyBean(ElasticsearchTemplate template) { this.template = template; } // ... }
如果添加一個(gè)自己的ElasticsearchTemplate類型的@Bean,它將替換默認(rèn)的。
應(yīng)用集成ElasticSearch案例
新建elasticsearch.properties配置文件,添加如下配置內(nèi)容:
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()); } }
兩個(gè)實(shí)體類,具體代碼如下:
@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; } }
數(shù)據(jù)源繼承ElasticsearchRepository類,封裝接口代碼如下:
public interface PostRepository extends ElasticsearchRepository<Post, String>{ Page<Post> findByTagsName(String name, Pageable pageable); }
數(shù)據(jù)服務(wù)接口及實(shí)現(xiàn)類,代碼如下:
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)); }
總結(jié)
以上所述是小編給大家介紹的Spring Boot整合Elasticsearch實(shí)現(xiàn)全文搜索引擎案例解析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
feign參數(shù)過多導(dǎo)致調(diào)用失敗的解決方案
這篇文章主要介紹了feign參數(shù)過多導(dǎo)致調(diào)用失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Springboot視頻接口報(bào)大量的ClientAbortException找不到原因的解決
本文主要介紹了Springboot視頻接口報(bào)大量的ClientAbortException找不到原因的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08Java語言實(shí)現(xiàn)簡單FTP軟件 FTP上傳下載管理模塊實(shí)現(xiàn)(11)
這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟件,F(xiàn)TP本地文件管理模塊的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04java中URLEncoder.encode與URLDecoder.decode處理url特殊參數(shù)的方法
這篇文章主要給大家介紹了關(guān)于java中URLEncoder.encode與URLDecoder.decode處理url特殊參數(shù)的方法,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03JAVA編程實(shí)現(xiàn)TCP網(wǎng)絡(luò)通訊的方法示例
這篇文章主要介紹了JAVA編程實(shí)現(xiàn)TCP網(wǎng)絡(luò)通訊的方法,簡單說明了TCP通訊的原理并結(jié)合具體實(shí)例形式分析了java實(shí)現(xiàn)TCP通訊的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08