SpringBoot2.x整合elasticsearch5.6.x
1、新增maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、介面繼承ElasticSearchRepository,裡面有很多預設實現
注意點:
索引名稱記得小寫,類屬性名稱也要小寫
新建實體物件article
加上類註解 @Document(indexName = "blog", type = "article")
package net.xdclass.base_project.repository;
import net.xdclass.base_project.domain.Article;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Component
//@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
}
實體類
@Document(indexName = "blog", type = "article")
public class Article implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
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;
}
}
3、配置檔案:
# ELASTICSEARCH (ElasticsearchProperties)
spring.data.elasticsearch.cluster-name=elasticsearch # Elasticsearch cluster name.
spring.data.elasticsearch.cluster-nodes=localhost:9300 # Comma-separated list of cluster node addresses.
spring.data.elasticsearch.repositories.enabled=true # Whether to enable Elasticsearch repositories.
4.具體controller使用
package net.xdclass.base_project.controller;
import net.xdclass.base_project.domain.Article;
import net.xdclass.base_project.domain.JsonData;
import net.xdclass.base_project.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/article")
public class ArticleController {
@Autowired
private 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,這個是新版本 2018年錄製");
article.setTitle(title);
article.setSummary("搜尋框架整合");
articleRepository.save(article);
return JsonData.buildSuccess();
}
@GetMapping("search")
public Object search(String title){
//QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); //搜尋全部文件
QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title);
Iterable<Article> list = articleRepository.search(queryBuilder);
return JsonData.buildSuccess(list);
}
}
4、檢視es資料
檢視索引資訊:http://localhost:9200/_cat/indices?v
檢視某個索引庫結構:http://localhost:9200/blog
檢視某個物件:http://localhost:9200/blog/article/1
elasticSearch主要特點
1、特點:全文檢索,結構化檢索,資料統計、分析,接近實時處理,分散式搜尋(可部署數百臺伺服器),處理PB級別的資料
搜尋糾錯,自動完成
2、使用場景:日誌搜尋,資料聚合,資料監控,報表統計分析
3、國內外使用者:維基百科,Stack Overflow,GitHub
與mysql相似:
mysql:database table rocord
es : index type(只能存在一個) document