SpringBoot高级(三)Elasticsearch

一、ES介绍

1、安装

elastic 的官网 elastic.co/downloads/elasticsearch 获取最新版本的Elasticsearch。解压文档后,按照下面的操作

cd elasticsearch-<version>
./bin/elasticsearch.bat

此时,Elasticsearch运行在本地的9200端口,在浏览器中输入网址“http://localhost:9200/”,

2、概念

在这里插入图片描述

二、SpringBoot整合

1、核心依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

2、配置文件

spring.elasticsearch.rest.uris=127.0.0.1:9200

3、实体类配置

@Document(indexName = "mvc")    //设置保存的索引
public class Article {
    
    

    private Integer id;

    private String author;

    private String title;

    private String content;

4、调用

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

	//添加
    @Test
    void contextLoads() {
    
    
        Article article = new Article(1, "allen", "bbb", "helloworld");
        System.err.println(article);
        //修改也可以用save
        elasticsearchRestTemplate.save(article);
    }

	//获取
    @Test
    public void test02(){
    
    
        //获取aaa索引中id为1的Article对象
        Article article = elasticsearchRestTemplate.get("1", Article.class);
        System.out.println(article);
    }

猜你喜欢

转载自blog.csdn.net/qq_38618691/article/details/118144159