响应式 Elasticsearch 操作

ReactiveElasticsearchOperations 是使用 ReactiveElasticsearchClient 执行针对 Elasticsearch 集群的高级命令的网关。

ReactiveElasticsearchTemplateReactiveElasticsearchOperations 的默认实现。

首先,ReactiveElasticsearchOperations 需要知道要使用的实际客户端。有关客户端的详细信息以及如何配置它,请参阅 响应式 REST 客户端

响应式操作使用

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 marvel 索引中插入一个新的 Person 文档。id 在服务器端生成并设置到返回的实例中。
2 marvel 索引中查找匹配 idPerson
3 删除 marvel 索引中匹配 idPerson(从给定实例中提取)。
4 计算 marvel 索引中文档的总数。
5 别忘了 subscribe()
© . This site is unofficial and not affiliated with VMware.