美文网首页
Spring Boot ElasticSearch

Spring Boot ElasticSearch

作者: 虫儿飞ZLEI | 来源:发表于2019-03-03 18:40 被阅读0次

    1.结构

    image.png

    索引:类似于mysql的数据库
    类型:类似于mysql的表
    文档:类似于mysql的一条记录
    属性:类似于mysql的一条记录的某一列

    文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

    2. Spring Boot使用

    两种方式操作ES

    1. Jest:默认不生效
    2. SpringData ElasticSearch

    2.1 Jest方式

    导包:

            <!--SpringBoot默认使用SpringData ElasticSearch模块进行操作,先注释掉-->
            <!--<dependency>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-starter-data-elasticsearch</artifactId>-->
            <!--</dependency>
            <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
            <dependency>
                <groupId>io.searchbox</groupId>
                <artifactId>jest</artifactId>
                <version>5.3.3</version>
            </dependency>
    

    配置:

    spring.elasticsearch.jest.uris=http://118.24.44.169:9200
    

    使用:

    bean:

    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;
        }
    }
    

    保存:

        @Autowired
        JestClient jestClient;
    
        @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();
            }
        }
    

    搜索:

        @Autowired
        JestClient jestClient;
    
        //测试搜索
        @Test
        public void search(){
    
            //查询表达式
            String json ="{\n" +
                    "    \"query\" : {\n" +
                    "        \"match\" : {\n" +
                    "            \"content\" : \"hello\"\n" +
                    "        }\n" +
                    "    }\n" +
                    "}";
    
            //更多操作:https://github.com/searchbox-io/Jest/tree/master/jest
            //构建搜索功能
            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();
            }
        }
    

    2.2 SpringData 方式

    导包:

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

    配置:

    spring.data.elasticsearch.cluster-name=elasticsearch
    spring.data.elasticsearch.cluster-nodes=118.24.44.169:9300
    

    版本适配:


    image.png
    •  如果版本不适配
      
    •      1)、升级SpringBoot版本
      
    •      2)、安装对应版本的ES
      

    使用:
    bean:@Document(indexName = "atguigu",type = "book")

    package com.atguigu.elastic.bean;
    
    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 + '\'' +
                    '}';
        }
    }
    
    
    public interface BookRepository extends ElasticsearchRepository<Book,Integer(主键类型)> {
    }
    

    保存:

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

    这个ElasticsearchRepository默认有一些增删改查的方法,也支持自定义方法

    自定义方法

    只需要写方法名,不需要写方法实现,方法名的名称要按照一定的规则命名

    public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
    
       //参照
       // https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
      public List<Book> findByBookNameLike(String bookName);
    
    }
    
        @Test
        public void test02(){
    
            for (Book book : bookRepository.findByBookNameLike("游")) {
                System.out.println(book);
            }
        }
    

    相关文章

      网友评论

          本文标题:Spring Boot ElasticSearch

          本文链接:https://www.haomeiwen.com/subject/zbcmuqtx.html