SpringBoot系列二十九:整合ElasticSearch

一、简介
 ElasticSearch 可以快速的存储、搜索和分析海量数据。Elasticsearch是一个分布式搜索服务,提供Restful API,底层基于Lucene,采用 多shard(分片)的方式保证数据安全,并且提供自动resharding的功能,github 等大型的站点也是采用了ElasticSearch作为其搜索服务。
 以员工文档的形式存储为例:一个文档代表一个员工数据。存储数据到 ElasticSearch 的行为叫做索引 ,但在索引一个文档之前,需要确定将文档存储在哪里。
 一个 ElasticSearch 集群可以包含多个索引 ,相应的每个索引可以包含多个类型 。 这些不同的类型存储着多个 文档 ,每个文档又有 多个 属性 。
类似关系:
 索引-数据库
 类型-表
 文档-表中的记录
 属性-列
在这里插入图片描述
二、整合ElasticSearch
1、引入依赖

<!--SpringBoot默认使用SpringData ElasticSearch模块进行操作-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>5.3.3</version>
</dependency>

2、application.properties配置

spring.elasticsearch.jest.uris=http://192.168.106.130:9200
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.106.130:9300

3、主程序类 Springboot11ElasticApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/***
 * SpringBoot默认支持两种技术来和ES交互
 * 1、Jest(默认不生效)
 *    需要导入jest的工具包(io.searchbox.client.JestClient)
 * 2、SpringData ElasticSearch【ES版本有可能不合适】
 *    版本适配说明:https://github.com/spring-projects/spring-data-elasticsearch
 *    如果版本不适配:2.4.6
 *        1)、升级SpringBoot版本
 *        2)、安装对应版本的ES
 *    1)、Client 节点信息clusterNodes;clusterName
 *    2)、ElasticsearchTemplate
 *    3)、编写一个ElasticsearchRepository的子接口来操作ES;
 * 两种用法:https://github.com/spring-projects/spring-data-elasticsearch
 * 1)、编写一个ElasticsearchRepository
 *
 **/
@SpringBootApplication
public class Springboot11ElasticApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot11ElasticApplication.class, args);
    }

}

4、实体类 Article

import io.searchbox.annotations.JestId;

public class Article {

    @JestId
    private Integer id;
    private String author;
    private String title;
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

5、实体类 Book

import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "atguigu", type = "book")
public class Book {

    private Integer id;
    private String bookName;
    private String author;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

6、BookRepository

import com.atguigu.elastic.bean.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;

public interface BookRepository extends ElasticsearchRepository<Book, Integer> {

    public List<Book> findByBookNameLike(String bookName);
}

7、使用JestClient

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot11ElasticApplicationTests {

    @Autowired
    JestClient jestClient;
    @Autowired
    BookRepository bookRepository;

    @Test
    public void test02() {
       /* Book book = new Book();
        book.setId(1);
        book.setBookName("西游记");
        book.setAuthor("吴承恩");
        bookRepository.index(book);*/

        for (Book book : bookRepository.findByBookNameLike("游")) {
            System.out.println(book);
        }
    }

    @Test
    public void contextLoads() {
        //1、给Es中索引(保存)一个文档;
        Article article = new Article();
        article.setId(1);
        article.setTitle("好消息");
        article.setAuthor("zhangsan");
        article.setContent("Hello World");

        //构建一个索引功能
        Index index = new Index.Builder(article).index("atguigu").type("news").build();
        try {
            //执行
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void search() {
        String json = "{\n" +
                "    \"query\": {\n" +
                "        \"match\": {\n" +
                "            \"content\": \"hello\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        //构建搜索功能
        Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();
        try {
            //执行
            SearchResult result = jestClient.execute(search);
            System.out.println(result.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/lizhiqiang1217/article/details/92079618
今日推荐