SpringBoot检索篇Ⅳ --- 整合ElasticSearch

知识储备: 

关于ElasticSearch的基本使用我已经在上一篇文章介绍过了(传送门),本篇文章主要讲述的是SpringBoot与ElasticSearch的整合使用。

SpringBoot与ElasticSearch的整合

一.Jest的使用

1.1 Jest默认不生效,需要导入Jest的工具包:

<dependency>
      <groupId>io.searchbox</groupId>
      <artifactId>jest</artifactId>
      <version>5.3.3</version>
 </dependency>

1.2 默认连接的主机地址是localhost,若不是部署在本地的话需要配置主机地址

spring.elasticsearch.jest.uris=http://172.**.**.**:9200

1.3 创建需要操作的实体类

public class People {
    @JestId //注意要给id加上注解
    private Integer id;
    private String name;
    private Integer age;
    private String gender;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

1.4  测试:给ES中的索引保存一个文档

   @Autowired
    JestClient jestClient; //SpringBoot已经给我们自动配置好了,可以直接引用
    @Test
    public void contextLoads() {
        //1,给ES中的索引保存一个文档
        People people = new People();
        people.setId(1);
        people.setAge(22);
        people.setGender("男");
        people.setName("王鑫");

        //构建一个索引功能
        Index index = new Index.Builder(people).index("school").type("qinghua").build();
        try {
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

1.5 测试:构建搜索功能

 @Test
    public void search(){
        //查询表达式
        String json = "{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"name\" : \"王\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        //构建搜索功能
        Search search = new Search.Builder(json).addIndex("school").addType("qinghua").build();
        try {
            SearchResult result = jestClient.execute(search);
            System.out.println(result.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这里只是给出几个简单的演示,更多的可以详细参考Jest的官方文档

 

二.SpringData ElasticSearch的使用

在使用SpringData ElasticSearch前有个问题需要关注一下,就是版本问题,我一开始用的是ES的最新版,SpringBoot2.x的版本,但是一直报错。所以经过几番测试我还是将SpringBoot版本将为1.5.6版本,并用docker重新安装了2.4.6版本的ES。

2.1 配置节点名称,端口

SpringData ElasticSearch采用的是TCP通信,用的是9300端口。

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=***.**.**.**:9300
spring.data.elasticsearch.repositories.enabled=true

2.2 创建实体类,配置索引信息

package com.wang.elasticsearch.bean;

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

@Document(indexName = "xinhua",type = "book") //这边要配置索引信息
public class Book {
    private Integer id;
    private String bookname;
    private String author;

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", bookname='" + bookname + '\'' +
                ", author='" + 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;
    }
}

2.3 编写一个ElasticsearchRepository

/**
 * 泛型的类型为<实体类,索引主键类型>
 */
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
   
}

2.4 创建一个索引

  @Test
    public void Booksave(){
        Book book = new Book();
        book.setId(1);
        book.setBookname("Java开发");
        book.setAuthor("Wang");
        bookRepository.index(book);
    }

2.5 自定义ElasticsearchRepository方法

ElasticsearchRepository提供有很多自带的方法,能够满足很多开发需求,当然也可以扩展自己的方法,扩展方法只需要给个方法名就可以了,不过要注意方法名称要符合规范。

/**
 * 泛型的类型为<实体类,索引类型>
 */
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
    /**
     * 这边的方法名要注意和属性名对应,Book的属性名为bookname,对应的方法名中的属性为Bookname而不是BookName
     * @param bookname
     * @return
     */
    public List<Book> findByBooknameLike(String bookname);
}
@Test
    public void Bookfind(){
    
        for (Book book: bookRepository.findByBooknameLike("开")){
            System.out.println(book);
        }

    }

 

posted @ 2019-01-15 11:59  薛定谔病态猫  阅读(351)  评论(0编辑  收藏  举报