ReactiveElasticsearchOperations
is the gateway to executing high level commands against an Elasticsearch cluster using the ReactiveElasticsearchClient
.
The ReactiveElasticsearchTemplate
is the default implementation of ReactiveElasticsearchOperations
.
To get started the ReactiveElasticsearchTemplate
needs to know about the actual client to work with.
Please see [elasticsearch.clients.reactive] for details on the client.
The easiest way of setting up the ReactiveElasticsearchTemplate
is via AbstractReactiveElasticsearchConfiguration
providing
dedicated configuration method hooks for base package
, the initial entity set
etc.
@Configuration
public class Config extends AbstractReactiveElasticsearchConfiguration {
@Bean (1)
@Override
public ReactiveElasticsearchClient reactiveElasticsearchClient() {
// ...
}
}
-
Configure the client to use. This can be done by
ReactiveRestClients
or directly viaDefaultReactiveElasticsearchClient
.
Note
|
If applicable set default HttpHeaders via the ClientConfiguration of the ReactiveElasticsearchClient . See [elasticsearch.clients.configuration].
|
Tip
|
If needed the ReactiveElasticsearchTemplate can be configured with default RefreshPolicy and IndicesOptions that get applied to the related requests by overriding the defaults of refreshPolicy() and indicesOptions() .
|
However one might want to be more in control over the actual components and use a more verbose approach.
@Configuration
public class Config {
@Bean (1)
public ReactiveElasticsearchClient reactiveElasticsearchClient() {
// ...
}
@Bean (2)
public ElasticsearchConverter elasticsearchConverter() {
return new MappingElasticsearchConverter(elasticsearchMappingContext());
}
@Bean (3)
public SimpleElasticsearchMappingContext elasticsearchMappingContext() {
return new SimpleElasticsearchMappingContext();
}
@Bean (4)
public ReactiveElasticsearchOperations reactiveElasticsearchOperations() {
return new ReactiveElasticsearchTemplate(reactiveElasticsearchClient(), elasticsearchConverter());
}
}
-
Configure the client to use. This can be done by
ReactiveRestClients
or directly viaDefaultReactiveElasticsearchClient
. -
Set up the
ElasticsearchConverter
used for domain type mapping utilizing metadata provided by the mapping context. -
The Elasticsearch specific mapping context for domain type metadata.
-
The actual template based on the client and conversion infrastructure.
ReactiveElasticsearchTemplate
lets you save, find and delete your domain objects and map those objects to documents stored in Elasticsearch.
Consider the following:
@Document(indexName = "marvel")
public class Person {
private @Id String id;
private String name;
private int age;
// Getter/Setter omitted...
}
template.save(new Person("Bruce Banner", 42)) (1)
.doOnNext(System.out::println)
.flatMap(person -> template.findById(person.id, Person.class)) (2)
.doOnNext(System.out::println)
.flatMap(person -> template.delete(person)) (3)
.doOnNext(System.out::println)
.flatMap(id -> template.count(Person.class)) (4)
.doOnNext(System.out::println)
.subscribe(); (5)
The above outputs the following sequence on the console.
> Person(id=QjWCWWcBXiLAnp77ksfR, name=Bruce Banner, age=42)
> Person(id=QjWCWWcBXiLAnp77ksfR, name=Bruce Banner, age=42)
> QjWCWWcBXiLAnp77ksfR
> 0
-
Insert a new
Person
document into the marvel index under type characters. Theid
is generated on server side and set into the instance returned. -
Lookup the
Person
with matchingid
in the marvel index under type characters. -
Delete the
Person
with matchingid
, extracted from the given instance, in the marvel index under type characters. -
Count the total number of documents in the marvel index under type characters.
-
Don’t forget to subscribe().