Spring整合Elasticsearch----其他Elasticsearch操作支持

这篇具有很好参考价值的文章主要介绍了Spring整合Elasticsearch----其他Elasticsearch操作支持。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


本文介绍了对Elasticsearch操作的额外支持,这些操作无法通过存储库接口直接访问。建议将这些操作添加为自定义实现,如“ 自定义存储库实现”中所述。

一、索引设置

当使用Spring Data创建Elasticsearch索引时,可以使用@Setting注解定义不同的索引设置。以下参数可用:

  • useServerConfiguration 不发送任何设置参数,因此由Elasticsearch服务器配置决定。
  • settingPath指的是一个JSON文件,该文件定义了必须在类路径中解析的设置
  • shards要使用的分片数,默认为1
  • replicas复制副本的数量,默认为1
  • refreshIntervall,默认为“1s”
  • indexStoreType,默认为“fs”

也可以定义索引排序(查看链接的Elasticsearch文档中可能的字段类型和值):

@Document(indexName = "entities")
@Setting(
  sortFields = { "secondField", "firstField" },               --------1                   
  sortModes = { Setting.SortMode.max, Setting.SortMode.min }, --------2                   
  sortOrders = { Setting.SortOrder.desc, Setting.SortOrder.asc },
  sortMissingValues = { Setting.SortMissing._last, Setting.SortMissing._first })
class Entity {
    @Nullable
    @Id private String id;

    @Nullable
    @Field(name = "first_field", type = FieldType.Keyword)
    private String firstField;

    @Nullable @Field(name = "second_field", type = FieldType.Keyword)
    private String secondField;

    // getter and setter...
}

1. 定义排序字段时,请使用Java属性的名称(firstField),而不是可能为Elasticsearch定义的名称(first_field)
2. sortModes、sortOrders和sortMissingValues是可选的,但如果设置了它们,则条目的数量必须与sortFields元素的数量相匹配

二、索引映射

当Spring Data Elasticsearch使用IndexOperations.createMapping()方法创建索引映射时,它会使用Mapping Annotation Overview中描述的注解,尤其是@Field注解。除此之外,还可以将@Mapping注解添加到类中。此注解具有以下属性:

  • mappingPath JSON格式的类路径资源;如果这不是空的,它将用作映射,则不进行其他映射处理。
  • enabled 当设置为false时启用,该标志将被写入映射,并且不进行进一步处理。
  • dateDetection和numericDetection在未设置为DEFAULT时设置映射中的相应属性。
  • dynamicDateFormats当此字符串数组不为空时,它定义用于自动日期检测的日期格式。
  • runtimeFieldsPath JSON格式的类路径资源,包含写入索引映射的运行时字段的定义,例如:
{
  "day_of_week": {
    "type": "keyword",
    "script": {
      "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
    }
  }
}

三、Filter Builder

Filter Builder提高查询速度。

private ElasticsearchOperations operations;

IndexCoordinates index = IndexCoordinates.of("sample-index");

Query query = NativeQuery.builder()
	.withQuery(q -> q
		.matchAll(ma -> ma))
	.withFilter( q -> q
		.bool(b -> b
			.must(m -> m
				.term(t -> t
					.field("id")
					.value(documentId))
			)))
	.build();

SearchHits<SampleEntity> sampleEntities = operations.search(query, SampleEntity.class, index);

四、为大结果集使用滚动Scroll

Elasticsearch有一个滚动API,用于获取大块的结果集。Spring Data Elasticsearch内部使用它来提供<T> SearchHitsIterator<T> SearchOperations.searchForStream(Query query, Class<T> clazz, IndexCoordinates index)方法的实现。

IndexCoordinates index = IndexCoordinates.of("sample-index");

Query searchQuery = NativeQuery.builder()
    .withQuery(q -> q
        .matchAll(ma -> ma))
    .withFields("message")
    .withPageable(PageRequest.of(0, 10))
    .build();

SearchHitsIterator<SampleEntity> stream = elasticsearchOperations.searchForStream(searchQuery, SampleEntity.class,
index);

List<SampleEntity> sampleEntities = new ArrayList<>();
while (stream.hasNext()) {
  sampleEntities.add(stream.next());
}

stream.close();

在SearchOperations API中没有方法来访问滚动id,如果有必要访问它,可以使用AbstractElasticsearchTemplate的以下方法(这是不同ElasticsearchOperations实现的基础实现):

@Autowired ElasticsearchOperations operations;

AbstractElasticsearchTemplate template = (AbstractElasticsearchTemplate)operations;

IndexCoordinates index = IndexCoordinates.of("sample-index");

