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

猜你喜欢

转载自blog.csdn.net/peng_0129/article/details/85235471
今日推荐