Reactive Elasticsearch Repositories

Reactive Elasticsearch存储库支持基于由Reactive REST Client执行的、通过Reactive Elasticsearch Operations提供的操作的Repositories中解释的核心存储库支持的基础上构建。

Reactive Elasticsearch repository support builds on the core repository support explained in Repositories utilizing operations provided via Reactive Elasticsearch Operations executed by a Reactive REST Client.

Spring Data Elasticsearch 反应式存储库支持使用 Project Reactor 作为其首选反应式组合库。

Spring Data Elasticsearch reactive repository support uses Project Reactor as its reactive composition library of choice.

有 3 个主要要使用的接口:

There are 3 main interfaces to be used:

  • ReactiveRepository

  • ReactiveCrudRepository

  • ReactiveSortingRepository

Usage

要使用 Repository 访问存储在 Elasticsearch 中的域对象,只需创建一个接口即可。在继续操作之前,需要一个实体。

To access domain objects stored in a Elasticsearch using a Repository, just create an interface for it. Before you can actually go on and do that you will need an entity.

Example 1. Sample Person entity
public class Person {

  @Id
  private String id;
  private String firstname;
  private String lastname;
  private Address address;

  // … getters and setters omitted
}

请注意,id 属性的类型需要是 String

Please note that the id property needs to be of type String.

Example 2. Basic repository interface to persist Person entities
interface ReactivePersonRepository extends ReactiveSortingRepository<Person, String> {

  Flux<Person> findByFirstname(String firstname);                                   1

  Flux<Person> findByFirstname(Publisher<String> firstname);                        2

  Flux<Person> findByFirstnameOrderByLastname(String firstname);                    3

  Flux<Person> findByFirstname(String firstname, Sort sort);                        4

  Flux<Person> findByFirstname(String firstname, Pageable page);                    5

  Mono<Person> findByFirstnameAndLastname(String firstname, String lastname);       6

  Mono<Person> findFirstByLastname(String lastname);                                7

  @Query("{ \"bool\" : { \"must\" : { \"term\" : { \"lastname\" : \"?0\" } } } }")
  Flux<Person> findByLastname(String lastname);                                     8

  Mono<Long> countByFirstname(String firstname)                                     9

  Mono<Boolean> existsByFirstname(String firstname)                                 10

  Mono<Long> deleteByFirstname(String firstname)                                    11
}
1 The method shows a query for all people with the given lastname.
2 Finder method awaiting input from Publisher to bind parameter value for firstname.
3 Finder method ordering matching documents by lastname.
4 Finder method ordering matching documents by the expression defined via the Sort parameter.
5 Use Pageable to pass offset and sorting parameters to the database.
6 Finder method concating criteria using And / Or keywords.
7 Find the first matching entity.
8 The method shows a query for all people with the given lastname looked up by running the annotated @Query with given parameters.
9 Count all entities with matching firstname.
10 Check if at least one entity with matching firstname exists.
11 Delete all entities with matching firstname.

Configuration

对于 Java 配置,请使用 @EnableReactiveElasticsearchRepositories 注释。如果未配置基本包,该基础会扫描注释的配置类的包。

For Java configuration, use the @EnableReactiveElasticsearchRepositories annotation. If no base package is configured, the infrastructure scans the package of the annotated configuration class.

以下清单展示如何针对仓库使用 Java 配置:

The following listing shows how to use Java configuration for a repository:

Example 3. Java configuration for repositories
@Configuration
@EnableReactiveElasticsearchRepositories
public class Config extends AbstractReactiveElasticsearchConfiguration {

  @Override
  public ReactiveElasticsearchClient reactiveElasticsearchClient() {
    return ReactiveRestClients.create(ClientConfiguration.localhost());
  }
}

因为来自上一个示例的存储库扩展了 ReactiveSortingRepository,所以除了对实体的分类访问方法外,所有 CRUD 操作都是可用的。使用存储库实例是一个将依赖注入到客户端的问题,如下示例所示:

Because the repository from the previous example extends ReactiveSortingRepository, all CRUD operations are available as well as methods for sorted access to the entities. Working with the repository instance is a matter of dependency injecting it into a client, as the following example shows:

Example 4. Sorted access to Person entities
public class PersonRepositoryTests {

  @Autowired ReactivePersonRepository repository;

  @Test
  public void sortsElementsCorrectly() {

    Flux<Person> persons = repository.findAll(Sort.by(new Order(ASC, "lastname")));

    // ...
  }
}