> 文章列表 > elasticsearch-starter

elasticsearch-starter

elasticsearch-starter

使用说明
功能

  1. 引入 Spring Data Elasticsearch
  2. 支持 LocalDateTime <-> Long 时间戳参数转换

如何引入

com.hete.support
xx-elasticsearch-starter

如何配置
配置同原生配置

推荐配置如下
spring:
elasticsearch:
uris: #{ip:port}
username: #{username}
password: #{password}

使用注意事项
1 如何创建索引
● 手动编写脚本创建,不要在程序中设置自动创建。必须设置:createIndex = false
@Data
@Document(indexName = “index_demo”, createIndex = false)
public class EsPo {

}
● 创建索引一般脚本
http://账号:密码@ip:端口/索引名

{
“mappings”: {
“properties”: {
“brand”: {
“type”: “text”
},
“stringList”: {
“type”: “keyword”
},
“price”: {
“type”: “scaled_float”,
“scaling_factor”: 100
},
“diggCount”: {
“type”: “integer”
},
“createTime”: {
“type”: “date”
},
“lastUpdateTime”: {
“type”: “date”
}
}
}
}

关于keyword类型的查询
⚠️ 对于 keyword 类型,默认是大小写敏感的。部分场景需要在查询条件中忽略大小写查询,这个时候需要使用 “normalizer”: “lowercase”
参考:https://www.jianshu.com/p/bac93ca9a2a9
{
“mappings”: {
“properties”: {
“name”: {
“type”: “keyword”,
“normalizer”: “lowercase”
}
}
},
“settings”: {
“analysis”: {
“normalizer”: {
“lowercase”: {
“type”: “custom”,
“filter”: [
“lowercase”
]
}
}
}
}
}

2 写入数据
● es 的操作,封装在对应 es 的 dao 中
private final ElasticsearchOperations elasticsearchOperations;

elasticsearchOperations.save(esPo);

3 读数据
● es 的操作,封装在对应 es 的 dao 中
● 提供3种读数据的方法:https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.operations.criteriaquery
○ CriteriaQuery(推荐:简单查询)
○ StringQuery
○ NativeQuery(推荐:复杂查询,例如分桶聚合等)
private final ElasticsearchOperations elasticsearchOperations;

Criteria criteria = new Criteria(LambdaUtil.getFieldName(EsPo::getStringList)).is(“李四”);
Query query = new CriteriaQuery(criteria);
SearchHits search = elasticsearchOperations.search(query, EsPo.class);
CommonPageResult.PageInfo esPoPageInfo = EsUtil.toPageInfo(pageDto, search);

NativeSearchQuery nativeSearchQuery2 = new NativeSearchQueryBuilder()
.withAggregations()
//查询条件
.withQuery(QueryBuilders.termQuery(LambdaUtil.getFieldName(EsPo::getStringList), “李四”))

	.withPageable(EsUtil.toPageRequest(pageDto)).build();

SearchHits searchHits4 = elasticsearchOperations.search(nativeSearchQuery2, EsPo.class);

4 导出数据
● es 的操作,封装在对应 es 的 dao 中
● 数据导出,通过接入 SFDS 实现
● 业务系统需要提供2个接口
○ 查询数据总数
○ 根据查询条件查询数据
■ scroll_id 为空时,使用参数进行查询
■ scroll_id 不为空时,使用 scroll_id 进行 scroll 导出
● scroll_id 的有效期,建议为 5s ,不能超过1分钟

private final ElasticsearchRestTemplate elasticsearchRestTemplate;

if (StringUtils.isBlank(scrollId)) {
NativeSearchQuery nativeSearchQuery2 = new NativeSearchQueryBuilder()
.withAggregations()
//查询条件
.withQuery(QueryBuilders.termQuery(LambdaUtil.getFieldName(EsPo::getStringList), “李四”))
.withPageable(PageRequest.of(0, 1))
.build();
SearchScrollHits search = elasticsearchRestTemplate.searchScrollStart(5000L, nativeSearchQuery2, EsPo.class, IndexCoordinates.of(“hello_world_test_2”));
ExportationListResultBo esPoPageInfo = EsUtil.toExportationResult(search);
reurn BizConverter.INSTANCE.toVoResult(esPoPageInfo);
}

SearchScrollHits scrollHints = elasticsearchRestTemplate.searchScrollContinue(scrollId, 5000L, EsPo.class, IndexCoordinates.of(“hello_world_test_2”));
ExportationListResultBo esPoPageInfo = EsUtil.toExportationResult(scrollHints);

// 如果查询出的数量不足,意味着查询可结束,手动清掉 scroll_id 的缓存,释放 es 空间
int resultSize = Optional.ofNullable(result).map(ExportationListResultBo::getRowDataList).orElse(Collections.emptyList()).size();
if (resultSize < pageSize) {
elasticsearchRestTemplate.searchScrollClear(Lists.newArrayList(scrollId));
}

reurn BizConverter.INSTANCE.toVoResult(esPoPageInfo);

版本更新

1.0.0

  1. 引入 spring data elasticsearch
  2. 支撑 LocalDateTime 类型