Spring Boot 中的 Elasticsearch 的数据操作配置

Spring Boot 中的 Elasticsearch 的数据操作配置

Elasticsearch是一个基于Lucene的搜索引擎,可以快速地存储、搜索和分析大量的数据。Spring Boot是一个开发框架,提供了快速构建基于Spring的应用程序的工具和技术。在本文中,我们将讨论如何在Spring Boot应用程序中配置Elasticsearch数据操作。

在这里插入图片描述

Elasticsearch简介

Elasticsearch是一个开源的全文搜索和分析引擎,可以快速地存储、搜索和分析大量的数据。它基于Lucene引擎,提供了一个分布式、多租户、实时搜索和分析的能力。Elasticsearch使用JSON格式的文档来存储数据,可以通过RESTful API进行访问。

Spring Boot中的Elasticsearch数据操作

Spring Boot提供了对Elasticsearch的支持,可以通过Spring Data Elasticsearch来进行数据操作。Spring Data Elasticsearch是一个基于Spring Data的模块,提供了对Elasticsearch的集成支持。它可以通过注解或者XML文件来配置Elasticsearch的数据访问。

配置Elasticsearch客户端

在Spring Boot应用程序中使用Elasticsearch,首先需要配置Elasticsearch客户端。可以使用Elasticsearch官方提供的Java客户端或者Spring Data Elasticsearch提供的客户端。这里我们选择使用Spring Data Elasticsearch提供的客户端。

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.repository")
public class ElasticsearchConfig {
    
    

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.clustername}")
    private String clusterName;

    @Bean
    public Client client() throws Exception {
    
    

        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .build();

        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));

        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    
    
        return new ElasticsearchTemplate(client());
    }
}

在上面的代码中,我们定义了一个ElasticsearchConfig类,使用@EnableElasticsearchRepositories注解开启Elasticsearch的Repository支持。在该类中,我们通过@Value注解读取配置文件中的Elasticsearch主机、端口和集群名称。然后通过TransportClient创建Elasticsearch客户端,并将其注入到Spring容器中。

创建Elasticsearch Repository

在Spring Boot应用程序中使用Elasticsearch,需要定义一个Elasticsearch Repository。Elasticsearch Repository是一个接口,继承自ElasticsearchCrudRepository。ElasticsearchCrudRepository是Spring Data Elasticsearch提供的CrudRepository的子接口,封装了Elasticsearch的基本数据操作接口。ElasticsearchCrudRepository定义了一些基本的CRUD操作,如save、delete、findById等。

@Repository
public interface BookRepository extends ElasticsearchCrudRepository<Book, String> {
    
    

    List<Book> findByName(String name);

    List<Book> findByAuthor(String author);

    List<Book> findByCategory(String category);
}

在上面的代码中,我们定义了一个BookRepository接口,继承自ElasticsearchCrudRepository。在该接口中,我们定义了一些查询方法,如findByName、findByAuthor和findByCategory等。

创建Elasticsearch实体类

在Spring Boot应用程序中使用Elasticsearch,需要定义一个Elasticsearch实体类。Elasticsearch实体类是一个POJO类,映射到Elasticsearch中的一个文档。

@Document(indexName = "book", type = "book", shards = 1, replicas = 0, refreshInterval = "-1")
public class Book {
    
    

    @Id
    private String id;

    private String name;

    private String author;

    private String category;

    private LocalDateTime publishTime;

    // getter and setter

    // toString

}

在上面的代码中,我们定义了一个Book类,使用@Document注解指定了索引名称为book,类型为book,分片数为1,副本数为0,刷新间隔为-1。在该类中,我们定义了一些属性,如id、name、author、category和publishTime等。使用@Id注解指定了id属性为文档的id。

数据操作示例

在Spring Boot应用程序中使用Elasticsearch,可以通过Elasticsearch Repository进行数据操作。下面是一个数据操作的示例代码。

@Service
public class BookService {
    
    

    @Autowired
    private BookRepository bookRepository;

    public void save(Book book) {
    
    
        bookRepository.save(book);
    }

    public void delete(String id) {
    
    
        bookRepository.deleteById(id);
    }

    public Book findById(String id) {
    
    
        Optional<Book> optional = bookRepository.findById(id);
        return optional.orElse(null);
    }

    public List<Book> findByName(String name) {
    
    
        return bookRepository.findByName(name);
    }

    public List<Book> findByAuthor(String author) {
    
    
        return bookRepository.findByAuthor(author);
    }

    public List<Book> findByCategory(String category) {
    
    
        return bookRepository.findByCategory(category);
    }
}

在上面的代码中,我们定义了一个BookService类,使用@Autowired注解注入了BookRepository。在该类中,我们定义了一些数据操作方法,如save、delete、findById、findByName、findByAuthor和findByCategory等。这些方法直接调用Elasticsearch Repository中封装的基本数据操作方法。

总结

在本文中,我们介绍了如何在Spring Boot应用程序中配置Elasticsearch数据操作。首先我们需要配置Elasticsearch客户端,然后定义一个Elasticsearch Repository和Elasticsearch实体类。最后我们可以通过Elasticsearch Repository进行数据操作。使用Spring Boot和Elasticsearch可以快速地构建一个高效的搜索引擎应用程序。

猜你喜欢

转载自blog.csdn.net/2301_77835649/article/details/131487833