Query query = NativeQuery.builder()
    .withQuery(q -> q
        .matchAll(ma -> ma))
    .withFields("message")
    .withPageable(PageRequest.of(0, 10))
    .build();

SearchScrollHits<SampleEntity> scroll = template.searchScrollStart(1000, query, SampleEntity.class, index);

String scrollId = scroll.getScrollId();
List<SampleEntity> sampleEntities = new ArrayList<>();
while (scroll.hasSearchHits()) {
  sampleEntities.addAll(scroll.getSearchHits());
  scrollId = scroll.getScrollId();
  scroll = template.searchScrollContinue(scrollId, 1000, SampleEntity.class);
}
template.searchScrollClear(scrollId);

要将Scroll API与存储库方法一起使用,返回类型必须在Elasticsearch存储库中定义为Stream。然后,该方法的实现将使用ElasticsearchTemplate中的scroll方法。

interface SampleEntityRepository extends Repository<SampleEntity, String> {

    Stream<SampleEntity> findBy();

}

五、排序选项

除了分页和排序中描述的默认排序选项外,Spring Data Elasticsearch还提供了从“org.springframework.Data.domain.sort.Order”派生而来的类“org.springframework.Data.reasticsearch.core.query”。它提供了额外的参数,在指定结果排序时可以将这些参数发送到Elasticsearch(请参见这里)。
还有“org.springframework.data.aelasticsearch.core.query.GeoDistanceOrder”类,可用于按地理距离排序搜索操作的结果。
如果要检索的类具有名为location的GeoPoint属性,则以下排序将按到给定点的距离对结果进行排序:

Sort.by(new GeoDistanceOrder("location", new GeoPoint(48.137154, 11.5761247)))

六、运行时字段

从7.12版本开始,Elasticsearch增加了运行时字段的功能。Spring Data Elasticsearch通过两种方式支持这一点:

6.1 索引映射中的运行时字段定义

定义运行时字段的第一种方法是将定义添加到索引映射中(请参见这里)。要在Spring Data Elasticsearch中使用这种方法,用户必须提供一个包含相应定义的JSON文件,例如:
例1:runtime-fields.json

{
  "day_of_week": {
    "type": "keyword",
    "script": {
      "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
    }
  }
}

这个JSON文件的路径必须出现在类路径中,然后必须在实体的@Mapping注解中设置:

@Document(indexName = "runtime-fields")
@Mapping(runtimeFieldsPath = "/runtime-fields.json")
public class RuntimeFieldEntity {
	// properties, getter, setter,...
}

6.2 在查询上设置的运行时字段定义

定义运行时字段的第二种方法是将定义添加到搜索查询中(请参见这里)。以下代码示例展示了如何使用Spring Data Elasticsearch进行此操作:
所使用的实体是一个具有price属性的简单对象:

@Document(indexName = "some_index_name")
public class SomethingToBuy {

	private @Id @Nullable String id;
	@Nullable @Field(type = FieldType.Text) private String description;
	@Nullable @Field(type = FieldType.Double) private Double price;

	// getter and setter
}

下面的查询使用一个运行时字段,该字段通过将价格添加19%来计算priceWithTax值,并在搜索查询中使用该值来查找priceWithTax高于或等于给定值的所有实体:

RuntimeField runtimeField = new RuntimeField("priceWithTax", "double", "emit(doc['price'].value * 1.19)");
Query query = new CriteriaQuery(new Criteria("priceWithTax").greaterThanEqual(16.5));
query.addRuntimeField(runtimeField);

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

这适用于Query接口的每一个实现。

七、Point In Time (PIT) API

ElasticsearchOperations支持Elasticsearch的point in time API(请参见这里)。以下代码片段显示了如何将此功能与虚构的Person类一起使用:

ElasticsearchOperations operations; // autowired
Duration tenSeconds = Duration.ofSeconds(10);

String pit = operations.openPointInTime(IndexCoordinates.of("person"), tenSeconds); --------1

// create query for the pit
Query query1 = new CriteriaQueryBuilder(Criteria.where("lastName").is("Smith"))
    .withPointInTime(new Query.PointInTime(pit, tenSeconds))                        --------2
    .build();
SearchHits<Person> searchHits1 = operations.search(query1, Person.class);
// do something with the data

// create 2nd query for the pit, use the id returned in the previous result
Query query2 = new CriteriaQueryBuilder(Criteria.where("lastName").is("Miller"))
    .withPointInTime(
        new Query.PointInTime(searchHits1.getPointInTimeId(), tenSeconds))          --------3 
    .build();
SearchHits<Person> searchHits2 = operations.search(query2, Person.class);
// do something with the data

