欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot整合ElasticSearch(四)

程序员文章站 2024-03-05 15:35:07
...

ElasticsearchRepository

1.注入依赖

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

2.application.properties添加配置

添加基于 spring.data.elasticsearch的配置。

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

nodes不同于jest使用http通信,使用nodes端口。

"cluster_name" : "elasticsearch"名字信息

{
  "name" : "MTwQmnF",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "lLW-yQMmRPW7nUBpHOqe8Q",
  "version" : {
    "number" : "5.6.12",
    "build_hash" : "cfe3d9f",
    "build_date" : "2018-09-10T20:12:43.732Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}
  1. 使用ElasticsearchRepository 往elasticsearch中添加数据

自定义对象Book

package com.invi.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "invi", type = "book")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {

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

}

lombok插件:

  • 注入依赖

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.22</version>
            </dependency>
    
  • idea安装lombok插件 不然会语法报错

    • @Data :注解在类上, 为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法
    • @AllArgsConstructor 全参构造方法
    • @NoArgsConstructor 无参构造方法

添加BookRepository

package com.invi.repository;

import com.invi.bean.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;


public interface BookRepository extends ElasticsearchRepository<Book, Integer> {
    //自定义查询方法
    public List<Book> findByBookNameLike(String bookName);
}

继承ElasticsearchRepository

ElasticsearchRepository 中定义的方法

<S extends T> S index(S var1); //存储序列

Iterable<T> search(QueryBuilder var1);//查询

Page<T> search(QueryBuilder var1, Pageable var2);

Page<T> search(SearchQuery var1);

Page<T> searchSimilar(T var1, String[] var2, Pageable var3);

void refresh();

Class<T> getEntityClass();
测试
package com.invi;

import com.invi.bean.Article;
import com.invi.bean.Book;
import com.invi.repository.BookRepository;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.Build;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

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


    @Autowired
    BookRepository bookRepository;


    @Test
    public void save() {
        Book book = new Book(1, "《西游记》", "吴承恩");
        bookRepository.index(book);
    }


    @Test
    public void testSearch() {
        for (Book book : bookRepository.findByBookNameLike("西游记")) {
            System.out.println(book);
        }
    }
}

  1. index 用来保存信息

  2. 需要在 Book 类上标注 序列名indexName,类型type

    @Document(indexName = "invi", type = "book")

  3. BookRepository 支持 自定义命名方法: findByBookNameLike

测试都成功!

ElasticsearchTemplate