SpringBoot2.1.X 整合Elasticsearch

一、查看 Spring Data官网:http://projects.spring.io/spring-data/

    Spring Data 的使命是给各种数据访问提供统一的编程接口,不管是关系型数据库(如MySQL),还是非关系数据库(如Redis),或者类似Elasticsearch这样的索引数据库。从而简化开发人员的代码,提高开发效率。

Spring Data Elasticsearch的页面:https://projects.spring.io/spring-data-elasticsearch/ 

Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。

从上看:Elasticsearch与mysql对比

索引库(indices)--------------------------------Databases 数据库

类型(type)--------------------------------table 数据表

文档(document)--------------------------------row 行数据

字段(field)--------------------------------column 列

 

 

上图来自SpringBoot整合ElasticSearch及源码

在Elasticsearch有一些集群相关的概念:

    1.  索引集(Indices,index的复数):逻辑上的完整索引
    2. 分片(shard):数据拆分后的各个部分
    3.  副本(replica):每个分片的复制
注意:
       Elasticsearch本身就是分布式的,因此即便你只有一个节点,Elasticsearch默认也会对你的数据进行分片和副本操作,当你向集群添加新数据时,数据也会在新加入的节点中进行平衡。

 

 

二、实战操作

1、添加maven依赖

  <!--springboot整合relasticsearch搜索引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

2、接口继承ElasticSearchRepository,里面有很多默认实现

package net.hlx.myspringboot.redis_demo.repository;

import net.hlx.myspringboot.redis_demo.entity.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/**
 * ArticleRepository接口
 */
@Component
//@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {

}

3、实体对象(索引名称记得小写,类属性名称也要小写;加上类注解 @Document(indexName = "blog", type = "article"))

**
 * 功能描述:文章对象
 * @Document:一条记录
 * indexName=数据库名
 * type=表名
 */
@Document(indexName = "blog", type = "article")
public class Article implements Serializable{

	private static final long serialVersionUID = 1L;

	private long id;  //ID
	
	private String title;  //标题
	
	private String summary; //概要
	
	private String content; //内容
	
	private int pv; //pv

   4、配置文件application.properties

         查看文档:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/

# Elasticsearch cluster name.
spring.data.elasticsearch.cluster-name=elasticsearch

# Comma-separated list of cluster node addresses.
spring.data.elasticsearch.cluster-nodes=localhost:9300

# Additional properties used to configure the client.
spring.data.elasticsearch.repositories.enabled=true

5、Controller类

@RestController
@RequestMapping("/elastic/my")
public class ArticleController {

    /**
     * 注入
     */
    @Autowired
    private ArticleRepository articleRepository;

    @GetMapping("save")
    public Object save(){

        //数据对象
        Article article =new Article(1L,"ElasticSearch","如何安装和使用ElasticSearch","what are your reading book?",888);
        Article article1 =new Article(2L,"Springboot","使用Springboot","can I borrow your copy?",889);
        Article article3 =new Article(3L,"这个elasticSearch搜索引擎,你知道吗?","这是概述","would you do like book?",890);

        //写入
        articleRepository.save(article);
       articleRepository.save(article1);
        articleRepository.save(article3);

        return JsonData.buildSuccess();
    }

    @GetMapping("find")
    public Object find(String title){
      //  QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); //搜索全部文档
        //查找summary
        QueryBuilder builder= QueryBuilders.matchQuery("title",title);

        //返回集合
        Iterable<Article> it = articleRepository.search(builder);

        return JsonData.buildSuccess(it);
    }

}

 查询构建器的工厂QueryBuilder使用:
        https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.3/query-dsl-queries.html

启动:

添加索引数据

查看索引列表 

   查看某个索引库结构:http://localhost:9200/blog 

    查看某个对象:http://localhost:9200/blog/article/1

根据标题查询数据 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值