operations.closePointInTime(searchHits2.getPointInTimeId());                        --------4

1. 为索引(可以是多个名称)和keep-alive持续时间创建一个point in time,并检索其id
2. 将该id传递到查询中,以便与下一个keep-alive一起进行搜索
3. 对于下一个查询,使用上一次搜索返回的id
4. 完成后,使用最后返回的id关闭point in time

八、搜索模板(Template)支持

支持使用搜索模板Template API。要使用它,首先需要创建一个存储的脚本。ElasticsearchOperations接口扩展了提供必要功能的ScriptOperations。这里使用的示例假设我们有一个名为firstName的属性的Person实体。搜索模板脚本可以这样保存:

operations.putScript(                          --------1  
  Script.builder()
    .withId("person-firstname")                --------2  
    .withLanguage("mustache")                  --------3  
    .withSource("""                            --------4  
      {
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "firstName": "{{firstName}}" --------5  
                }
              }
            ]
          }
        },
        "from": "{{from}}",                    --------6  
        "size": "{{size}}"                     --------7  
      }
      """)
    .build()
);

1. 使用putScript()方法存储搜索模板脚本
2. 脚本的名称/id
3. 搜索模板中使用的脚本必须是mustache语言。
4. 脚本源
5. 脚本中的搜索参数
6. 分页请求偏移
7. 分页请求大小

为了在搜索查询中使用搜索模板,Spring Data Elasticsearch提供了SearchTemplateQuery,这是“org.springframework.data.elasticsearch.core.query.Query”接口的实现。
在下面的代码中,我们将使用搜索模板查询向自定义存储库实现添加一个调用(请参阅自定义存储库实现),作为如何将其集成到存储库调用中的示例。
我们首先定义自定义存储库片段接口:

interface PersonCustomRepository {
	SearchPage<Person> findByFirstNameWithSearchTemplate(String firstName, Pageable pageable);
}

这个存储库片段的实现看起来像这样:

public class PersonCustomRepositoryImpl implements PersonCustomRepository {

  private final ElasticsearchOperations operations;

  public PersonCustomRepositoryImpl(ElasticsearchOperations operations) {
    this.operations = operations;
  }

  @Override
  public SearchPage<Person> findByFirstNameWithSearchTemplate(String firstName, Pageable pageable) {

    var query = SearchTemplateQuery.builder()                               --------1
      .withId("person-firstname")                                           --------2
      .withParams(
        Map.of(                                                             --------3
          "firstName", firstName,
          "from", pageable.getOffset(),
          "size", pageable.getPageSize()
          )
      )
      .build();

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

    return SearchHitSupport.searchPageFor(searchHits, pageable);
  }
}

1. 创建SearchTemplateQuery
2. 提供搜索模板的id
3. 参数在Map<String,Object>中传递
4. 以与其他查询类型相同的方式进行搜索。

九、嵌套排序Nested sort

Spring Data Elasticsearch支持在嵌套对象内排序(见这里)下面的例子,摘自org.springframework.data.elasticsearch.core.query.sort.NestedSortIntegrationTests类,展示了如何定义嵌套排序。

var filter = StringQuery.builder("""
	{ "term": {"movies.actors.sex": "m"} }
	""").build();
var order = new org.springframework.data.elasticsearch.core.query.Order(Sort.Direction.DESC,
	"movies.actors.yearOfBirth")
	.withNested(
		Nested.builder("movies")
			.withNested(
				Nested.builder("movies.actors")
					.withFilter(filter)
					.build())
			.build());

var query = Query.findAll().addSort(Sort.by(order));

关于filter查询:这里不可能使用CriteriaQuery,因为此查询将被转换为Elasticsearch嵌套查询,该查询在filter上下文中不起作用。所以这里只能使用StringQuery或NativeQuery。当使用其中一个时,如上面的term查询,必须使用Elasticsearch字段名,所以当使用@Field(name=“…​”)定义重新定义这些字段时要小心。
对于排序路径和嵌套路径的定义,应使用Java实体属性名称。文章来源地址https://www.toymoban.com/news/detail-850058.html

