javascript
SpringBoot 2.x (12):整合Elasticsearch
Elasticsearch:一個優秀的搜索引擎框架
搜索方面最基本的是SQL的like語句
進一步的有Lucene框架
后來有企業級的Solr框架
而Elasticsearch框架尤其適合于數據量特別大的
Elasticsearch底層也是由Lucene實現的
?
應用:Github、維基百科、StackOverflow
?
Elasticsearch部署:
純Java開發,因此必備JDK
采用5.6版本而不是最新版,因為SpringBoot可能不支持
?
推薦部署到Linux服務器,但是這里為了方便我直接部署在本地Windows系統
下載地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.8.zip
?
下載好進入BIN目錄,運行elasticsearch.bat運行
如果報錯,通常情況是機器配置不夠或者以錯誤地root權限啟動了
Win10高配機器不存在這種問題
?
http://127.0.0.1:9200/:進入主頁
http://localhost:9200/_cat/health?v:查看集群狀態
localhost:9200/_cat/indices?v:查看索引列表
?
參考官網,創建索引:
?
查看我創建的索引:
?
加入數據:
?
查看我加入的數據:
?
SpringBoot進行整合
依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>查看依賴后發現,SpringBoot2.1.4采用的elasticsearch是6.4.3版本
為了確保不出問題,我重新下載6.4.3版本,事后我驗證了,就以下的這些基本操作,es的版本可以不影響
?
新建實體類Article:
搜索的對象就是文章
package org.dreamtech.esdemo.domain;import java.io.Serializable;import org.springframework.data.elasticsearch.annotations.Document;/*** 文章對象* * @author Xu Yiqing**/ @Document(indexName = "blog", type = "article") public class Article implements Serializable {private static final long serialVersionUID = 8210249797764830332L;private long id;private String title;private String summary;private String content;private int pv;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getSummary() {return summary;}public void setSummary(String summary) {this.summary = summary;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public int getPv() {return pv;}public void setPv(int pv) {this.pv = pv;}}?
對ES進行配置:
spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 spring.data.elasticsearch.repositories.enabled=true?
對ES進行操作默認的接口:
package org.dreamtech.esdemo.repository;import org.dreamtech.esdemo.domain.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component;@Component public interface ArticleRepository extends ElasticsearchRepository<Article, Long> { }?
Controller層測試代碼:
package org.dreamtech.esdemo.controller;import org.dreamtech.esdemo.domain.Article; import org.dreamtech.esdemo.repository.ArticleRepository; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class ArticleController {@Autowiredprivate ArticleRepository articleRepository;@GetMapping("/save")public Object save(long id, String title) {Article article = new Article();article.setId(id);article.setPv(123);article.setContent("Springboot整合Elasticsearch");article.setTitle(title);article.setSummary("搜索框架整合");articleRepository.save(article);return "save";}@GetMapping("/search")public Object search(String title) {QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title);Iterable<Article> list = articleRepository.search(queryBuilder);return list;}}?
實際測試:
?
?
用這種方式保存多個文章后就可以進行搜索了:
轉載于:https://www.cnblogs.com/xuyiqing/p/10848273.html
總結
以上是生活随笔為你收集整理的SpringBoot 2.x (12):整合Elasticsearch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新辰:关于个人站点安全问题的分析及对策探
- 下一篇: [BZOJ3932][CQOI2015]