Elasticsearch Repositories
本章包括 Elasticsearch 存储库实现的详细信息。
This chapter includes details of the Elasticsearch repository implementation.
.The sample Book
entity
@Document(indexName="books")
class Book {
@Id
private String id;
@Field(type = FieldType.Text)
private String name;
@Field(type = FieldType.Text)
private String summary;
@Field(type = FieldType.Integer)
private Integer price;
// getter/setter ...
}
Automatic creation of indices with the corresponding mapping
@Document
注解有一个参数为 createIndex
。如果此参数设置为 true(这是默认值),那么在启动 Spring Data Elasticsearch 启动应用程序支持时,它将在引导存储库期间检查由 @Document
注解定义的索引是否存在。
The @Document
annotation has an argument createIndex
.
If this argument is set to true - which is the default value - Spring Data Elasticsearch will during bootstrapping the repository support on application startup check if the index defined by the @Document
annotation exists.
如果它不存在,则将创建索引,并且从实体的注解派生的映射(请参阅Elasticsearch Object Mapping)将写入新创建的索引。可以通过使用`@Setting`注解设置将创建的索引的详细信息,请参阅Index settings以获取进一步的信息。
If it does not exist, the index will be created and the mappings derived from the entity’s annotations (see Elasticsearch Object Mapping) will be written to the newly created index.
Details of the index that will be created can be set by using the @Setting
annotation, refer to Index settings for further information.
Annotations for repository methods
@Highlight
存储库方法上的 @Highlight
注解定义应为返回实体的哪些字段包含突出显示。要搜索 Book
名称或摘要中的某些文本并突出显示找到的数据,可以使用以下存储库方法:
The @Highlight
annotation on a repository method defines for which fields of the returned entity highlighting should be included.To search for some text in a Book
's name or summary and have the found data highlighted, the following repository method can be used:
interface BookRepository extends Repository<Book, String> {
@Highlight(fields = {
@HighlightField(name = "name"),
@HighlightField(name = "summary")
})
SearchHits<Book> findByNameOrSummary(String text, String summary);
}
可以像上面一样定义多个要突出的字段,并且 @Highlight
和 @HighlightField
注解可以通过 @HighlightParameters
注解进一步定制。查看 Javadoc 以了解可能的配置选项。
It is possible to define multiple fields to be highlighted like above, and both the @Highlight
and the @HighlightField
annotation can further be customized with a @HighlightParameters
annotation. Check the Javadocs for the possible configuration options.
在搜索结果中,可以从 SearchHit
类检索突出显示的数据。
In the search results the highlight data can be retrieved from the SearchHit
class.
@SourceFilters
有时,用户不需要让搜索返回实体的所有属性,而只需要一个子集。Elasticsearch 提供源筛选以减少通过网络传输到应用程序的数据量。
Sometimes the user does not need to have all the properties of an entity returned from a search but only a subset. Elasticsearch provides source filtering to reduce the amount of data that is transferred across the network to the application.
在使用 Query
实现和 ElasticsearchOperations
时,通过在查询上设置源筛选可以轻松实现此目的。
When working with Query
implementations and the ElasticsearchOperations
this is easily possible by setting a
source filter on the query.
在使用存储库方法时,有 @SourceFilters
注解:
When using repository methods there is the @SourceFilters
annotation:
interface BookRepository extends Repository<Book, String> {
@SourceFilters(includes = "name")
SearchHits<Book> findByName(String text);
}
在此示例中,返回的 Book
对象的所有属性都将为 null
,除了 name。
In this example, all the properties of the returned Book
objects would be null
except the name.
Annotation based configuration
可以使用注解通过 JavaConfig 激活 Spring Data Elasticsearch 存储库支持。
The Spring Data Elasticsearch repositories support can be activated using an annotation through JavaConfig.
@Configuration
@EnableElasticsearchRepositories( 1
basePackages = "org.springframework.data.elasticsearch.repositories"
)
static class Config {
@Bean
public ElasticsearchOperations elasticsearchTemplate() { 2
// ...
}
}
class ProductService {
private ProductRepository repository; 3
public ProductService(ProductRepository repository) {
this.repository = repository;
}
public Page<Product> findAvailableBookByName(String name, Pageable pageable) {
return repository.findByAvailableTrueAndNameStartingWith(name, pageable);
}
}
1 | The EnableElasticsearchRepositories annotation activates the Repository support.
If no base package is configured, it will use the one of the configuration class it is put on. |
2 | Provide a Bean named elasticsearchTemplate of type ElasticsearchOperations by using one of the configurations shown in the Elasticsearch Operations chapter. |
3 | Let Spring inject the Repository bean into your class. |
Spring Namespace
Spring Data Elasticsearch 模块包含一个自定义命名空间,允许定义存储库 Bean 以及用于实例化 ElasticsearchServer
的元素。
The Spring Data Elasticsearch module contains a custom namespace allowing definition of repository beans as well as elements for instantiating a ElasticsearchServer
.
使用 repositories
元素查找 Spring 数据存储库,如 Creating Repository Instances 中所述。
Using the repositories
element looks up Spring Data repositories as described in Creating Repository Instances.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/elasticsearch
https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
<elasticsearch:repositories base-package="com.acme.repositories" />
</beans>
使用 Transport Client
或 Rest Client
元素在上下文中注册 Elasticsearch Server
实例。
Using the Transport Client
or Rest Client
element registers an instance of Elasticsearch Server
in the context.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/elasticsearch
https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch
https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<elasticsearch:rest-client id="restClient" hosts="http://localhost:9200">
</beans>