此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data Elasticsearch 5.4.0spring-doc.cadn.net.cn

Elasticsearch作

Spring Data Elasticsearch 使用多个接口来定义可针对 Elasticsearch 索引调用的作(有关反应式接口的描述,请参见反应式 Elasticsearch作)。spring-doc.cadn.net.cn

这些接口对应于 Elasticsearch API 的结构。spring-doc.cadn.net.cn

接口的默认实现提供:spring-doc.cadn.net.cn

索引管理以及索引和映射的自动创建。

IndexOperations接口和提供的实现,可以从ElasticsearchOperationsinstance - 例如,通过调用operations.indexOps(clazz)- 使用户能够在 Elasticsearch 集群中创建索引、放置映射或存储模板和别名信息。 可以使用@Settingannotation 中,请参阅 Index settings 以了解更多信息。spring-doc.cadn.net.cn

这些作都不是IndexOperationsElasticsearchOperations. 调用这些方法是用户的责任。spring-doc.cadn.net.cn

使用 Spring Data Elasticsearch 存储库时,支持自动创建索引和写入 Map,请参阅使用相应的 Map 自动创建索引spring-doc.cadn.net.cn

使用示例

该示例演示如何使用注入的ElasticsearchOperations实例。 该示例假定Person是一个带有@Document,@Id等(请参阅 映射注记概述)。spring-doc.cadn.net.cn

示例 1.ElasticsearchOperations 使用情况
@RestController
@RequestMapping("/")
public class TestController {

  private  ElasticsearchOperations elasticsearchOperations;

  public TestController(ElasticsearchOperations elasticsearchOperations) { (1)
    this.elasticsearchOperations = elasticsearchOperations;
  }

  @PostMapping("/person")
  public String save(@RequestBody Person person) {                         (2)
    Person savedEntity = elasticsearchOperations.save(person);
    return savedEntity.getId();
  }

  @GetMapping("/person/{id}")
  public Person findById(@PathVariable("id")  Long id) {                   (3)
    Person person = elasticsearchOperations.get(id.toString(), Person.class);
    return person;
  }
}
1 让 Spring 注入提供的ElasticsearchOperationsBean 的 Bean 中。
2 在 Elasticsearch 集群中存储一些实体。 该 id 是从返回的实体中读取的,因为它在person对象,并由 Elasticsearch 创建。
3 检索具有 get by id 的实体。

要了解以下ElasticsearchOperations请参阅 API 文档。spring-doc.cadn.net.cn

搜索结果类型

当使用DocumentOperations接口,则只会返回找到的实体。 使用SearchOperations接口中,每个实体都有其他信息可用,例如找到的实体的 scoresortValuesspring-doc.cadn.net.cn

为了返回此信息,每个实体都包装在SearchHit对象,其中包含此特定于实体的附加信息。 这些SearchHit对象本身在SearchHits对象,该对象还包含有关整个搜索的信息,如 maxScore 或请求的聚合。 现在提供以下类和接口:spring-doc.cadn.net.cn

搜索 Hit<T>

包含以下信息:spring-doc.cadn.net.cn

搜索点击量<T>

包含以下信息:spring-doc.cadn.net.cn

SearchPage<T >

定义 Spring DataPage,其中包含一个SearchHits<T>元素,并可用于使用存储库方法进行分页访问。spring-doc.cadn.net.cn

SearchScrollHits<T>

ElasticsearchRestTemplate,它丰富了SearchHits<T>替换为 Elasticsearch 滚动 ID。spring-doc.cadn.net.cn

SearchHitsIterator<T>

SearchOperations接口。spring-doc.cadn.net.cn

ReactiveSearchHits (响应式搜索命中)

ReactiveSearchOperations有方法返回一个Mono<ReactiveSearchHits<T>>,它包含与SearchHits<T>object,但会提供包含的SearchHit<T>对象作为Flux<SearchHit<T>>而不是列表。spring-doc.cadn.net.cn

查询