到了这里,关于Spring整合Elasticsearch----其他Elasticsearch操作支持的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring整合Elasticsearch

            启动Elasticsearch的集群,如果不会搭建集群可以看我以前的文章          进入到head的扩展应用,连接后面的健康值为green就表示集群没问题   特征 :    Spring配置支持使用基于Java的 @Configuration 类或ES客户端实例的XML命名空间。             ElasticsearchTemplate 帮助类

    2024年02月16日
    浏览(28)
  • 35 Spring整合Elasticsearch

    spring-boot-starter-data-elasticsearch cluster-name集群名 cluster-nodes集群节点 如果项目中使用了redis,则需要解决冲突 es和redis都基于netty,这两者在启动netty时,会产生冲突:系统会认为redis已经启动了netty,es无法再启动 要尽可能在服务启动早期的时候,修改es.set.netty.runtime.available.pr

    2024年03月16日
    浏览(31)
  • Spring Boot 整合Elasticsearch入门

    Spring Data Elasticsearch 是 Spring Data 项目的子项目,提供了 Elasticsearch 与 Spring 的集成。实现了 Spring Data Repository 风格的 Elasticsearch 文档交互风格,让你轻松进行 Elasticsearch 客户端开发。 应粉丝要求特地将 Elasticsearch 整合到 Spring Boot  中去。本来打算整合到 kono 脚手架中,但是转

    2024年04月13日
    浏览(31)
  • spring boot es | spring boot 整合elasticsearch | spring boot整合多数据源es

    目录 Spring Boot与ES版本对应 Maven依赖 配置类 使用方式 @Test中注入方式 @Component中注入方式 查询文档 实体类 通过ElasticsearchRestTemplate查询 通过JPA查询 保存文档 参考链接 项目组件版本: Spring Boot:2.2.13.RELEASE Elasticsearch:6.8.0 JDK:1.8.0_66 Tips: 主要看第3列和第5列,根据ES版本选择

    2023年04月18日
    浏览(43)
  • Spring Boot整合Elasticsearch超详细教程

    SpringBoot整合Elasticsearch超详细教程 最新高级版 (1)导入springboot整合ES高级别客户端的坐标 (2)使用编程的形式设置连接的ES服务器,并获取客户端对象 (3)Book实体类 (4)连接Dao层 (5)使用客户端对象操作ES 例如创建索引:(这里需要先执行上面的删除索引操作,否则会报错)

    2023年04月09日
    浏览(40)
  • Spring Boot进阶(19):Spring Boot 整合ElasticSearch | 超级详细,建议收藏

            ElasticSearch是一款基于Lucene的开源搜索引擎,具有高效、可扩展、分布式的特点,可用于全文搜索、日志分析、数据挖掘等场景。Spring Boot作为目前最流行的微服务框架之一,也提供了对ElasticSearch的支持。本篇文章将介绍如何在Spring Boot项目中整合ElasticSearch,并展

    2024年02月06日
    浏览(31)
  • 在Spring Boot中整合Elasticsearch并实现高亮搜索

    本文详细介绍了如何在Spring Boot项目中整合Elasticsearch,实现高亮搜索功能。通过添加依赖、配置Spring Boot、为实体类添加注解,以及在Service层实现高亮搜索,读者能够了解如何在实际项目中利用Spring Boot Data Elasticsearch来操作Elasticsearch并实现高亮搜索。验证示例演示了如何使用RESTful API端点来搜索并获取包含高亮字段的用户列表,为读者提供了实际应用的参考。这篇文章将帮助读者轻松掌握Spring Boot与Elasticsearch的整合方法,从而为项目增加强大的搜索功能。

    2024年02月06日
    浏览(31)
  • Springboot --- 整合spring-data-jpa和spring-data-elasticsearch

    SpringBoot: 整合Ldap. SpringBoot: 整合Spring Data JPA. SpringBoot: 整合Elasticsearch. SpringBoot: 整合spring-data-jpa和spring-data-elasticsearch. SpringBoot: 整合thymeleaf. SpringBoot: 注入第三方jar包. SpringBoot: 整合Redis. SpringBoot: 整合slf4j打印日志. SpringBoot: 整合定时任务,自动执行方法. SpringBoot: 配置多数据源

    2023年04月25日
    浏览(48)
  • 知识点13--spring boot整合elasticsearch以及ES高亮

    本章知识点沿用知识点12的项目,介绍如何使用spring boot整合ES,没有ES的去我主页 各类型大数据集群搭建文档--大数据原生集群本地测试环境搭建三 中可以看到ES如何搭建 不管你有没有ES,最好是没有,因为一定要知道一点,一定要去官网查一下你当前用的spring boot data es的版

    2024年02月12日
    浏览(33)
  • Spring Data Elasticsearch - 在Spring应用中操作Elasticsearch数据库

    Spring Data Elasticsearch为文档的存储,查询,排序和统计提供了一个高度抽象的模板。使用Spring Data ElasticSearch来操作Elasticsearch,可以较大程度的减少我们的代码量,提高我们的开发效率。 要使用Elasticsearch我们需要引入如下依赖: 还需要在配置文件中增加如下配置 类比于MyBat

    2024年02月14日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包