diff --git a/pom.xml b/pom.xml index 9ea978e883bc9b7c3815488590b59f0a587ca288..78b46d208f5723bd07d7a878ac535f351c6d76d3 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ 2.6 4.0.1 3.2.7.RELEASE - 6.8.8 + 7.8.0 1.2.2 zlt-job/**/*, zlt-register/**/*, zlt-web/**/* openjdk:8-jre-alpine diff --git a/zlt-business/search-center/search-server/src/main/java/com/central/admin/service/impl/IndexServiceImpl.java b/zlt-business/search-center/search-server/src/main/java/com/central/admin/service/impl/IndexServiceImpl.java index 3639c7a2ba019c59394f047c66183a0b1a3e8604..08790812c8b59ba595b2942b145ba1e73a82abdd 100644 --- a/zlt-business/search-center/search-server/src/main/java/com/central/admin/service/impl/IndexServiceImpl.java +++ b/zlt-business/search-center/search-server/src/main/java/com/central/admin/service/impl/IndexServiceImpl.java @@ -2,50 +2,50 @@ package com.central.admin.service.impl; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; -import com.carrotsearch.hppc.cursors.ObjectCursor; import com.central.admin.model.IndexDto; import com.central.admin.service.IIndexService; import com.central.common.model.PageResult; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.http.util.EntityUtils; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; -import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; +import org.elasticsearch.client.indices.CreateIndexRequest; +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.get.GetIndexRequest; -import org.elasticsearch.action.admin.indices.get.GetIndexResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; -import org.elasticsearch.cluster.metadata.AliasMetaData; -import org.elasticsearch.cluster.metadata.MappingMetaData; -import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.cluster.metadata.AliasMetadata; +import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentType; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.stereotype.Service; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * 索引 * * @author zlt * @date 2019/4/23 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Service public class IndexServiceImpl implements IIndexService { - @Autowired - private ElasticsearchRestTemplate elasticsearchRestTemplate; - private ObjectMapper mapper = new ObjectMapper(); + private final ElasticsearchRestTemplate elasticsearchRestTemplate; + + public IndexServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) { + this.elasticsearchRestTemplate = elasticsearchRestTemplate; + } + @Override public boolean create(IndexDto indexDto) throws IOException { CreateIndexRequest request = new CreateIndexRequest(indexDto.getIndexName()); @@ -53,9 +53,9 @@ public class IndexServiceImpl implements IIndexService { .put("index.number_of_shards", indexDto.getNumberOfShards()) .put("index.number_of_replicas", indexDto.getNumberOfReplicas()) ); - if (StrUtil.isNotEmpty(indexDto.getType()) && StrUtil.isNotEmpty(indexDto.getMappingsSource())) { + if (StrUtil.isNotEmpty(indexDto.getMappingsSource())) { //mappings - request.mapping(indexDto.getType(), indexDto.getMappingsSource(), XContentType.JSON); + request.mapping(indexDto.getMappingsSource(), XContentType.JSON); } CreateIndexResponse response = elasticsearchRestTemplate.getClient() .indices() @@ -87,24 +87,14 @@ public class IndexServiceImpl implements IIndexService { return PageResult.>builder().data(listOfIndicesFromEs).code(0).build(); } - /** - * bytes 转换为 kb - */ - private Double getKB(Long bytes) { - if (bytes == null) { - return 0D; - } - return bytes / 1024D; - } - @Override public Map show(String indexName) throws IOException { - GetIndexRequest request = new GetIndexRequest(); - request.indices(indexName); + GetIndexRequest request = new GetIndexRequest(indexName); GetIndexResponse getIndexResponse = elasticsearchRestTemplate.getClient() .indices().get(request, RequestOptions.DEFAULT); - ImmutableOpenMap mappOpenMap = getIndexResponse.getMappings().get(indexName); - List indexAliases = getIndexResponse.getAliases().get(indexName); + MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName); + Map mappOpenMap = mappingMetadata.getSourceAsMap(); + List indexAliases = getIndexResponse.getAliases().get(indexName); String settingsStr = getIndexResponse.getSettings().get(indexName).toString(); Object settingsObj = null; @@ -113,20 +103,13 @@ public class IndexServiceImpl implements IIndexService { } Map result = new HashMap<>(1); Map indexMap = new HashMap<>(3); - Map mappMap = new HashMap<>(mappOpenMap.size()); List aliasesList = new ArrayList<>(indexAliases.size()); indexMap.put("aliases", aliasesList); indexMap.put("settings", settingsObj); - indexMap.put("mappings", mappMap); + indexMap.put("mappings", mappOpenMap); 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 (AliasMetaData aliases : indexAliases) { + for (AliasMetadata aliases : indexAliases) { aliasesList.add(aliases.getAlias()); } return result; diff --git a/zlt-business/search-center/search-server/src/main/java/com/central/search/service/impl/AggregationServiceImpl.java b/zlt-business/search-center/search-server/src/main/java/com/central/search/service/impl/AggregationServiceImpl.java index 9a98eac3db6e7ea977e3c6b5b2b32dffc7446853..9e6f783f893811e814363fd394e41e70518e4a8e 100644 --- a/zlt-business/search-center/search-server/src/main/java/com/central/search/service/impl/AggregationServiceImpl.java +++ b/zlt-business/search-center/search-server/src/main/java/com/central/search/service/impl/AggregationServiceImpl.java @@ -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.Range; 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.joda.time.DateTime; -import org.joda.time.DateTimeZone; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.stereotype.Service; import java.io.IOException; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.HashMap; @@ -35,6 +35,9 @@ import java.util.Map; * * @author zlt * @date 2019/5/7 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 */ @Service public class AggregationServiceImpl implements IAggregationService { @@ -113,7 +116,7 @@ public class AggregationServiceImpl implements IAggregationService { */ @Override public Map requestStatAgg(String indexName, String routing) throws IOException { - DateTime currDt = DateTime.now(); + ZonedDateTime zonedDateTime = ZonedDateTime.now(); LocalDate localDate = LocalDate.now(); LocalDateTime curDateTime = LocalDateTime.now(); @@ -126,7 +129,7 @@ public class AggregationServiceImpl implements IAggregationService { .dateRange("currDate") .field("timestamp") .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( AggregationBuilders @@ -138,16 +141,16 @@ public class AggregationServiceImpl implements IAggregationService { AggregationBuilders .dateRange("curr24Hour") .field("timestamp") - .addRange(currDt.minusDays(1), currDt) + .addRange(zonedDateTime.minusDays(1), zonedDateTime) .subAggregation( //聚合并且按小时分组查询当天内的数据 AggregationBuilders .dateHistogram("statDate") .field("timestamp") - .dateHistogramInterval(new DateHistogramInterval("90m")) + .fixedInterval(new DateHistogramInterval("90m")) .format(CommonConstant.DATETIME_FORMAT) //时区相差8小时 - .timeZone(DateTimeZone.forOffsetHours(8)) + .timeZone(ZoneId.of("GMT+8")) .minDocCount(0L) .extendedBounds(new ExtendedBounds( curDateTime.minusDays(1).format(DateTimeFormatter.ofPattern(CommonConstant.DATETIME_FORMAT)), @@ -164,16 +167,16 @@ public class AggregationServiceImpl implements IAggregationService { AggregationBuilders .dateRange("currWeek") .field("timestamp") - .addRange(currDt.minusDays(7), currDt) + .addRange(zonedDateTime.minusDays(7), zonedDateTime) .subAggregation( //聚合并且按日期分组查询7天内的数据 AggregationBuilders .dateHistogram("statWeek") .field("timestamp") - .dateHistogramInterval(DateHistogramInterval.DAY) + .calendarInterval(DateHistogramInterval.DAY) .format(CommonConstant.DATE_FORMAT) //时区相差8小时 - .timeZone(DateTimeZone.forOffsetHours(8)) + .timeZone(ZoneId.of("GMT+8")) .minDocCount(0L) .extendedBounds(new ExtendedBounds( localDate.minusDays(6).format(DateTimeFormatter.ofPattern(CommonConstant.DATE_FORMAT)), @@ -190,7 +193,7 @@ public class AggregationServiceImpl implements IAggregationService { AggregationBuilders .dateRange("currMonth") .field("timestamp") - .addRange(currDt.minusDays(30), currDt) + .addRange(zonedDateTime.minusDays(30), zonedDateTime) ).aggregation( //聚合查询浏览器的数据 AggregationBuilders @@ -207,7 +210,7 @@ public class AggregationServiceImpl implements IAggregationService { .dateRange("currHour") .field("timestamp") .addRange( - currDt.minusHours(1), currDt + zonedDateTime.minusHours(1), zonedDateTime ) .subAggregation( AggregationBuilders @@ -237,7 +240,7 @@ public class AggregationServiceImpl implements IAggregationService { private void setCurrDate(Map result, Aggregations aggregations) { ParsedDateRange currDate = aggregations.get("currDate"); 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_uv", cardinality.getValue()); } @@ -293,7 +296,7 @@ public class AggregationServiceImpl implements IAggregationService { List items = new ArrayList<>(); List uv = new ArrayList<>(); List pv = new ArrayList<>(); - Cardinality cardinality; + ParsedCardinality cardinality; for (Histogram.Bucket bucket : agg.getBuckets()) { items.add(bucket.getKeyAsString()); pv.add(bucket.getDocCount()); @@ -311,7 +314,7 @@ public class AggregationServiceImpl implements IAggregationService { private void setCurrHour(Map result, Aggregations aggregations) { ParsedDateRange currDate = aggregations.get("currHour"); 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()); } /** @@ -322,7 +325,7 @@ public class AggregationServiceImpl implements IAggregationService { List items = new ArrayList<>(); List uv = new ArrayList<>(); List pv = new ArrayList<>(); - Cardinality cardinality; + ParsedCardinality cardinality; for (Histogram.Bucket bucket : agg.getBuckets()) { items.add(getTimeByDatetimeStr(bucket.getKeyAsString())); pv.add(bucket.getDocCount()); diff --git a/zlt-commons/zlt-elasticsearch-spring-boot-starter/src/main/java/com/central/es/utils/SearchBuilder.java b/zlt-commons/zlt-elasticsearch-spring-boot-starter/src/main/java/com/central/es/utils/SearchBuilder.java index 63289c4f0426c8fe162a7925387587bbbed40a79..5550b94fb3a40ea86235d5a5e7241e7a0473daef 100644 --- a/zlt-commons/zlt-elasticsearch-spring-boot-starter/src/main/java/com/central/es/utils/SearchBuilder.java +++ b/zlt-commons/zlt-elasticsearch-spring-boot-starter/src/main/java/com/central/es/utils/SearchBuilder.java @@ -119,9 +119,24 @@ public class SearchBuilder { * @param 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) { searchBuilder.from((page - 1) * limit) .size(limit); + if (trackTotalHits) { + searchBuilder.trackTotalHits(trackTotalHits); + } + } return this; } @@ -207,7 +222,7 @@ public class SearchBuilder { this.setPage(page, limit); SearchResponse response = this.get(); SearchHits searchHits = response.getHits(); - long totalCnt = searchHits.getTotalHits(); + long totalCnt = searchHits.getTotalHits().value; List list = getList(searchHits); return PageResult.builder().data(list).code(0).count(totalCnt).build(); } diff --git a/zlt-web/back-web/src/main/resources/static/pages/search/index_manager_form.html b/zlt-web/back-web/src/main/resources/static/pages/search/index_manager_form.html index 93e93a0aadc7508fd0cca482c331c03ded15d10d..b7fed557e60f0b16e7e93280f3fd96acc0260845 100644 --- a/zlt-web/back-web/src/main/resources/static/pages/search/index_manager_form.html +++ b/zlt-web/back-web/src/main/resources/static/pages/search/index_manager_form.html @@ -18,12 +18,6 @@ -

- -
- -
-