Spring Data Elasticsearch
这里主要学习的是4.4.16版本的文档
1. 版本
下表显示了 Spring Data 发行版系列使用的 Elasticsearch 版本和其中包含的 Spring Data Elasticsearch 版本,以及引用该特定 Spring Data 发行版系列的 Spring Boot 版本。给出的 Elasticsearch 版本显示了 Spring Data Elasticsearch 是使用哪些客户端库构建和测试的。
|   Spring Data Release Train  |   Spring Data Elasticsearch  |   Elasticsearch  |   Spring Framework  |   Spring Boot  | 
|   2023.0 (Ullmann)  |   5.1.x  |   8.7.1  |   6.0.x  |   3.1.x  | 
|   2022.0 (Turing)  |   5.0.x  |   8.5.3  |   6.0.x  |   3.0.x  | 
|   2021.2 (Raj)  |   4.4.x[1]  |   7.17.3  |   5.3.x  |   2.7.x  | 
|   2021.1 (Q)  |   4.3.x[1]  |   7.15.2  |   5.3.x  |   2.6.x  | 
|   2021.0 (Pascal)  |   4.2.x[1]  |   7.12.0  |   5.3.x  |   2.5.x  | 
|   2020.0 (Ockham)  |   4.1.x[1]  |   7.9.3  |   5.3.2  |   2.4.x  | 
|   Neumann  |   4.0.x[1]  |   7.6.2  |   5.2.12  |   2.3.x  | 
|   Moore  |   3.2.x[1]  |   6.8.12  |   5.2.12  |   2.2.x  | 
|   Lovelace  |   3.1.x[1]  |   6.2.2  |   5.1.19  |   2.1.x  | 
|   Kay  |   3.0.x[1]  |   5.5.0  |   5.0.13  |   2.0.x  | 
|   Ingalls  |   2.1.x[1]  |   2.4.0  |   4.3.25  |   1.5.x  | 
2. Working with Spring Data Repositories
1. Repositories
Spring Data Elasticsearch - Reference Documentation

Spring Data的repository抽象的中心接口是Repository,它是一个泛型接口,需要指定domain类和唯一标识的类型。该接口主要充当标记接口,用于捕获要使用的类型并帮助您发现扩展该接口的接口。
CrudRepository接口为管理的实体类提供了复杂的CRUD操作PagingAndSortingRepository接口在CRUD的基础上,增加了分页查询和排序功能

Spring Data Elasticsearch - Reference Documentation
2.1. Query methods
2.1.1. Query creation
自定义接口并继承ElasticsearchRepository接口,通过定义的方法名就能自动创建各种查询。
例子:
interface BookRepository extends Repository<Book, String> {
  List<Book> findByNameAndPrice(String name, Integer price);
} 
The method name above will be translated into the following Elasticsearch json query:
{
    "query": {
        "bool" : {
            "must" : [
                { "query_string" : { "query" : "?", "fields" : [ "name" ] } },
                { "query_string" : { "query" : "?", "fields" : [ "price" ] } }
            ]
        }
    }
} 
 