几乎所有在SearchOperationsReactiveSearchOperationsinterface 采用Query定义要执行的搜索查询的参数。Query是一个接口,Spring Data Elasticsearch 提供了三种实现:CriteriaQuery,StringQueryNativeQuery.spring-doc.cadn.net.cn

CriteriaQuery 查询

CriteriaQuery基于查询允许创建查询来搜索数据,而无需了解 Elasticsearch 查询的语法或基础知识。 它们允许用户通过简单地链接和组合来构建查询Criteria指定搜索的文档必须满足的条件的对象。spring-doc.cadn.net.cn

在讨论 AND 或 OR 时,在组合条件时请记住,在 Elasticsearch 中,AND 被转换为 must 条件,OR 被转换为 should

Criteria它们的用法最好地通过示例来解释(假设我们有一个Book具有priceproperty) 的spring-doc.cadn.net.cn

示例 2.获取具有给定价格的图书
Criteria criteria = new Criteria("price").is(42.0);
Query query = new CriteriaQuery(criteria);

同一字段的条件可以链接,它们将与逻辑 AND 组合:spring-doc.cadn.net.cn

例 3.获取具有给定价格的图书
Criteria criteria = new Criteria("price").greaterThan(42.0).lessThan(34.0);
Query query = new CriteriaQuery(criteria);

当链接Criteria,默认情况下使用 AND 逻辑:spring-doc.cadn.net.cn

示例 4.获取名字为 James 和姓氏为 Miller 的所有人员:
Criteria criteria = new Criteria("lastname").is("Miller") (1)
  .and("firstname").is("James")                           (2)
Query query = new CriteriaQuery(criteria);
1 第一个Criteria
2 and() 会创建一个新的Criteria并将其链接到第一个 Broker。

如果要创建嵌套查询,则需要为此使用子查询。 假设我们要查找姓氏为 Miller 且名字为 JackJohn 的所有人员:spring-doc.cadn.net.cn

例 5.嵌套子查询
Criteria miller = new Criteria("lastName").is("Miller")  (1)
  .subCriteria(                                          (2)
    new Criteria().or("firstName").is("John")            (3)
      .or("firstName").is("Jack")                        (4)
  );
Query query = new CriteriaQuery(criteria);
1 创建第一个Criteria对于姓氏
2 this 与 AND 组合成一个 subCriteria
3 此子 Criteria 是名字 John 的 OR 组合
4 和名字 Jack

请参考 API 文档Criteria类,以全面了解不同的可用作。spring-doc.cadn.net.cn

StringQuery 查询

此类将 Elasticsearch 查询作为 JSON 字符串。 以下代码显示了一个查询,该查询搜索名字为 “Jack” 的人员:spring-doc.cadn.net.cn

Query query = new StringQuery("{ \"match\": { \"firstname\": { \"query\": \"Jack\" } } } ");
SearchHits<Person> searchHits = operations.search(query, Person.class);

StringQuery如果您已经有要使用的 Elasticsearch 查询,则可能合适。spring-doc.cadn.net.cn

NativeQuery

NativeQuery是当您有复杂查询或无法使用CriteriaAPI,例如在构建查询和使用聚合时。 它允许使用所有不同的co.elastic.clients.elasticsearch._types.query_dsl.Query因此,将其命名为 “native” 的 Elasticsearch 库。spring-doc.cadn.net.cn

以下代码演示如何搜索具有给定firstName对于找到的文档,有一个 terms 聚合,用于计算lastName对于这些人:spring-doc.cadn.net.cn

Query query = NativeQuery.builder()
	.withAggregation("lastNames", Aggregation.of(a -> a
		.terms(ta -> ta.field("lastName").size(10))))
	.withQuery(q -> q
		.match(m -> m
			.field("firstName")
			.query(firstName)
		)
	)
	.withPageable(pageable)
	.build();

SearchHits<Person> searchHits = operations.search(query, Person.class);

搜索模板查询

这是Query与存储的搜索模板结合使用的界面。 有关更多信息,请参阅搜索模板支持spring-doc.cadn.net.cn