此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Data Elasticsearch 5.4.0! |
Elasticsearch作
Spring Data Elasticsearch 使用多个接口来定义可针对 Elasticsearch 索引调用的作(有关反应式接口的描述,请参见反应式 Elasticsearch作)。
-
IndexOperations
定义索引级别的作,例如创建或删除索引。 -
DocumentOperations
定义根据实体的 ID 存储、更新和检索实体的作。 -
SearchOperations
定义使用查询搜索多个实体的作 -
ElasticsearchOperations
结合DocumentOperations
和SearchOperations
接口。
这些接口对应于 Elasticsearch API 的结构。
接口的默认实现提供:
-
索引管理功能。
-
域类型的读/写映射支持。
-
丰富的查询和条件 API。
-
资源管理和异常转换。
索引管理以及索引和映射的自动创建。
这 这些作都不是由 使用 Spring Data Elasticsearch 存储库时,支持自动创建索引和写入 Map,请参阅使用相应的 Map 自动创建索引 |
使用示例
该示例演示如何使用注入的ElasticsearchOperations
实例。
该示例假定Person
是一个带有@Document
,@Id
等(请参阅 映射注记概述)。
@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 注入提供的ElasticsearchOperations Bean 的 Bean 中。 |
2 | 在 Elasticsearch 集群中存储一些实体。
该 id 是从返回的实体中读取的,因为它在person 对象,并由 Elasticsearch 创建。 |
3 | 检索具有 get by id 的实体。 |
要了解以下ElasticsearchOperations
请参阅 API 文档。
搜索结果类型
当使用DocumentOperations
接口,则只会返回找到的实体。
使用SearchOperations
接口中,每个实体都有其他信息可用,例如找到的实体的 score 或 sortValues。
为了返回此信息,每个实体都包装在SearchHit
对象,其中包含此特定于实体的附加信息。
这些SearchHit
对象本身在SearchHits
对象,该对象还包含有关整个搜索的信息,如 maxScore 或请求的聚合,或者完成请求所花费的执行持续时间。
现在提供以下类和接口:
包含以下信息:
-
ID
-
得分
-
对值进行排序
-
高亮显示字段
-
内部点击(这是一个嵌入的
SearchHits
包含最终返回的内部命中的对象) -
检索到的类型为 <T 的实体>
包含以下信息:
-
总点击数
-
Total hits 关系
-
最高分
-
一个
SearchHit<T>
对象 -
返回的聚合
-
返回的建议结果
定义 Spring DataPage
,其中包含一个SearchHits<T>
元素,并可用于使用存储库方法进行分页访问。
由ElasticsearchRestTemplate
,它丰富了SearchHits<T>
替换为 Elasticsearch 滚动 ID。
由SearchOperations
接口。
ReactiveSearchOperations
有方法返回一个Mono<ReactiveSearchHits<T>>
,它包含与SearchHits<T>
object,但会提供包含的SearchHit<T>
对象作为Flux<SearchHit<T>>
而不是列表。
查询
几乎所有在SearchOperations
和ReactiveSearchOperations
interface 采用Query
定义要执行的搜索查询的参数。Query
是一个接口,Spring Data Elasticsearch 提供了三种实现:CriteriaQuery
,StringQuery
和NativeQuery
.
CriteriaQuery 查询
CriteriaQuery
基于查询允许创建查询来搜索数据,而无需了解 Elasticsearch 查询的语法或基础知识。
它们允许用户通过简单地链接和组合来构建查询Criteria
指定搜索的文档必须满足的条件的对象。
在讨论 AND 或 OR 时,在组合条件时请记住,在 Elasticsearch 中,AND 被转换为 must 条件,OR 被转换为 should |
Criteria
它们的用法最好地通过示例来解释(假设我们有一个Book
具有price
property) 的
Criteria criteria = new Criteria("price").is(42.0);
Query query = new CriteriaQuery(criteria);
同一字段的条件可以链接,它们将与逻辑 AND 组合:
Criteria criteria = new Criteria("price").greaterThan(42.0).lessThan(34.0);
Query query = new CriteriaQuery(criteria);
当链接Criteria
,默认情况下使用 AND 逻辑:
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 且名字为 Jack 或 John 的所有人员:
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
类,以全面了解不同的可用作。
StringQuery 查询
此类将 Elasticsearch 查询作为 JSON 字符串。 以下代码显示了一个查询,该查询搜索名字为 “Jack” 的人员:
Query query = new StringQuery("{ \"match\": { \"firstname\": { \"query\": \"Jack\" } } } ");
SearchHits<Person> searchHits = operations.search(query, Person.class);
用StringQuery
如果您已经有要使用的 Elasticsearch 查询,则可能合适。
NativeQuery
NativeQuery
是当您有复杂查询或无法使用Criteria
API,例如在构建查询和使用聚合时。
它允许使用所有不同的co.elastic.clients.elasticsearch._types.query_dsl.Query
因此,将其命名为 “native” 的 Elasticsearch 库。
以下代码演示如何搜索具有给定firstName
对于找到的文档,有一个 terms 聚合,用于计算lastName
对于这些人:
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
与存储的搜索模板结合使用的界面。
有关更多信息,请参阅搜索模板支持。