Supported keywords inside method names:
|   Keyword  |   Sample  |   Elasticsearch Query String  | 
|   And  |   findByNameAndPrice  |   {"bool" : {"must" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}}  | 
|   Or  |   findByNameOrPrice  |   {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}}  | 
|   Is  |   findByName  |   {"bool" : {"must" : {"field" : {"name" : "?"}}}}  | 
|   Not  |   findByNameNot  |   {"bool" : {"must_not" : {"field" : {"name" : "?"}}}}  | 
|   Between  |   findByPriceBetween  |   {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}  | 
|   LessThanEqual  |   findByPriceLessThan  |   {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}  | 
|   GreaterThanEqual  |   findByPriceGreaterThan  |   {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}}  | 
|   Before  |   findByPriceBefore  |   {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}  | 
|   After  |   findByPriceAfter  |   {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}}  | 
|   Like  |   findByNameLike  |   {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}}  | 
|   StartingWith  |   findByNameStartingWith  |   {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}}  | 
|   EndingWith  |   findByNameEndingWith  |   {"bool" : {"must" : {"field" : {"name" : {"query" : "*?","analyze_wildcard" : true}}}}}  | 
|   Contains/Containing  |   findByNameContaining  |   {"bool" : {"must" : {"field" : {"name" : {"query" : "*?*","analyze_wildcard" : true}}}}}  | 
|   In  |   findByNameIn(Collection<String>names)  |   {"bool" : {"must" : {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"name" : "?"}} ]}}}}  | 
|   NotIn  |   findByNameNotIn(Collection<String>names)  |   {"bool" : {"must_not" : {"bool" : {"should" : {"field" : {"name" : "?"}}}}}}  | 
|   Near  |   findByStoreNear  |   Not Supported Yet !  | 
|   True  |   findByAvailableTrue  |   {"bool" : {"must" : {"field" : {"available" : true}}}}  | 
|   False  |   findByAvailableFalse  |   {"bool" : {"must" : {"field" : {"available" : false}}}}  | 
|   OrderBy  |   findByAvailableTrueOrderByNameDesc  |   {"sort" : [{ "name" : {"order" : "desc"} }],"bool" : {"must" : {"field" : {"available" : true}}}}  | 
通过方法名派生的查询可能导致可读性降低,也可以实现@Query注解声明query。
如:
interface BookRepository extends ElasticsearchRepository<Book, String> {
    @Query("{\"match\": {\"name\": {\"query\": \"?0\"}}}")
    Page<Book> findByName(String name,Pageable pageable);
} 
2.1.2. Method return types
为了返回多个元素,Repository接口方法可被定义为如下返回值类型
- List<T>
 - Stream<T>
 - SearchHits<T>
 - List<SearchHit<T>>
 - Stream<SearchHit<T>>
 - SearchPage<T>
 
3. Elasticsearch Object Mapping
Spring Data Elasticsearch - Reference Documentation
Spring Data Elasticsearch 对象映射是将 Java 对象(域实体)映射到存储在 Elasticsearch 中的 JSON 表示形式并映射回来的过程。
3.1. Mapping Annotation
MappingElasticsearchConverter使用元数据来驱动对象到文档的映射,元数据取自被注解的实体属性。
如下是常用的注解:
|   注解  |   用途  |   常用参数  | 
|   @Document  |   在类级别应用以指示该类是映射到数据库的候选类。  |   indexName:存储该实体类的ES index createIndex createIndex:是否自动创建索引,默认为true  | 
|   @Id  |   应用于字段上,标记该字段为唯一标识  |   无参数  | 
|   @Field  |   应用于字段上,定义字段的属性  |   name:该字段在ES中的名称,默认为Java实体类字段名 type:文档中字段的类型,类型很多,具体参考官网,常用的有:Text, Keyword, Long, Integer, Short, Byte, Double, Float, Boolean, Date store: Flag whether the original field value should be store in Elasticsearch, default value is false. analyzer:分词器  | 
|   @Setting  |   索引设置  |   Spring Data Elasticsearch - Reference Documentation shards:分片数,默认为1  | 
4. 配置
4.1. 使用配置类
@Configuration
@EnableElasticsearchRepositories(basePackages = "org.springframework.data.elasticsearch.repository")
public class ElasticsearchConfiguration extends AbstractElasticsearchConfiguration {
    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
} 
 
问题1: elasticsearch在配置文件中写了连接配置还需要写elasticsearch配置类吗
在Spring Boot应用程序中,通常你不需要显式编写一个独立的Elasticsearch配置类,因为Spring Boot已经提供了自动配置来处理Elasticsearch连接。当你在application.properties或application.yml中提供了Elasticsearch连接信息时,Spring Boot会根据这些属性自动配置Elasticsearch连接。
问题2:spring.data.elasticsearch.repositories.enabled是什么属性?
spring.data.elasticsearch.repositories.enabled是一个Spring Boot属性,用于控制是否启用Spring Data Elasticsearch存储库(Repositories)。默认情况下,Spring Boot会自动检测你的项目中是否存在继承ElasticsearchRepository的接口,如果存在,则自动配置并启用Spring Data Elasticsearch存储库。
4.2. 在yml文件中添加配置属性
由上述两个问题可以得知,只需要配置spring.elasticsearch.uris属性,即可。
因为默认情况下,Spring Boot会自动检测你的项目中是否存在继承ElasticsearchRepository的接口,如果存在,则自动配置并启用Spring Data Elasticsearch存储库



















