反应式 Elasticsearch作
ReactiveElasticsearchOperations
是针对 Elasticsearch 集群执行高级命令的网关,使用ReactiveElasticsearchClient
.
这ReactiveElasticsearchTemplate
是ReactiveElasticsearchOperations
.
要开始使用,ReactiveElasticsearchOperations
需要了解要合作的实际客户。
请参阅 Reactive Rest Client 以了解有关 Client 及其配置方法的详细信息。
反应式作用法
ReactiveElasticsearchOperations
允许您保存、查找和删除域对象,并将这些对象映射到存储在 Elasticsearch 中的文档。
请考虑以下事项:
示例 1.使用 ReactiveElasticsearchOperations
@Document(indexName = "marvel")
public class Person {
private @Id String id;
private String name;
private int age;
// Getter/Setter omitted...
}
ReactiveElasticsearchOperations operations;
// ...
operations.save(new Person("Bruce Banner", 42)) (1)
.doOnNext(System.out::println)
.flatMap(person -> operations.get(person.id, Person.class)) (2)
.doOnNext(System.out::println)
.flatMap(person -> operations.delete(person)) (3)
.doOnNext(System.out::println)
.flatMap(id -> operations.count(Person.class)) (4)
.doOnNext(System.out::println)
.subscribe(); (5)
上面在控制台上输出以下序列。
> Person(id=QjWCWWcBXiLAnp77ksfR, name=Bruce Banner, age=42)
> Person(id=QjWCWWcBXiLAnp77ksfR, name=Bruce Banner, age=42)
> QjWCWWcBXiLAnp77ksfR
> 0
1 | 插入新的Person document 导入 Marvel 索引 。这id 在服务器端生成并设置到返回的实例中。 |
2 | 查找Person with matchingid 在 Marvel Index 中。 |
3 | 删除Person with matchingid ,从 marvel 索引中的给定实例中提取。 |
4 | 计算 marvel 索引中的文档总数。 |
5 | 不要忘记 subscribe() 。 |