package com.kidgrow.admin.service.impl;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import com.kidgrow.admin.model.IndexDto;
import com.kidgrow.admin.model.IndexVo;
import com.kidgrow.admin.service.IIndexService;
import com.kidgrow.common.model.PageResult;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.stats.IndexStats;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.search.stats.SearchStats;
import org.elasticsearch.index.shard.DocsStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
*
* @Description: 索引
* @Project:
* @CreateDate: Created in 2020/2/24 13:47
* @Author: liuke
*/
@Service
public class IndexServiceImpl implements IIndexService {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public void create(IndexDto indexDto) {
// setting
Settings settings = Settings.builder()
.put("index.number_of_shards", indexDto.getNumberOfShards())
.put("index.number_of_replicas", indexDto.getNumberOfReplicas())
.build();
CreateIndexRequestBuilder builder = elasticsearchTemplate.getClient().admin()
.indices()
.prepareCreate(indexDto.getIndexName())
.setSettings(settings);
if (StrUtil.isNotEmpty(indexDto.getType()) && StrUtil.isNotEmpty(indexDto.getMappingsSource())) {
//mappings
builder.addMapping(indexDto.getType(), indexDto.getMappingsSource(), XContentType.JSON);
}
builder.get();
}
@Override
public void delete(String indexName) {
elasticsearchTemplate.getClient().admin().indices()
.prepareDelete(indexName)
.execute().actionGet();
}
@Override
public PageResult list(String queryStr, String... indices) {
IndicesStatsRequestBuilder indicesBuilder = elasticsearchTemplate.getClient().admin().indices()
.prepareStats(indices);
if (StrUtil.isNotEmpty(queryStr)) {
indicesBuilder.setIndices(queryStr);
}
List indexList = new ArrayList<>();
try {
IndicesStatsResponse response = indicesBuilder.execute().actionGet();
Map indicesMap = response.getIndices();
for(IndexStats stat : indicesMap.values()) {
IndexVo vo = new IndexVo();
vo.setIndexName(stat.getIndex());
//获取文档数据
DocsStats docsStats = stat.getTotal().getDocs();
vo.setDocsCount(docsStats.getCount());
vo.setDocsDeleted(docsStats.getDeleted());
//获取存储数据
vo.setStoreSizeInBytes(getKB(stat.getTotal().getStore().getSizeInBytes()));
//获取查询数据
SearchStats.Stats searchStats = stat.getTotal().getSearch().getTotal();
vo.setQueryCount(searchStats.getQueryCount());
vo.setQueryTimeInMillis(searchStats.getQueryTimeInMillis() / 1000D);
indexList.add(vo);
}
} catch (IndexNotFoundException e) {}
return PageResult.builder().data(indexList).code(0).build();
}
/**
* bytes 转换为 kb
*/
private Double getKB(Long bytes) {
if (bytes == null) {
return 0D;
}
return bytes / 1024D;
}
@Override
public Map show(String indexName) {
ImmutableOpenMap stat = elasticsearchTemplate.getClient().admin().cluster()
.prepareState().setIndices(indexName).execute().actionGet()
.getState()
.getMetaData()
.getIndices();
IndexMetaData indexMetaData = stat.get(indexName);
//获取settings数据
String settingsStr = indexMetaData.getSettings().toString();
Object settingsObj = null;
if (StrUtil.isNotEmpty(settingsStr)) {
settingsObj = JSONObject.parse(settingsStr);
}
ImmutableOpenMap mappOpenMap = indexMetaData.getMappings();
ImmutableOpenMap aliasesOpenMap = indexMetaData.getAliases();
Map result = new HashMap<>(1);
Map indexMap = new HashMap<>(3);
Map mappMap = new HashMap<>(mappOpenMap.size());
Map aliasesMap = new HashMap<>(aliasesOpenMap.size());
indexMap.put("aliases", aliasesMap);
indexMap.put("settings", settingsObj);
indexMap.put("mappings", mappMap);
result.put(indexName, indexMap);
//获取mappings数据
for (ObjectCursor key : mappOpenMap.keys()) {
MappingMetaData data = mappOpenMap.get(key.value);
Map dataMap = data.getSourceAsMap();
mappMap.put(key.value, dataMap);
}
//获取aliases数据
for (ObjectCursor key : aliasesOpenMap.keys()) {
AliasMetaData data = aliasesOpenMap.get(key.value);
aliasesMap.put(key.value, data.alias());
}
return result;
}
}