提交 322fe40d 编写于 作者: zlt2000's avatar zlt2000

升级search-center支持ES7.x,不支持6.x版本

上级 c3584a31
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
<commons-io.version>2.6</commons-io.version> <commons-io.version>2.6</commons-io.version>
<servlet-api.version>4.0.1</servlet-api.version> <servlet-api.version>4.0.1</servlet-api.version>
<spring-data-elasticsearch.version>3.2.7.RELEASE</spring-data-elasticsearch.version> <spring-data-elasticsearch.version>3.2.7.RELEASE</spring-data-elasticsearch.version>
<elasticsearch.version>6.8.8</elasticsearch.version> <elasticsearch.version>7.8.0</elasticsearch.version>
<docker-maven-plugin.version>1.2.2</docker-maven-plugin.version> <docker-maven-plugin.version>1.2.2</docker-maven-plugin.version>
<sonar.exclusions>zlt-job/**/*, zlt-register/**/*, zlt-web/**/*</sonar.exclusions> <sonar.exclusions>zlt-job/**/*, zlt-register/**/*, zlt-web/**/*</sonar.exclusions>
<docker.baseImage>openjdk:8-jre-alpine</docker.baseImage> <docker.baseImage>openjdk:8-jre-alpine</docker.baseImage>
......
...@@ -2,50 +2,50 @@ package com.central.admin.service.impl; ...@@ -2,50 +2,50 @@ package com.central.admin.service.impl;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import com.central.admin.model.IndexDto; import com.central.admin.model.IndexDto;
import com.central.admin.service.IIndexService; import com.central.admin.service.IIndexService;
import com.central.common.model.PageResult; import com.central.common.model.PageResult;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Request; import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* 索引 * 索引
* *
* @author zlt * @author zlt
* @date 2019/4/23 * @date 2019/4/23
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/ */
@Service @Service
public class IndexServiceImpl implements IIndexService { public class IndexServiceImpl implements IIndexService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
private ObjectMapper mapper = new ObjectMapper(); private ObjectMapper mapper = new ObjectMapper();
private final ElasticsearchRestTemplate elasticsearchRestTemplate;
public IndexServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) {
this.elasticsearchRestTemplate = elasticsearchRestTemplate;
}
@Override @Override
public boolean create(IndexDto indexDto) throws IOException { public boolean create(IndexDto indexDto) throws IOException {
CreateIndexRequest request = new CreateIndexRequest(indexDto.getIndexName()); CreateIndexRequest request = new CreateIndexRequest(indexDto.getIndexName());
...@@ -53,9 +53,9 @@ public class IndexServiceImpl implements IIndexService { ...@@ -53,9 +53,9 @@ public class IndexServiceImpl implements IIndexService {
.put("index.number_of_shards", indexDto.getNumberOfShards()) .put("index.number_of_shards", indexDto.getNumberOfShards())
.put("index.number_of_replicas", indexDto.getNumberOfReplicas()) .put("index.number_of_replicas", indexDto.getNumberOfReplicas())
); );
if (StrUtil.isNotEmpty(indexDto.getType()) && StrUtil.isNotEmpty(indexDto.getMappingsSource())) { if (StrUtil.isNotEmpty(indexDto.getMappingsSource())) {
//mappings //mappings
request.mapping(indexDto.getType(), indexDto.getMappingsSource(), XContentType.JSON); request.mapping(indexDto.getMappingsSource(), XContentType.JSON);
} }
CreateIndexResponse response = elasticsearchRestTemplate.getClient() CreateIndexResponse response = elasticsearchRestTemplate.getClient()
.indices() .indices()
...@@ -87,24 +87,14 @@ public class IndexServiceImpl implements IIndexService { ...@@ -87,24 +87,14 @@ public class IndexServiceImpl implements IIndexService {
return PageResult.<Map<String, String>>builder().data(listOfIndicesFromEs).code(0).build(); return PageResult.<Map<String, String>>builder().data(listOfIndicesFromEs).code(0).build();
} }
/**
* bytes 转换为 kb
*/
private Double getKB(Long bytes) {
if (bytes == null) {
return 0D;
}
return bytes / 1024D;
}
@Override @Override
public Map<String, Object> show(String indexName) throws IOException { public Map<String, Object> show(String indexName) throws IOException {
GetIndexRequest request = new GetIndexRequest(); GetIndexRequest request = new GetIndexRequest(indexName);
request.indices(indexName);
GetIndexResponse getIndexResponse = elasticsearchRestTemplate.getClient() GetIndexResponse getIndexResponse = elasticsearchRestTemplate.getClient()
.indices().get(request, RequestOptions.DEFAULT); .indices().get(request, RequestOptions.DEFAULT);
ImmutableOpenMap<String, MappingMetaData> mappOpenMap = getIndexResponse.getMappings().get(indexName); MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName);
List<AliasMetaData> indexAliases = getIndexResponse.getAliases().get(indexName); Map<String, Object> mappOpenMap = mappingMetadata.getSourceAsMap();
List<AliasMetadata> indexAliases = getIndexResponse.getAliases().get(indexName);
String settingsStr = getIndexResponse.getSettings().get(indexName).toString(); String settingsStr = getIndexResponse.getSettings().get(indexName).toString();
Object settingsObj = null; Object settingsObj = null;
...@@ -113,20 +103,13 @@ public class IndexServiceImpl implements IIndexService { ...@@ -113,20 +103,13 @@ public class IndexServiceImpl implements IIndexService {
} }
Map<String, Object> result = new HashMap<>(1); Map<String, Object> result = new HashMap<>(1);
Map<String, Object> indexMap = new HashMap<>(3); Map<String, Object> indexMap = new HashMap<>(3);
Map<String, Object> mappMap = new HashMap<>(mappOpenMap.size());
List<String> aliasesList = new ArrayList<>(indexAliases.size()); List<String> aliasesList = new ArrayList<>(indexAliases.size());
indexMap.put("aliases", aliasesList); indexMap.put("aliases", aliasesList);
indexMap.put("settings", settingsObj); indexMap.put("settings", settingsObj);
indexMap.put("mappings", mappMap); indexMap.put("mappings", mappOpenMap);
result.put(indexName, indexMap); result.put(indexName, indexMap);
//获取mappings数据
for (ObjectCursor<String> key : mappOpenMap.keys()) {
MappingMetaData data = mappOpenMap.get(key.value);
Map<String, Object> dataMap = data.getSourceAsMap();
mappMap.put(key.value, dataMap);
}
//获取aliases数据 //获取aliases数据
for (AliasMetaData aliases : indexAliases) { for (AliasMetadata aliases : indexAliases) {
aliasesList.add(aliases.getAlias()); aliasesList.add(aliases.getAlias());
} }
return result; return result;
......
...@@ -14,16 +14,16 @@ import org.elasticsearch.search.aggregations.bucket.histogram.*; ...@@ -14,16 +14,16 @@ import org.elasticsearch.search.aggregations.bucket.histogram.*;
import org.elasticsearch.search.aggregations.bucket.range.ParsedDateRange; import org.elasticsearch.search.aggregations.bucket.range.ParsedDateRange;
import org.elasticsearch.search.aggregations.bucket.range.Range; import org.elasticsearch.search.aggregations.bucket.range.Range;
import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality; import org.elasticsearch.search.aggregations.metrics.ParsedCardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -35,6 +35,9 @@ import java.util.Map; ...@@ -35,6 +35,9 @@ import java.util.Map;
* *
* @author zlt * @author zlt
* @date 2019/5/7 * @date 2019/5/7
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/ */
@Service @Service
public class AggregationServiceImpl implements IAggregationService { public class AggregationServiceImpl implements IAggregationService {
...@@ -113,7 +116,7 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -113,7 +116,7 @@ public class AggregationServiceImpl implements IAggregationService {
*/ */
@Override @Override
public Map<String, Object> requestStatAgg(String indexName, String routing) throws IOException { public Map<String, Object> requestStatAgg(String indexName, String routing) throws IOException {
DateTime currDt = DateTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.now();
LocalDate localDate = LocalDate.now(); LocalDate localDate = LocalDate.now();
LocalDateTime curDateTime = LocalDateTime.now(); LocalDateTime curDateTime = LocalDateTime.now();
...@@ -126,7 +129,7 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -126,7 +129,7 @@ public class AggregationServiceImpl implements IAggregationService {
.dateRange("currDate") .dateRange("currDate")
.field("timestamp") .field("timestamp")
.addRange( .addRange(
currDt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0), currDt.plusDays(1) zonedDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0), zonedDateTime.plusDays(1)
) )
.subAggregation( .subAggregation(
AggregationBuilders AggregationBuilders
...@@ -138,16 +141,16 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -138,16 +141,16 @@ public class AggregationServiceImpl implements IAggregationService {
AggregationBuilders AggregationBuilders
.dateRange("curr24Hour") .dateRange("curr24Hour")
.field("timestamp") .field("timestamp")
.addRange(currDt.minusDays(1), currDt) .addRange(zonedDateTime.minusDays(1), zonedDateTime)
.subAggregation( .subAggregation(
//聚合并且按小时分组查询当天内的数据 //聚合并且按小时分组查询当天内的数据
AggregationBuilders AggregationBuilders
.dateHistogram("statDate") .dateHistogram("statDate")
.field("timestamp") .field("timestamp")
.dateHistogramInterval(new DateHistogramInterval("90m")) .fixedInterval(new DateHistogramInterval("90m"))
.format(CommonConstant.DATETIME_FORMAT) .format(CommonConstant.DATETIME_FORMAT)
//时区相差8小时 //时区相差8小时
.timeZone(DateTimeZone.forOffsetHours(8)) .timeZone(ZoneId.of("GMT+8"))
.minDocCount(0L) .minDocCount(0L)
.extendedBounds(new ExtendedBounds( .extendedBounds(new ExtendedBounds(
curDateTime.minusDays(1).format(DateTimeFormatter.ofPattern(CommonConstant.DATETIME_FORMAT)), curDateTime.minusDays(1).format(DateTimeFormatter.ofPattern(CommonConstant.DATETIME_FORMAT)),
...@@ -164,16 +167,16 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -164,16 +167,16 @@ public class AggregationServiceImpl implements IAggregationService {
AggregationBuilders AggregationBuilders
.dateRange("currWeek") .dateRange("currWeek")
.field("timestamp") .field("timestamp")
.addRange(currDt.minusDays(7), currDt) .addRange(zonedDateTime.minusDays(7), zonedDateTime)
.subAggregation( .subAggregation(
//聚合并且按日期分组查询7天内的数据 //聚合并且按日期分组查询7天内的数据
AggregationBuilders AggregationBuilders
.dateHistogram("statWeek") .dateHistogram("statWeek")
.field("timestamp") .field("timestamp")
.dateHistogramInterval(DateHistogramInterval.DAY) .calendarInterval(DateHistogramInterval.DAY)
.format(CommonConstant.DATE_FORMAT) .format(CommonConstant.DATE_FORMAT)
//时区相差8小时 //时区相差8小时
.timeZone(DateTimeZone.forOffsetHours(8)) .timeZone(ZoneId.of("GMT+8"))
.minDocCount(0L) .minDocCount(0L)
.extendedBounds(new ExtendedBounds( .extendedBounds(new ExtendedBounds(
localDate.minusDays(6).format(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT)), localDate.minusDays(6).format(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT)),
...@@ -190,7 +193,7 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -190,7 +193,7 @@ public class AggregationServiceImpl implements IAggregationService {
AggregationBuilders AggregationBuilders
.dateRange("currMonth") .dateRange("currMonth")
.field("timestamp") .field("timestamp")
.addRange(currDt.minusDays(30), currDt) .addRange(zonedDateTime.minusDays(30), zonedDateTime)
).aggregation( ).aggregation(
//聚合查询浏览器的数据 //聚合查询浏览器的数据
AggregationBuilders AggregationBuilders
...@@ -207,7 +210,7 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -207,7 +210,7 @@ public class AggregationServiceImpl implements IAggregationService {
.dateRange("currHour") .dateRange("currHour")
.field("timestamp") .field("timestamp")
.addRange( .addRange(
currDt.minusHours(1), currDt zonedDateTime.minusHours(1), zonedDateTime
) )
.subAggregation( .subAggregation(
AggregationBuilders AggregationBuilders
...@@ -237,7 +240,7 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -237,7 +240,7 @@ public class AggregationServiceImpl implements IAggregationService {
private void setCurrDate(Map<String, Object> result, Aggregations aggregations) { private void setCurrDate(Map<String, Object> result, Aggregations aggregations) {
ParsedDateRange currDate = aggregations.get("currDate"); ParsedDateRange currDate = aggregations.get("currDate");
Range.Bucket bucket = currDate.getBuckets().get(0); Range.Bucket bucket = currDate.getBuckets().get(0);
Cardinality cardinality = bucket.getAggregations().get("uv"); ParsedCardinality cardinality = bucket.getAggregations().get("uv");
result.put("currDate_pv", bucket.getDocCount()); result.put("currDate_pv", bucket.getDocCount());
result.put("currDate_uv", cardinality.getValue()); result.put("currDate_uv", cardinality.getValue());
} }
...@@ -293,7 +296,7 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -293,7 +296,7 @@ public class AggregationServiceImpl implements IAggregationService {
List<String> items = new ArrayList<>(); List<String> items = new ArrayList<>();
List<Long> uv = new ArrayList<>(); List<Long> uv = new ArrayList<>();
List<Long> pv = new ArrayList<>(); List<Long> pv = new ArrayList<>();
Cardinality cardinality; ParsedCardinality cardinality;
for (Histogram.Bucket bucket : agg.getBuckets()) { for (Histogram.Bucket bucket : agg.getBuckets()) {
items.add(bucket.getKeyAsString()); items.add(bucket.getKeyAsString());
pv.add(bucket.getDocCount()); pv.add(bucket.getDocCount());
...@@ -311,7 +314,7 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -311,7 +314,7 @@ public class AggregationServiceImpl implements IAggregationService {
private void setCurrHour(Map<String, Object> result, Aggregations aggregations) { private void setCurrHour(Map<String, Object> result, Aggregations aggregations) {
ParsedDateRange currDate = aggregations.get("currHour"); ParsedDateRange currDate = aggregations.get("currHour");
Range.Bucket bucket = currDate.getBuckets().get(0); Range.Bucket bucket = currDate.getBuckets().get(0);
Cardinality cardinality = bucket.getAggregations().get("uv"); ParsedCardinality cardinality = bucket.getAggregations().get("uv");
result.put("currHour_uv", cardinality.getValue()); result.put("currHour_uv", cardinality.getValue());
} }
/** /**
...@@ -322,7 +325,7 @@ public class AggregationServiceImpl implements IAggregationService { ...@@ -322,7 +325,7 @@ public class AggregationServiceImpl implements IAggregationService {
List<String> items = new ArrayList<>(); List<String> items = new ArrayList<>();
List<Long> uv = new ArrayList<>(); List<Long> uv = new ArrayList<>();
List<Long> pv = new ArrayList<>(); List<Long> pv = new ArrayList<>();
Cardinality cardinality; ParsedCardinality cardinality;
for (Histogram.Bucket bucket : agg.getBuckets()) { for (Histogram.Bucket bucket : agg.getBuckets()) {
items.add(getTimeByDatetimeStr(bucket.getKeyAsString())); items.add(getTimeByDatetimeStr(bucket.getKeyAsString()));
pv.add(bucket.getDocCount()); pv.add(bucket.getDocCount());
......
...@@ -119,9 +119,24 @@ public class SearchBuilder { ...@@ -119,9 +119,24 @@ public class SearchBuilder {
* @param limit 每页显示数 * @param limit 每页显示数
*/ */
public SearchBuilder setPage(Integer page, Integer limit) { public SearchBuilder setPage(Integer page, Integer limit) {
setPage(page, limit, false);
return this;
}
/**
* 设置分页
* @param page 当前页数
* @param limit 每页显示数
* @param trackTotalHits 分页总数是否显示所有条数,默认只显示10000
*/
public SearchBuilder setPage(Integer page, Integer limit, boolean trackTotalHits) {
if (page != null && limit != null) { if (page != null && limit != null) {
searchBuilder.from((page - 1) * limit) searchBuilder.from((page - 1) * limit)
.size(limit); .size(limit);
if (trackTotalHits) {
searchBuilder.trackTotalHits(trackTotalHits);
}
} }
return this; return this;
} }
...@@ -207,7 +222,7 @@ public class SearchBuilder { ...@@ -207,7 +222,7 @@ public class SearchBuilder {
this.setPage(page, limit); this.setPage(page, limit);
SearchResponse response = this.get(); SearchResponse response = this.get();
SearchHits searchHits = response.getHits(); SearchHits searchHits = response.getHits();
long totalCnt = searchHits.getTotalHits(); long totalCnt = searchHits.getTotalHits().value;
List<JSONObject> list = getList(searchHits); List<JSONObject> list = getList(searchHits);
return PageResult.<JSONObject>builder().data(list).code(0).count(totalCnt).build(); return PageResult.<JSONObject>builder().data(list).code(0).count(totalCnt).build();
} }
......
...@@ -18,12 +18,6 @@ ...@@ -18,12 +18,6 @@
<input type="text" name="numberOfReplicas" placeholder="请输入副本数" maxlength="20" class="layui-input"> <input type="text" name="numberOfReplicas" placeholder="请输入副本数" maxlength="20" class="layui-input">
</div> </div>
</div> </div>
<div class="layui-form-item">
<label class="layui-form-label">类型</label>
<div class="layui-input-block">
<input type="text" name="type" placeholder="请输入类型" maxlength="20" class="layui-input">
</div>
</div>
<div class="layui-form-item layui-form-text"> <div class="layui-form-item layui-form-text">
<label class="layui-form-label">mappings</label> <label class="layui-form-label">mappings</label>
<div class="layui-input-block"> <div class="layui-input-block">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册