优化搜索中心search-center,客户端由TransportClient改为HighLevelClient
This commit is contained in:
@@ -14,14 +14,22 @@
|
|||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-config</artifactId>
|
<artifactId>zlt-config</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>search-client</artifactId>
|
<artifactId>search-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-elasticsearch-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
@@ -31,6 +39,7 @@
|
|||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>mybatis-plus-extension</artifactId>
|
<artifactId>mybatis-plus-extension</artifactId>
|
||||||
@@ -51,11 +60,6 @@
|
|||||||
<groupId>io.micrometer</groupId>
|
<groupId>io.micrometer</groupId>
|
||||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
+5
-5
@@ -1,7 +1,6 @@
|
|||||||
package com.central.admin.controller;
|
package com.central.admin.controller;
|
||||||
|
|
||||||
import com.central.admin.model.IndexDto;
|
import com.central.admin.model.IndexDto;
|
||||||
import com.central.admin.model.IndexVo;
|
|
||||||
import com.central.admin.properties.IndexProperties;
|
import com.central.admin.properties.IndexProperties;
|
||||||
import com.central.admin.service.IIndexService;
|
import com.central.admin.service.IIndexService;
|
||||||
import com.central.common.model.PageResult;
|
import com.central.common.model.PageResult;
|
||||||
@@ -11,6 +10,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,7 +30,7 @@ public class IndexController {
|
|||||||
private IndexProperties indexProperties;
|
private IndexProperties indexProperties;
|
||||||
|
|
||||||
@PostMapping("/index")
|
@PostMapping("/index")
|
||||||
public Result createIndex(@RequestBody IndexDto indexDto) {
|
public Result createIndex(@RequestBody IndexDto indexDto) throws IOException {
|
||||||
if (indexDto.getNumberOfShards() == null) {
|
if (indexDto.getNumberOfShards() == null) {
|
||||||
indexDto.setNumberOfShards(1);
|
indexDto.setNumberOfShards(1);
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ public class IndexController {
|
|||||||
* 索引列表
|
* 索引列表
|
||||||
*/
|
*/
|
||||||
@GetMapping("/indices")
|
@GetMapping("/indices")
|
||||||
public PageResult<IndexVo> list(@RequestParam(required = false) String queryStr) {
|
public PageResult<Map<String, String>> list(@RequestParam(required = false) String queryStr) throws IOException {
|
||||||
return indexService.list(queryStr, indexProperties.getShow());
|
return indexService.list(queryStr, indexProperties.getShow());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ public class IndexController {
|
|||||||
* 索引明细
|
* 索引明细
|
||||||
*/
|
*/
|
||||||
@GetMapping("/index")
|
@GetMapping("/index")
|
||||||
public Result<Map<String, Object>> showIndex(String indexName) {
|
public Result<Map<String, Object>> showIndex(String indexName) throws IOException {
|
||||||
Map<String, Object> result = indexService.show(indexName);
|
Map<String, Object> result = indexService.show(indexName);
|
||||||
return Result.succeed(result);
|
return Result.succeed(result);
|
||||||
}
|
}
|
||||||
@@ -62,7 +62,7 @@ public class IndexController {
|
|||||||
* 删除索引
|
* 删除索引
|
||||||
*/
|
*/
|
||||||
@DeleteMapping("/index")
|
@DeleteMapping("/index")
|
||||||
public Result deleteIndex(String indexName) {
|
public Result deleteIndex(String indexName) throws IOException {
|
||||||
indexService.delete(indexName);
|
indexService.delete(indexName);
|
||||||
return Result.succeed("操作成功");
|
return Result.succeed("操作成功");
|
||||||
}
|
}
|
||||||
|
|||||||
-34
@@ -1,34 +0,0 @@
|
|||||||
package com.central.admin.model;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author zlt
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class IndexVo {
|
|
||||||
/**
|
|
||||||
* 索引名
|
|
||||||
*/
|
|
||||||
private String indexName;
|
|
||||||
/**
|
|
||||||
* 文档数
|
|
||||||
*/
|
|
||||||
private Long docsCount;
|
|
||||||
/**
|
|
||||||
* 文档删除数
|
|
||||||
*/
|
|
||||||
private Long docsDeleted;
|
|
||||||
/**
|
|
||||||
* 索引大小(kb)
|
|
||||||
*/
|
|
||||||
private Double storeSizeInBytes;
|
|
||||||
/**
|
|
||||||
* 总查询数
|
|
||||||
*/
|
|
||||||
private Long queryCount;
|
|
||||||
/**
|
|
||||||
* 总查询耗时(s)
|
|
||||||
*/
|
|
||||||
private Double queryTimeInMillis;
|
|
||||||
}
|
|
||||||
+1
-1
@@ -18,5 +18,5 @@ public class IndexProperties {
|
|||||||
/**
|
/**
|
||||||
* 配置过滤的索引名:默认只显示这些索引
|
* 配置过滤的索引名:默认只显示这些索引
|
||||||
*/
|
*/
|
||||||
private String[] show;
|
private String show;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -1,9 +1,9 @@
|
|||||||
package com.central.admin.service;
|
package com.central.admin.service;
|
||||||
|
|
||||||
import com.central.admin.model.IndexDto;
|
import com.central.admin.model.IndexDto;
|
||||||
import com.central.admin.model.IndexVo;
|
|
||||||
import com.central.common.model.PageResult;
|
import com.central.common.model.PageResult;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,24 +16,24 @@ public interface IIndexService {
|
|||||||
/**
|
/**
|
||||||
* 创建索引
|
* 创建索引
|
||||||
*/
|
*/
|
||||||
void create(IndexDto indexDto);
|
boolean create(IndexDto indexDto) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除索引
|
* 删除索引
|
||||||
* @param indexName 索引名
|
* @param indexName 索引名
|
||||||
*/
|
*/
|
||||||
void delete(String indexName);
|
boolean delete(String indexName) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 索引列表
|
* 索引列表
|
||||||
* @param queryStr 搜索字符串
|
* @param queryStr 搜索字符串
|
||||||
* @param indices 默认显示的索引名
|
* @param indices 默认显示的索引名
|
||||||
*/
|
*/
|
||||||
PageResult<IndexVo> list(String queryStr, String... indices);
|
PageResult<Map<String, String>> list(String queryStr, String indices) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 显示索引明细
|
* 显示索引明细
|
||||||
* @param indexName 索引名
|
* @param indexName 索引名
|
||||||
*/
|
*/
|
||||||
Map<String, Object> show(String indexName);
|
Map<String, Object> show(String indexName) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-68
@@ -4,26 +4,30 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.carrotsearch.hppc.cursors.ObjectCursor;
|
import com.carrotsearch.hppc.cursors.ObjectCursor;
|
||||||
import com.central.admin.model.IndexDto;
|
import com.central.admin.model.IndexDto;
|
||||||
import com.central.admin.model.IndexVo;
|
|
||||||
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 org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import org.elasticsearch.action.admin.indices.stats.IndexStats;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
|
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
||||||
|
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
||||||
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||||
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
|
||||||
|
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
|
||||||
|
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
|
||||||
|
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.AliasMetaData;
|
||||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
|
||||||
import org.elasticsearch.cluster.metadata.MappingMetaData;
|
import org.elasticsearch.cluster.metadata.MappingMetaData;
|
||||||
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
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.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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -38,62 +42,49 @@ import java.util.Map;
|
|||||||
@Service
|
@Service
|
||||||
public class IndexServiceImpl implements IIndexService {
|
public class IndexServiceImpl implements IIndexService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
private ElasticsearchRestTemplate elasticsearchRestTemplate;
|
||||||
|
|
||||||
|
private ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create(IndexDto indexDto) {
|
public boolean create(IndexDto indexDto) throws IOException {
|
||||||
// setting
|
CreateIndexRequest request = new CreateIndexRequest(indexDto.getIndexName());
|
||||||
Settings settings = Settings.builder()
|
request.settings(Settings.builder()
|
||||||
.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())
|
||||||
.build();
|
);
|
||||||
CreateIndexRequestBuilder builder = elasticsearchTemplate.getClient().admin()
|
|
||||||
.indices()
|
|
||||||
.prepareCreate(indexDto.getIndexName())
|
|
||||||
.setSettings(settings);
|
|
||||||
if (StrUtil.isNotEmpty(indexDto.getType()) && StrUtil.isNotEmpty(indexDto.getMappingsSource())) {
|
if (StrUtil.isNotEmpty(indexDto.getType()) && StrUtil.isNotEmpty(indexDto.getMappingsSource())) {
|
||||||
//mappings
|
//mappings
|
||||||
builder.addMapping(indexDto.getType(), indexDto.getMappingsSource(), XContentType.JSON);
|
request.mapping(indexDto.getType(), indexDto.getMappingsSource(), XContentType.JSON);
|
||||||
}
|
}
|
||||||
builder.get();
|
CreateIndexResponse response = elasticsearchRestTemplate.getClient()
|
||||||
|
.indices()
|
||||||
|
.create(request, RequestOptions.DEFAULT);
|
||||||
|
return response.isAcknowledged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(String indexName) {
|
public boolean delete(String indexName) throws IOException {
|
||||||
elasticsearchTemplate.getClient().admin().indices()
|
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
|
||||||
.prepareDelete(indexName)
|
DeleteIndexResponse response = elasticsearchRestTemplate.getClient().indices().delete(request, RequestOptions.DEFAULT);
|
||||||
.execute().actionGet();
|
return response.isAcknowledged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResult<IndexVo> list(String queryStr, String... indices) {
|
public PageResult<Map<String, String>> list(String queryStr, String indices) throws IOException {
|
||||||
IndicesStatsRequestBuilder indicesBuilder = elasticsearchTemplate.getClient().admin().indices()
|
Response response = elasticsearchRestTemplate.getClient().getLowLevelClient()
|
||||||
.prepareStats(indices);
|
.performRequest(new Request(
|
||||||
if (StrUtil.isNotEmpty(queryStr)) {
|
"GET",
|
||||||
indicesBuilder.setIndices(queryStr);
|
"/_cat/indices?h=health,status,index,docsCount,docsDeleted,storeSize&s=cds:desc&format=json&index="+StrUtil.nullToEmpty(indices)
|
||||||
}
|
));
|
||||||
List<IndexVo> indexList = new ArrayList<>();
|
|
||||||
try {
|
|
||||||
IndicesStatsResponse response = indicesBuilder.execute().actionGet();
|
|
||||||
Map<String, IndexStats> 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);
|
List<Map<String, String>> listOfIndicesFromEs = null;
|
||||||
|
if (response != null) {
|
||||||
|
String rawBody = EntityUtils.toString(response.getEntity());
|
||||||
|
TypeReference<List<HashMap<String, String>>> typeRef = new TypeReference<List<HashMap<String, String>>>() {};
|
||||||
|
listOfIndicesFromEs = mapper.readValue(rawBody, typeRef);
|
||||||
}
|
}
|
||||||
} catch (IndexNotFoundException e) {}
|
return PageResult.<Map<String, String>>builder().data(listOfIndicesFromEs).code(0).build();
|
||||||
return PageResult.<IndexVo>builder().data(indexList).code(0).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,28 +98,24 @@ public class IndexServiceImpl implements IIndexService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> show(String indexName) {
|
public Map<String, Object> show(String indexName) throws IOException {
|
||||||
ImmutableOpenMap<String, IndexMetaData> stat = elasticsearchTemplate.getClient().admin().cluster()
|
GetIndexRequest request = new GetIndexRequest();
|
||||||
.prepareState().setIndices(indexName).execute().actionGet()
|
request.indices(indexName);
|
||||||
.getState()
|
GetIndexResponse getIndexResponse = elasticsearchRestTemplate.getClient()
|
||||||
.getMetaData()
|
.indices().get(request, RequestOptions.DEFAULT);
|
||||||
.getIndices();
|
ImmutableOpenMap<String, MappingMetaData> mappOpenMap = getIndexResponse.getMappings().get(indexName);
|
||||||
|
List<AliasMetaData> indexAliases = getIndexResponse.getAliases().get(indexName);
|
||||||
|
|
||||||
IndexMetaData indexMetaData = stat.get(indexName);
|
String settingsStr = getIndexResponse.getSettings().get(indexName).toString();
|
||||||
//获取settings数据
|
|
||||||
String settingsStr = indexMetaData.getSettings().toString();
|
|
||||||
Object settingsObj = null;
|
Object settingsObj = null;
|
||||||
if (StrUtil.isNotEmpty(settingsStr)) {
|
if (StrUtil.isNotEmpty(settingsStr)) {
|
||||||
settingsObj = JSONObject.parse(settingsStr);
|
settingsObj = JSONObject.parse(settingsStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ImmutableOpenMap<String, MappingMetaData> mappOpenMap = indexMetaData.getMappings();
|
|
||||||
ImmutableOpenMap<String, AliasMetaData> aliasesOpenMap = indexMetaData.getAliases();
|
|
||||||
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());
|
Map<String, Object> mappMap = new HashMap<>(mappOpenMap.size());
|
||||||
Map<String, Object> aliasesMap = new HashMap<>(aliasesOpenMap.size());
|
List<String> aliasesList = new ArrayList<>(indexAliases.size());
|
||||||
indexMap.put("aliases", aliasesMap);
|
indexMap.put("aliases", aliasesList);
|
||||||
indexMap.put("settings", settingsObj);
|
indexMap.put("settings", settingsObj);
|
||||||
indexMap.put("mappings", mappMap);
|
indexMap.put("mappings", mappMap);
|
||||||
result.put(indexName, indexMap);
|
result.put(indexName, indexMap);
|
||||||
@@ -139,9 +126,8 @@ public class IndexServiceImpl implements IIndexService {
|
|||||||
mappMap.put(key.value, dataMap);
|
mappMap.put(key.value, dataMap);
|
||||||
}
|
}
|
||||||
//获取aliases数据
|
//获取aliases数据
|
||||||
for (ObjectCursor<String> key : aliasesOpenMap.keys()) {
|
for (AliasMetaData aliases : indexAliases) {
|
||||||
AliasMetaData data = aliasesOpenMap.get(key.value);
|
aliasesList.add(aliases.getAlias());
|
||||||
aliasesMap.put(key.value, data.alias());
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-4
@@ -3,9 +3,9 @@ package com.central.search.controller;
|
|||||||
import com.central.search.service.IAggregationService;
|
import com.central.search.service.IAggregationService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,8 +19,11 @@ import java.util.Map;
|
|||||||
@Api(tags = "搜索模块api")
|
@Api(tags = "搜索模块api")
|
||||||
@RequestMapping("/agg")
|
@RequestMapping("/agg")
|
||||||
public class AggregationController {
|
public class AggregationController {
|
||||||
@Autowired
|
private final IAggregationService aggregationService;
|
||||||
private IAggregationService aggregationService;
|
|
||||||
|
public AggregationController(IAggregationService aggregationService) {
|
||||||
|
this.aggregationService = aggregationService;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 访问统计聚合查询
|
* 访问统计聚合查询
|
||||||
@@ -28,7 +31,7 @@ public class AggregationController {
|
|||||||
* @param routing es的路由
|
* @param routing es的路由
|
||||||
*/
|
*/
|
||||||
@GetMapping("/requestStat/{indexName}/{routing}")
|
@GetMapping("/requestStat/{indexName}/{routing}")
|
||||||
public Map<String, Object> requestStatAgg(@PathVariable String indexName, @PathVariable String routing) {
|
public Map<String, Object> requestStatAgg(@PathVariable String indexName, @PathVariable String routing) throws IOException {
|
||||||
return aggregationService.requestStatAgg(indexName, routing);
|
return aggregationService.requestStatAgg(indexName, routing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+8
-4
@@ -5,12 +5,13 @@ import com.central.common.model.*;
|
|||||||
|
|
||||||
import com.central.search.model.SearchDto;
|
import com.central.search.model.SearchDto;
|
||||||
import com.central.search.service.ISearchService;
|
import com.central.search.service.ISearchService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用搜索
|
* 通用搜索
|
||||||
*
|
*
|
||||||
@@ -21,8 +22,11 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Api(tags = "搜索模块api")
|
@Api(tags = "搜索模块api")
|
||||||
@RequestMapping("/search")
|
@RequestMapping("/search")
|
||||||
public class SearchController {
|
public class SearchController {
|
||||||
@Autowired
|
private final ISearchService searchService;
|
||||||
private ISearchService searchService;
|
|
||||||
|
public SearchController(ISearchService searchService) {
|
||||||
|
this.searchService = searchService;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询文档列表
|
* 查询文档列表
|
||||||
@@ -30,7 +34,7 @@ public class SearchController {
|
|||||||
* @param searchDto 搜索Dto
|
* @param searchDto 搜索Dto
|
||||||
*/
|
*/
|
||||||
@PostMapping("/{indexName}")
|
@PostMapping("/{indexName}")
|
||||||
public PageResult<JSONObject> strQuery(@PathVariable String indexName, @RequestBody(required = false) SearchDto searchDto) {
|
public PageResult<JSONObject> strQuery(@PathVariable String indexName, @RequestBody(required = false) SearchDto searchDto) throws IOException {
|
||||||
if (searchDto == null) {
|
if (searchDto == null) {
|
||||||
searchDto = new SearchDto();
|
searchDto = new SearchDto();
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
package com.central.search.service;
|
package com.central.search.service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,5 +14,5 @@ public interface IAggregationService {
|
|||||||
* @param routing es的路由
|
* @param routing es的路由
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
Map<String, Object> requestStatAgg(String indexName, String routing);
|
Map<String, Object> requestStatAgg(String indexName, String routing) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject;
|
|||||||
import com.central.common.model.PageResult;
|
import com.central.common.model.PageResult;
|
||||||
import com.central.search.model.SearchDto;
|
import com.central.search.model.SearchDto;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2019/4/24
|
* @date 2019/4/24
|
||||||
@@ -15,5 +17,5 @@ public interface ISearchService {
|
|||||||
* @param searchDto 搜索Dto
|
* @param searchDto 搜索Dto
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
PageResult<JSONObject> strQuery(String indexName, SearchDto searchDto);
|
PageResult<JSONObject> strQuery(String indexName, SearchDto searchDto) throws IOException;
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-42
@@ -4,22 +4,24 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.central.common.constant.CommonConstant;
|
import com.central.common.constant.CommonConstant;
|
||||||
import com.central.search.model.AggItemVo;
|
import com.central.search.model.AggItemVo;
|
||||||
import com.central.search.service.IAggregationService;
|
import com.central.search.service.IAggregationService;
|
||||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
import org.elasticsearch.client.RequestOptions;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||||
import org.elasticsearch.search.aggregations.Aggregations;
|
import org.elasticsearch.search.aggregations.Aggregations;
|
||||||
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
|
import org.elasticsearch.search.aggregations.bucket.histogram.*;
|
||||||
import org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBounds;
|
import org.elasticsearch.search.aggregations.bucket.range.ParsedDateRange;
|
||||||
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
|
import org.elasticsearch.search.aggregations.bucket.range.Range;
|
||||||
import org.elasticsearch.search.aggregations.bucket.range.InternalDateRange;
|
|
||||||
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.cardinality.Cardinality;
|
||||||
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.joda.time.DateTimeZone;
|
import org.joda.time.DateTimeZone;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
@@ -36,8 +38,11 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class AggregationServiceImpl implements IAggregationService {
|
public class AggregationServiceImpl implements IAggregationService {
|
||||||
@Autowired
|
private final ElasticsearchRestTemplate elasticsearchRestTemplate;
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
|
||||||
|
public AggregationServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) {
|
||||||
|
this.elasticsearchRestTemplate = elasticsearchRestTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 访问统计聚合查询,需要es里面提供以下结构的数据
|
* 访问统计聚合查询,需要es里面提供以下结构的数据
|
||||||
@@ -107,13 +112,15 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> requestStatAgg(String indexName, String routing) {
|
public Map<String, Object> requestStatAgg(String indexName, String routing) throws IOException {
|
||||||
DateTime currDt = DateTime.now();
|
DateTime currDt = DateTime.now();
|
||||||
LocalDate localDate = LocalDate.now();
|
LocalDate localDate = LocalDate.now();
|
||||||
LocalDateTime curDateTime = LocalDateTime.now();
|
LocalDateTime curDateTime = LocalDateTime.now();
|
||||||
SearchRequestBuilder searchRequestBuilder = elasticsearchTemplate.getClient().prepareSearch(indexName)
|
|
||||||
.setRouting(routing)
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||||
.addAggregation(
|
SearchRequest searchRequest = new SearchRequest(indexName);
|
||||||
|
searchRequest.source(searchSourceBuilder).routing(routing);
|
||||||
|
searchSourceBuilder.aggregation(
|
||||||
//聚合查询当天的数据
|
//聚合查询当天的数据
|
||||||
AggregationBuilders
|
AggregationBuilders
|
||||||
.dateRange("currDate")
|
.dateRange("currDate")
|
||||||
@@ -126,8 +133,7 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
.cardinality("uv")
|
.cardinality("uv")
|
||||||
.field("ip.keyword")
|
.field("ip.keyword")
|
||||||
)
|
)
|
||||||
)
|
).aggregation(
|
||||||
.addAggregation(
|
|
||||||
//聚合查询7天内的数据
|
//聚合查询7天内的数据
|
||||||
AggregationBuilders
|
AggregationBuilders
|
||||||
.dateRange("curr24Hour")
|
.dateRange("curr24Hour")
|
||||||
@@ -153,8 +159,7 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
.field("ip.keyword")
|
.field("ip.keyword")
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
).aggregation(
|
||||||
.addAggregation(
|
|
||||||
//聚合查询7天内的数据
|
//聚合查询7天内的数据
|
||||||
AggregationBuilders
|
AggregationBuilders
|
||||||
.dateRange("currWeek")
|
.dateRange("currWeek")
|
||||||
@@ -180,27 +185,23 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
.field("ip.keyword")
|
.field("ip.keyword")
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
).aggregation(
|
||||||
.addAggregation(
|
|
||||||
//聚合查询30天内的数据
|
//聚合查询30天内的数据
|
||||||
AggregationBuilders
|
AggregationBuilders
|
||||||
.dateRange("currMonth")
|
.dateRange("currMonth")
|
||||||
.field("timestamp")
|
.field("timestamp")
|
||||||
.addRange(currDt.minusDays(30), currDt)
|
.addRange(currDt.minusDays(30), currDt)
|
||||||
)
|
).aggregation(
|
||||||
.addAggregation(
|
|
||||||
//聚合查询浏览器的数据
|
//聚合查询浏览器的数据
|
||||||
AggregationBuilders
|
AggregationBuilders
|
||||||
.terms("browser")
|
.terms("browser")
|
||||||
.field("browser.keyword")
|
.field("browser.keyword")
|
||||||
)
|
).aggregation(
|
||||||
.addAggregation(
|
|
||||||
//聚合查询操作系统的数据
|
//聚合查询操作系统的数据
|
||||||
AggregationBuilders
|
AggregationBuilders
|
||||||
.terms("operatingSystem")
|
.terms("operatingSystem")
|
||||||
.field("operatingSystem.keyword")
|
.field("operatingSystem.keyword")
|
||||||
)
|
).aggregation(
|
||||||
.addAggregation(
|
|
||||||
//聚合查询1小时内的数据
|
//聚合查询1小时内的数据
|
||||||
AggregationBuilders
|
AggregationBuilders
|
||||||
.dateRange("currHour")
|
.dateRange("currHour")
|
||||||
@@ -213,10 +214,10 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
.cardinality("uv")
|
.cardinality("uv")
|
||||||
.field("ip.keyword")
|
.field("ip.keyword")
|
||||||
)
|
)
|
||||||
)
|
).size(0);
|
||||||
.setSize(0);
|
|
||||||
|
|
||||||
SearchResponse response = searchRequestBuilder.get();
|
RestHighLevelClient client = elasticsearchRestTemplate.getClient();
|
||||||
|
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
Aggregations aggregations = response.getAggregations();
|
Aggregations aggregations = response.getAggregations();
|
||||||
Map<String, Object> result = new HashMap<>(15);
|
Map<String, Object> result = new HashMap<>(15);
|
||||||
if (aggregations != null) {
|
if (aggregations != null) {
|
||||||
@@ -234,8 +235,8 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
* 赋值当天统计
|
* 赋值当天统计
|
||||||
*/
|
*/
|
||||||
private void setCurrDate(Map<String, Object> result, Aggregations aggregations) {
|
private void setCurrDate(Map<String, Object> result, Aggregations aggregations) {
|
||||||
InternalDateRange currDate = aggregations.get("currDate");
|
ParsedDateRange currDate = aggregations.get("currDate");
|
||||||
InternalDateRange.Bucket bucket = currDate.getBuckets().get(0);
|
Range.Bucket bucket = currDate.getBuckets().get(0);
|
||||||
Cardinality cardinality = bucket.getAggregations().get("uv");
|
Cardinality 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());
|
||||||
@@ -244,8 +245,8 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
* 赋值周统计
|
* 赋值周统计
|
||||||
*/
|
*/
|
||||||
private void setCurr24Hour(Map<String, Object> result, Aggregations aggregations) {
|
private void setCurr24Hour(Map<String, Object> result, Aggregations aggregations) {
|
||||||
InternalDateRange curr24Hour = aggregations.get("curr24Hour");
|
ParsedDateRange curr24Hour = aggregations.get("curr24Hour");
|
||||||
InternalDateRange.Bucket bucket = curr24Hour.getBuckets().get(0);
|
Range.Bucket bucket = curr24Hour.getBuckets().get(0);
|
||||||
//赋值天趋势统计
|
//赋值天趋势统计
|
||||||
setStatDate(result, bucket.getAggregations());
|
setStatDate(result, bucket.getAggregations());
|
||||||
}
|
}
|
||||||
@@ -253,8 +254,8 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
* 赋值周统计
|
* 赋值周统计
|
||||||
*/
|
*/
|
||||||
private void setCurrWeek(Map<String, Object> result, Aggregations aggregations) {
|
private void setCurrWeek(Map<String, Object> result, Aggregations aggregations) {
|
||||||
InternalDateRange currWeek = aggregations.get("currWeek");
|
ParsedDateRange currWeek = aggregations.get("currWeek");
|
||||||
InternalDateRange.Bucket bucket = currWeek.getBuckets().get(0);
|
Range.Bucket bucket = currWeek.getBuckets().get(0);
|
||||||
result.put("currWeek_pv", bucket.getDocCount());
|
result.put("currWeek_pv", bucket.getDocCount());
|
||||||
//赋值周趋势统计
|
//赋值周趋势统计
|
||||||
setStatWeek(result, bucket.getAggregations());
|
setStatWeek(result, bucket.getAggregations());
|
||||||
@@ -263,8 +264,8 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
* 赋值月统计
|
* 赋值月统计
|
||||||
*/
|
*/
|
||||||
private void setCurrMonth(Map<String, Object> result, Aggregations aggregations) {
|
private void setCurrMonth(Map<String, Object> result, Aggregations aggregations) {
|
||||||
InternalDateRange currMonth = aggregations.get("currMonth");
|
ParsedDateRange currMonth = aggregations.get("currMonth");
|
||||||
InternalDateRange.Bucket bucket = currMonth.getBuckets().get(0);
|
Range.Bucket bucket = currMonth.getBuckets().get(0);
|
||||||
result.put("currMonth_pv", bucket.getDocCount());
|
result.put("currMonth_pv", bucket.getDocCount());
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -288,12 +289,12 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
* 赋值周趋势统计
|
* 赋值周趋势统计
|
||||||
*/
|
*/
|
||||||
private void setStatWeek(Map<String, Object> result, Aggregations aggregations) {
|
private void setStatWeek(Map<String, Object> result, Aggregations aggregations) {
|
||||||
InternalDateHistogram agg = aggregations.get("statWeek");
|
ParsedDateHistogram agg = aggregations.get("statWeek");
|
||||||
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;
|
Cardinality cardinality;
|
||||||
for (InternalDateHistogram.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());
|
||||||
|
|
||||||
@@ -308,8 +309,8 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
* 赋值小时内统计-当前在线数
|
* 赋值小时内统计-当前在线数
|
||||||
*/
|
*/
|
||||||
private void setCurrHour(Map<String, Object> result, Aggregations aggregations) {
|
private void setCurrHour(Map<String, Object> result, Aggregations aggregations) {
|
||||||
InternalDateRange currDate = aggregations.get("currHour");
|
ParsedDateRange currDate = aggregations.get("currHour");
|
||||||
InternalDateRange.Bucket bucket = currDate.getBuckets().get(0);
|
Range.Bucket bucket = currDate.getBuckets().get(0);
|
||||||
Cardinality cardinality = bucket.getAggregations().get("uv");
|
Cardinality cardinality = bucket.getAggregations().get("uv");
|
||||||
result.put("currHour_uv", cardinality.getValue());
|
result.put("currHour_uv", cardinality.getValue());
|
||||||
}
|
}
|
||||||
@@ -317,12 +318,12 @@ public class AggregationServiceImpl implements IAggregationService {
|
|||||||
* 赋值天趋势统计
|
* 赋值天趋势统计
|
||||||
*/
|
*/
|
||||||
private void setStatDate(Map<String, Object> result, Aggregations aggregations) {
|
private void setStatDate(Map<String, Object> result, Aggregations aggregations) {
|
||||||
InternalDateHistogram agg = aggregations.get("statDate");
|
ParsedDateHistogram agg = aggregations.get("statDate");
|
||||||
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;
|
Cardinality cardinality;
|
||||||
for (InternalDateHistogram.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());
|
||||||
|
|
||||||
|
|||||||
+11
-7
@@ -2,14 +2,15 @@ package com.central.search.service.impl;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.central.common.model.PageResult;
|
import com.central.common.model.PageResult;
|
||||||
|
import com.central.es.utils.SearchBuilder;
|
||||||
import com.central.search.model.SearchDto;
|
import com.central.search.model.SearchDto;
|
||||||
import com.central.search.service.ISearchService;
|
import com.central.search.service.ISearchService;
|
||||||
import com.central.search.util.SearchBuilder;
|
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用搜索
|
* 通用搜索
|
||||||
*
|
*
|
||||||
@@ -18,8 +19,11 @@ import org.springframework.stereotype.Service;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class SearchServiceImpl implements ISearchService {
|
public class SearchServiceImpl implements ISearchService {
|
||||||
@Autowired
|
private final ElasticsearchRestTemplate elasticsearchRestTemplate;
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
|
||||||
|
public SearchServiceImpl(ElasticsearchRestTemplate elasticsearchRestTemplate) {
|
||||||
|
this.elasticsearchRestTemplate = elasticsearchRestTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* StringQuery通用搜索
|
* StringQuery通用搜索
|
||||||
@@ -28,8 +32,8 @@ public class SearchServiceImpl implements ISearchService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public PageResult<JSONObject> strQuery(String indexName, SearchDto searchDto) {
|
public PageResult<JSONObject> strQuery(String indexName, SearchDto searchDto) throws IOException {
|
||||||
return SearchBuilder.builder(elasticsearchTemplate, indexName)
|
return SearchBuilder.builder(elasticsearchRestTemplate, indexName)
|
||||||
.setStringQuery(searchDto.getQueryStr())
|
.setStringQuery(searchDto.getQueryStr())
|
||||||
.addSort(searchDto.getSortCol(), SortOrder.DESC)
|
.addSort(searchDto.getSortCol(), SortOrder.DESC)
|
||||||
.setIsHighlight(searchDto.getIsHighlighter())
|
.setIsHighlight(searchDto.getIsHighlighter())
|
||||||
|
|||||||
-226
@@ -1,226 +0,0 @@
|
|||||||
package com.central.search.util;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
|
||||||
import cn.hutool.core.util.BooleanUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.central.common.model.PageResult;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.apache.commons.beanutils.PropertyUtils;
|
|
||||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
|
||||||
import org.elasticsearch.common.text.Text;
|
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
|
||||||
import org.elasticsearch.search.SearchHits;
|
|
||||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
|
||||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
|
||||||
import org.springframework.data.elasticsearch.ElasticsearchException;
|
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ES查询Builder
|
|
||||||
*
|
|
||||||
* @author zlt
|
|
||||||
* @date 2019/4/23
|
|
||||||
*/
|
|
||||||
@Setter
|
|
||||||
@Getter
|
|
||||||
public class SearchBuilder {
|
|
||||||
/**
|
|
||||||
* 高亮前缀
|
|
||||||
*/
|
|
||||||
private static final String HIGHLIGHTER_PRE_TAGS = "<mark>";
|
|
||||||
/**
|
|
||||||
* 高亮后缀
|
|
||||||
*/
|
|
||||||
private static final String HIGHLIGHTER_POST_TAGS = "</mark>";
|
|
||||||
|
|
||||||
private SearchRequestBuilder searchBuilder;
|
|
||||||
|
|
||||||
private SearchBuilder(SearchRequestBuilder searchBuilder) {
|
|
||||||
this.searchBuilder = searchBuilder;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成SearchBuilder实例
|
|
||||||
* @param elasticsearchTemplate
|
|
||||||
* @param indexName
|
|
||||||
*/
|
|
||||||
public static SearchBuilder builder(ElasticsearchTemplate elasticsearchTemplate, String indexName) {
|
|
||||||
SearchRequestBuilder searchBuilder = elasticsearchTemplate.getClient().prepareSearch(indexName);
|
|
||||||
return new SearchBuilder(searchBuilder);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成queryStringQuery查询
|
|
||||||
* @param queryStr 查询关键字
|
|
||||||
*/
|
|
||||||
public SearchBuilder setStringQuery(String queryStr) {
|
|
||||||
QueryBuilder queryBuilder;
|
|
||||||
if (StrUtil.isNotEmpty(queryStr)) {
|
|
||||||
queryBuilder = QueryBuilders.queryStringQuery(queryStr);
|
|
||||||
} else {
|
|
||||||
queryBuilder = QueryBuilders.matchAllQuery();
|
|
||||||
}
|
|
||||||
searchBuilder.setQuery(queryBuilder);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置分页
|
|
||||||
* @param page 当前页数
|
|
||||||
* @param limit 每页显示数
|
|
||||||
*/
|
|
||||||
public SearchBuilder setPage(Integer page, Integer limit) {
|
|
||||||
if (page != null && limit != null) {
|
|
||||||
searchBuilder.setFrom((page - 1) * limit)
|
|
||||||
.setSize(limit);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 增加排序
|
|
||||||
* @param field 排序字段
|
|
||||||
* @param order 顺序方向
|
|
||||||
*/
|
|
||||||
public SearchBuilder addSort(String field, SortOrder order) {
|
|
||||||
if (StrUtil.isNotEmpty(field) && order != null) {
|
|
||||||
searchBuilder.addSort(field, order);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置高亮
|
|
||||||
* @param preTags 高亮处理前缀
|
|
||||||
* @param postTags 高亮处理后缀
|
|
||||||
*/
|
|
||||||
public SearchBuilder setHighlight(String field, String preTags, String postTags) {
|
|
||||||
if (StrUtil.isNotEmpty(field) && StrUtil.isNotEmpty(preTags) && StrUtil.isNotEmpty(postTags)) {
|
|
||||||
HighlightBuilder highlightBuilder = new HighlightBuilder();
|
|
||||||
highlightBuilder.field(field)
|
|
||||||
.preTags(preTags)
|
|
||||||
.postTags(postTags);
|
|
||||||
searchBuilder.highlighter(highlightBuilder);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置是否需要高亮处理
|
|
||||||
* @param isHighlighter 是否需要高亮处理
|
|
||||||
*/
|
|
||||||
public SearchBuilder setIsHighlight(Boolean isHighlighter) {
|
|
||||||
if (BooleanUtil.isTrue(isHighlighter)) {
|
|
||||||
this.setHighlight("*"
|
|
||||||
, HIGHLIGHTER_PRE_TAGS, HIGHLIGHTER_POST_TAGS);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置查询路由
|
|
||||||
* @param routing 路由数组
|
|
||||||
*/
|
|
||||||
public SearchBuilder setRouting(String... routing) {
|
|
||||||
if (ArrayUtil.isNotEmpty(routing)) {
|
|
||||||
searchBuilder.setRouting(routing);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回结果 SearchResponse
|
|
||||||
*/
|
|
||||||
public SearchResponse get() {
|
|
||||||
return searchBuilder.execute().actionGet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回列表结果 List<JSONObject>
|
|
||||||
*/
|
|
||||||
public List<JSONObject> getList() {
|
|
||||||
return getList(this.get().getHits());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回分页结果 PageResult<JSONObject>
|
|
||||||
*/
|
|
||||||
public PageResult<JSONObject> getPage() {
|
|
||||||
return this.getPage(null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回分页结果 PageResult<JSONObject>
|
|
||||||
* @param page 当前页数
|
|
||||||
* @param limit 每页显示
|
|
||||||
*/
|
|
||||||
public PageResult<JSONObject> getPage(Integer page, Integer limit) {
|
|
||||||
this.setPage(page, limit);
|
|
||||||
SearchResponse response = this.get();
|
|
||||||
SearchHits searchHits = response.getHits();
|
|
||||||
long totalCnt = searchHits.getTotalHits();
|
|
||||||
List<JSONObject> list = getList(searchHits);
|
|
||||||
return PageResult.<JSONObject>builder().data(list).code(0).count(totalCnt).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回JSON列表数据
|
|
||||||
*/
|
|
||||||
private List<JSONObject> getList(SearchHits searchHits) {
|
|
||||||
List<JSONObject> list = new ArrayList<>();
|
|
||||||
if (searchHits != null) {
|
|
||||||
searchHits.forEach(item -> {
|
|
||||||
JSONObject jsonObject = JSON.parseObject(item.getSourceAsString());
|
|
||||||
jsonObject.put("id", item.getId());
|
|
||||||
|
|
||||||
Map<String, HighlightField> highlightFields = item.getHighlightFields();
|
|
||||||
if (highlightFields != null) {
|
|
||||||
populateHighLightedFields(jsonObject, highlightFields);
|
|
||||||
}
|
|
||||||
list.add(jsonObject);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 组装高亮字符
|
|
||||||
* @param result 目标对象
|
|
||||||
* @param highlightFields 高亮配置
|
|
||||||
*/
|
|
||||||
private <T> void populateHighLightedFields(T result, Map<String, HighlightField> highlightFields) {
|
|
||||||
for (HighlightField field : highlightFields.values()) {
|
|
||||||
try {
|
|
||||||
String name = field.getName();
|
|
||||||
if (!name.endsWith(".keyword")) {
|
|
||||||
PropertyUtils.setProperty(result, field.getName(), concat(field.fragments()));
|
|
||||||
}
|
|
||||||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
|
||||||
throw new ElasticsearchException("failed to set highlighted value for field: " + field.getName()
|
|
||||||
+ " with value: " + Arrays.toString(field.getFragments()), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 拼凑数组为字符串
|
|
||||||
*/
|
|
||||||
private String concat(Text[] texts) {
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
for (Text text : texts) {
|
|
||||||
sb.append(text.toString());
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
spring:
|
spring:
|
||||||
data:
|
|
||||||
elasticsearch:
|
|
||||||
cluster-name: ${zlt.elasticsearch.cluster-name}
|
|
||||||
cluster-nodes: ${zlt.elasticsearch.cluster-nodes}:9300
|
|
||||||
elasticsearch:
|
elasticsearch:
|
||||||
rest:
|
rest:
|
||||||
uris: ${zlt.elasticsearch.cluster-nodes}:9200
|
uris: ${zlt.elasticsearch.uris}
|
||||||
|
username: ${zlt.elasticsearch.username}
|
||||||
|
password: ${zlt.elasticsearch.password}
|
||||||
|
|
||||||
zlt:
|
zlt:
|
||||||
indices:
|
indices:
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ spring.redis.port=6379
|
|||||||
spring.redis.timeout=5000
|
spring.redis.timeout=5000
|
||||||
|
|
||||||
##### elasticsearch配置
|
##### elasticsearch配置
|
||||||
zlt.elasticsearch.cluster-name=my-es
|
zlt.elasticsearch.uris=192.168.28.130:9200
|
||||||
zlt.elasticsearch.cluster-nodes=192.168.28.130
|
zlt.elasticsearch.username=elastic
|
||||||
|
zlt.elasticsearch.password=qEnNfKNujqNrOPD9q5kb
|
||||||
|
|
||||||
##### sentinel配置
|
##### sentinel配置
|
||||||
zlt.sentinel.dashboard=192.168.28.130:6999
|
zlt.sentinel.dashboard=192.168.28.130:6999
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ spring.redis.sentinel.nodes=192.168.28.130:26380,192.168.28.131:26381
|
|||||||
spring.redis.password=1q2w3e4r
|
spring.redis.password=1q2w3e4r
|
||||||
|
|
||||||
##### elasticsearch配置
|
##### elasticsearch配置
|
||||||
zlt.elasticsearch.cluster-name=my-es
|
zlt.elasticsearch.uris=192.168.28.130:9200
|
||||||
zlt.elasticsearch.cluster-nodes=192.168.28.130
|
zlt.elasticsearch.username=
|
||||||
|
zlt.elasticsearch.password=
|
||||||
|
|
||||||
##### sentinel配置
|
##### sentinel配置
|
||||||
zlt.sentinel.dashboard=192.168.28.130:6999
|
zlt.sentinel.dashboard=192.168.28.130:6999
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ spring.redis.port=6379
|
|||||||
spring.redis.timeout=5000
|
spring.redis.timeout=5000
|
||||||
|
|
||||||
##### elasticsearch配置
|
##### elasticsearch配置
|
||||||
zlt.elasticsearch.cluster-name=my-es
|
zlt.elasticsearch.uris=192.168.28.130:9200
|
||||||
zlt.elasticsearch.cluster-nodes=192.168.28.130
|
zlt.elasticsearch.username=
|
||||||
|
zlt.elasticsearch.password=
|
||||||
|
|
||||||
##### sentinel配置
|
##### sentinel配置
|
||||||
zlt.sentinel.dashboard=192.168.28.130:6999
|
zlt.sentinel.dashboard=192.168.28.130:6999
|
||||||
|
|||||||
+2
@@ -50,6 +50,8 @@ public class RequestStatisticsFilter implements GlobalFilter, Ordered {
|
|||||||
return "CHROME";
|
return "CHROME";
|
||||||
} else if (browser.contains("FIREFOX")) {
|
} else if (browser.contains("FIREFOX")) {
|
||||||
return "FIREFOX";
|
return "FIREFOX";
|
||||||
|
} else if (browser.contains("SAFARI")) {
|
||||||
|
return "SAFARI";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return browser;
|
return browser;
|
||||||
|
|||||||
+2
@@ -57,6 +57,8 @@ public class RequestStatisticsFilter extends ZuulFilter {
|
|||||||
return "CHROME";
|
return "CHROME";
|
||||||
} else if (browser.contains("FIREFOX")) {
|
} else if (browser.contains("FIREFOX")) {
|
||||||
return "FIREFOX";
|
return "FIREFOX";
|
||||||
|
} else if (browser.contains("SAFARI")) {
|
||||||
|
return "SAFARI";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return browser;
|
return browser;
|
||||||
|
|||||||
@@ -37,19 +37,12 @@
|
|||||||
page: false,
|
page: false,
|
||||||
cols: [[
|
cols: [[
|
||||||
{type: 'numbers'},
|
{type: 'numbers'},
|
||||||
{field: 'indexName', sort: true, title: '索引名'},
|
{field: 'health', sort: true, title: '索引健康'},
|
||||||
|
{field: 'status', sort: true, title: '索引状态'},
|
||||||
|
{field: 'index', sort: true, title: '索引名'},
|
||||||
{field: 'docsCount', sort: true, title: '文档数'},
|
{field: 'docsCount', sort: true, title: '文档数'},
|
||||||
{field: 'docsDeleted', sort: true, title: '文档删除数'},
|
{field: 'docsDeleted', sort: true, title: '文档删除数'},
|
||||||
{
|
{field: 'storeSize', sort: true, title: '索引大小'},
|
||||||
sort: true, templet: function (d) {
|
|
||||||
if (d.storeSizeInBytes) {
|
|
||||||
return parseFloat(d.storeSizeInBytes).toFixed(2);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}, title: '索引大小(kb)'
|
|
||||||
},
|
|
||||||
{field: 'queryCount', sort: true, title: '总查询数'},
|
|
||||||
{field: 'queryTimeInMillis', sort: true, title: '总查询耗时(s)'},
|
|
||||||
{fixed: 'right', align: 'center', toolbar: '#index-table-bar', title: '操作', width: 120}
|
{fixed: 'right', align: 'center', toolbar: '#index-table-bar', title: '操作', width: 120}
|
||||||
]]
|
]]
|
||||||
});
|
});
|
||||||
@@ -87,7 +80,7 @@
|
|||||||
|
|
||||||
//显示索引查看弹窗
|
//显示索引查看弹窗
|
||||||
let showViewModel = function (data) {
|
let showViewModel = function (data) {
|
||||||
admin.putTempData('indexName', data.indexName);
|
admin.putTempData('indexName', data.index);
|
||||||
admin.popupCenter({
|
admin.popupCenter({
|
||||||
title: '查看索引',
|
title: '查看索引',
|
||||||
path: 'pages/search/index_manager_view.html'
|
path: 'pages/search/index_manager_view.html'
|
||||||
@@ -99,7 +92,7 @@
|
|||||||
layer.confirm('确定要删除吗?', function (i) {
|
layer.confirm('确定要删除吗?', function (i) {
|
||||||
layer.close(i);
|
layer.close(i);
|
||||||
layer.load(2);
|
layer.load(2);
|
||||||
admin.req('api-search/admin/index?indexName='+obj.data.indexName, {}, function (data) {
|
admin.req('api-search/admin/index?indexName='+obj.data.index, {}, function (data) {
|
||||||
layer.closeAll('loading');
|
layer.closeAll('loading');
|
||||||
if (data.resp_code == 0) {
|
if (data.resp_code == 0) {
|
||||||
layer.msg(data.resp_msg, {icon: 1, time: 500});
|
layer.msg(data.resp_msg, {icon: 1, time: 500});
|
||||||
|
|||||||
Reference in New Issue
Block a user