javascript
SpringBoot 2.x (12):整合Elasticsearch
Elasticsearch:一個(gè)優(yōu)秀的搜索引擎框架
搜索方面最基本的是SQL的like語(yǔ)句
進(jìn)一步的有Lucene框架
后來(lái)有企業(yè)級(jí)的Solr框架
而Elasticsearch框架尤其適合于數(shù)據(jù)量特別大的
Elasticsearch底層也是由Lucene實(shí)現(xiàn)的
?
應(yīng)用:Github、維基百科、StackOverflow
?
Elasticsearch部署:
純Java開(kāi)發(fā),因此必備JDK
采用5.6版本而不是最新版,因?yàn)镾pringBoot可能不支持
?
推薦部署到Linux服務(wù)器,但是這里為了方便我直接部署在本地Windows系統(tǒng)
下載地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.8.zip
?
下載好進(jìn)入BIN目錄,運(yùn)行elasticsearch.bat運(yùn)行
如果報(bào)錯(cuò),通常情況是機(jī)器配置不夠或者以錯(cuò)誤地root權(quán)限啟動(dòng)了
Win10高配機(jī)器不存在這種問(wèn)題
?
http://127.0.0.1:9200/:進(jìn)入主頁(yè)
http://localhost:9200/_cat/health?v:查看集群狀態(tài)
localhost:9200/_cat/indices?v:查看索引列表
?
參考官網(wǎng),創(chuàng)建索引:
?
查看我創(chuàng)建的索引:
?
加入數(shù)據(jù):
?
查看我加入的數(shù)據(jù):
?
SpringBoot進(jìn)行整合
依賴(lài):
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>查看依賴(lài)后發(fā)現(xiàn),SpringBoot2.1.4采用的elasticsearch是6.4.3版本
為了確保不出問(wèn)題,我重新下載6.4.3版本,事后我驗(yàn)證了,就以下的這些基本操作,es的版本可以不影響
?
新建實(shí)體類(lèi)Article:
搜索的對(duì)象就是文章
package org.dreamtech.esdemo.domain;import java.io.Serializable;import org.springframework.data.elasticsearch.annotations.Document;/*** 文章對(duì)象* * @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;}}?
對(duì)ES進(jìn)行配置:
spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 spring.data.elasticsearch.repositories.enabled=true?
對(duì)ES進(jìn)行操作默認(rèn)的接口:
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層測(cè)試代碼:
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;}}?
實(shí)際測(cè)試:
?
?
用這種方式保存多個(gè)文章后就可以進(jìn)行搜索了:
轉(zhuǎn)載于:https://www.cnblogs.com/xuyiqing/p/10848273.html
總結(jié)
以上是生活随笔為你收集整理的SpringBoot 2.x (12):整合Elasticsearch的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 新辰:关于个人站点安全问题的分析及对策探
- 下一篇: gradle idea java ssm