當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring boot 实现 Elasticsearch 动态创建索引
生活随笔
收集整理的這篇文章主要介紹了
Spring boot 实现 Elasticsearch 动态创建索引
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
查找了很多方法都是通過Spring EL表達式實現 @Document(IndexName="#{demo.getIndexName}")
這種方式的問題在于沒法解決數據庫里生成的序號,例如我希望通過公司ID生成索引編號。后來在外網上找到一個大佬提出的解決方案,這位大佬把兩種方案都實現了一遍。
通過entityId自定義index
Use an index name defined by the entity to store data in Spring Data Elasticsearch 4.0
通過SpEL動態生成index
How to provide a dynamic index name in Spring Data Elasticsearch using SpEL
我這里按照entity的方式實現了需求
elasticsearch 版本7.17.3
springboot版本2.7.1
Bean
@Data @Builder @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = false) @Document(indexName = ElasticSearchIndices.ES_INDEX_TROUBLES_COMPANY+"_*",createIndex = false) public class CompanyTroubleshootingBean implements Serializable {private static final long serialVersionUID = 1L;@Id@JsonFormat(shape = JsonFormat.Shape.STRING)@JsonSerialize(using = ToStringSerializer.class)@Field(type = FieldType.Keyword , store = true)private Long id;@Field(type = FieldType.Text, analyzer = "ik_smart")private String content;@Field(type = FieldType.Text, analyzer = "ik_smart")private String searchKeyword;@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")private String contentMaxWord;@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")private String searchKeywordMaxWord;private Integer useCounts;//也是內容 這個內容不能分詞查詢@Field(type = FieldType.Keyword)private String keywordContent;@Field(type = FieldType.Keyword)private Integer catalogId;public String getContentMaxWord() {return content;}//私有化set方法,讓該屬性只讀private void setContentMaxWord(String contentMaxWord) {this.content = contentMaxWord;}public String getSearchKeywordMaxWord() {return searchKeyword;}//私有化set方法,讓該屬性只讀private void setSearchKeywordMaxWord(String searchKeywordMaxWord) {this.searchKeyword = searchKeywordMaxWord;}/*** 公司ID*/private Integer companyId;/*** 平臺的解決方案ID*/@JsonFormat(shape = JsonFormat.Shape.STRING)@JsonSerialize(using = ToStringSerializer.class)private Long platformTroubleshootingId;}接口
//相當于重新定義了一個repository public interface CompanyTroubleshootElasticRepository<T> {<S extends T> S save(S entity);<S extends T> Iterable<S> save(Iterable<S> entities); }實現類
package online.ejiang.service.impl.troubleshooting;import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import online.ejiang.enums.ElasticSearchIndices; import online.ejiang.pojo.elasticsearch.CompanyTroubleshootingBean; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.IndexOperations; import org.springframework.data.elasticsearch.core.document.Document; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.stereotype.Service;import java.util.concurrent.ConcurrentHashMap;/*** <p>* 公司故障排除及定價 服務實現類** </p>clo** @author Phil* @since 2020-07-22*/ @Service @Slf4j @RequiredArgsConstructor public class CompanyTroubleshootingRepositoryImpl implements CompanyTroubleshootElasticRepository<CompanyTroubleshootingBean> {private final ElasticsearchOperations operations;@Nullableprivate Document mapping;@Overridepublic <S extends CompanyTroubleshootingBean> S save(S entity) {IndexCoordinates indexCoordinates = getIndexCoordinates(entity);S saved = operations.save(entity, indexCoordinates);operations.indexOps(indexCoordinates).refresh();return saved;}@Overridepublic <S extends CompanyTroubleshootingBean> Iterable<S> save(Iterable<S> entities) {if (entities != null && entities.iterator().hasNext()) {IndexCoordinates indexCoordinates = getIndexCoordinates(entities.iterator().next());Iterable<S> saved = operations.save(entities, indexCoordinates);operations.indexOps(indexCoordinates).refresh();return saved;}return null;}@NonNullprivate <S extends CompanyTroubleshootingBean> IndexCoordinates getIndexCoordinates(S entity) {String indexName = ElasticSearchIndices.ES_INDEX_TROUBLES_COMPANY + "_" + entity.getCompanyId();//把單實例的Map變成每次調用都初始化,解決重新同步時需要重啟服務器的問題。ConcurrentHashMap<String, IndexCoordinates> knownIndexCoordinates = new ConcurrentHashMap<>();return knownIndexCoordinates.computeIfAbsent(indexName, i -> {IndexCoordinates indexCoordinates = IndexCoordinates.of(i);IndexOperations indexOps = operations.indexOps(indexCoordinates);if (!indexOps.exists()) {indexOps.create();if (mapping == null) {mapping = indexOps.createMapping(CompanyTroubleshootingBean.class);}indexOps.putMapping(mapping);}return indexCoordinates;});} }調用
do {IPage<CompanyTroubleshootingBean> beanPage = this.pageByCustom(new MyPage<>(index, 800), companyId);pages = beanPage.getPages();Iterable<CompanyTroubleshootingBean> saved = companyTroubleshootElasticRepository.save(beanPage.getRecords());if (saved != null && saved.iterator().hasNext()) {index++;} else {break;}} while (index <= pages);總結
以上是生活随笔為你收集整理的Spring boot 实现 Elasticsearch 动态创建索引的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vrrp路由器三种状态_VRRP路由器切
- 下一篇: SkyEye硬件模拟平台:硬件仿真实现之