From 9d7149aa04ef1b3423438046b30dfe1e0d7276b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9A=AE=E5=85=89=EF=BC=9A=E5=9F=8E=E4=B8=AD=E5=9F=8E?= <806783409@qq.com> Date: Fri, 26 Jul 2019 23:48:48 +0800 Subject: [PATCH] =?UTF-8?q?es=E6=96=87=E6=A1=A3=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/EsMappingController.java | 55 ++++++- .../es-ui/src/assets/img/collapsed.png | Bin 0 -> 340 bytes .../es-ui/src/assets/img/expanded.png | Bin 0 -> 331 bytes .../es-ui/src/common/config/apilist.js | 1 + .../es-ui/src/common/config/apimix.js | 8 +- .../es-ui/src/common/lib/common/formatjson.js | 122 +++++++++++++++ zyplayer-doc-ui/es-ui/src/routes.js | 2 + .../es-ui/src/views/index/Executer.vue | 145 ++++++++++++++++++ 8 files changed, 322 insertions(+), 11 deletions(-) create mode 100644 zyplayer-doc-ui/es-ui/src/assets/img/collapsed.png create mode 100644 zyplayer-doc-ui/es-ui/src/assets/img/expanded.png create mode 100644 zyplayer-doc-ui/es-ui/src/common/lib/common/formatjson.js create mode 100644 zyplayer-doc-ui/es-ui/src/views/index/Executer.vue diff --git a/zyplayer-doc-es/src/main/java/com/zyplayer/doc/elasticsearch/controller/EsMappingController.java b/zyplayer-doc-es/src/main/java/com/zyplayer/doc/elasticsearch/controller/EsMappingController.java index 8a7b7554..f7590d95 100644 --- a/zyplayer-doc-es/src/main/java/com/zyplayer/doc/elasticsearch/controller/EsMappingController.java +++ b/zyplayer-doc-es/src/main/java/com/zyplayer/doc/elasticsearch/controller/EsMappingController.java @@ -4,21 +4,28 @@ import com.zyplayer.doc.core.json.DocResponseJson; import com.zyplayer.doc.core.json.ResponseJson; import com.zyplayer.doc.data.service.elasticsearch.support.ElasticSearchUtil; import org.apache.commons.lang3.StringUtils; +import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.GetIndexRequest; +import org.elasticsearch.client.indices.GetIndexResponse; import org.elasticsearch.client.indices.GetMappingsRequest; import org.elasticsearch.client.indices.GetMappingsResponse; import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.script.ScriptType; +import org.elasticsearch.script.mustache.SearchTemplateRequest; +import org.elasticsearch.script.mustache.SearchTemplateResponse; +import org.elasticsearch.search.SearchHit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; /** @@ -57,9 +64,43 @@ public class EsMappingController { return DocResponseJson.warn("获取文档失败"); } - @GetMapping("/list") - public ResponseJson list(String keywords) throws IOException { - return DocResponseJson.ok(); + @PostMapping("/execute") + public ResponseJson execute(String index, String sql) throws IOException { + SearchTemplateRequest request = new SearchTemplateRequest(); + request.setRequest(new SearchRequest().indices(index)); + request.setScriptType(ScriptType.INLINE); + request.setScript(sql); + request.setScriptParams(new HashMap<>()); + try { + RestHighLevelClient client = elasticSearchUtil.getEsClient("127.0.0.1:9200", "http"); + SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT); + List> resultList = new LinkedList<>(); + for (SearchHit searchHit : response.getResponse().getHits()) { + resultList.add(searchHit.getSourceAsMap()); + } + return DocResponseJson.ok(resultList); + } catch (Exception e) { + return DocResponseJson.warn(e.getMessage()); + } + } + + @GetMapping("/index") + public ResponseJson index(String index) throws IOException { + GetIndexRequest request = new GetIndexRequest(index); + request.setMasterTimeout(TimeValue.timeValueMinutes(1)); + try { + RestHighLevelClient client = elasticSearchUtil.getEsClient("127.0.0.1:9200", "http"); + GetIndexResponse indexResponse = client.indices().get(request, RequestOptions.DEFAULT); + Map resultMap = new HashMap<>(); + resultMap.put("mapping", indexResponse.getMappings().get(index)); + resultMap.put("setting", indexResponse.getSettings().get(index).keySet()); + +// return DocResponseJson.ok(indexResponse.getSetting(index, "index.number_of_shards")); + return DocResponseJson.ok(resultMap); + } catch (Exception e) { + e.printStackTrace(); + } + return DocResponseJson.warn("获取文档失败"); } } diff --git a/zyplayer-doc-ui/es-ui/src/assets/img/collapsed.png b/zyplayer-doc-ui/es-ui/src/assets/img/collapsed.png new file mode 100644 index 0000000000000000000000000000000000000000..f77921f35565efd45235d04e7f8c8eca12390246 GIT binary patch literal 340 zcmeAS@N?(olHy`uVBq!ia0vp^GC<7D!3-p$8Dv|56k~CayA#8@b22Z19F}xPUq=Rp zjs4tz5?O)#gaDrq*Z=?j|NHm*&!0cvzJ2}n^~1-HZ{L4-{qe)gw{M=keEImrvl|Z{ z+`D`0;?1k4uU$BH>CFB!CwFZ)bY=hE4Tp9tubsWa*w`3o63`GJi4|OoJa!!@AyE?K z7YtN}4KSP))_Vn1!&%@FSdnGBsfJ8O*@PF{GjPjZi-pyb6D6FX*zEq6bB^Zb!YGM&s@K5PI%|ld)dCe(?;&Q#L2zBQ-HQHFnGH9xvXEalo5nOxTP^dwH$K~P*gCr5Q zi!*-T4>1l1ShJ_7e(MtEC9bP3P2*b`pql%#rf^=*;X*_0$1mhIKYmfmA?+^nvqST# k@bQ|vY{wp+k6)!PI8Wm6yFHVD7BMh*y85}Sb4q9e0BHl4egFUf literal 0 HcmV?d00001 diff --git a/zyplayer-doc-ui/es-ui/src/common/config/apilist.js b/zyplayer-doc-ui/es-ui/src/common/config/apilist.js index 90e8b43e..61884a88 100644 --- a/zyplayer-doc-ui/es-ui/src/common/config/apilist.js +++ b/zyplayer-doc-ui/es-ui/src/common/config/apilist.js @@ -15,6 +15,7 @@ var URL = { manageUpdateDatasource: '/zyplayer-doc-db/datasource/update', esMappings: '/zyplayer-doc-es/es-mapping/mappings', + esExecuter: '/zyplayer-doc-es/es-mapping/execute', systemUpgradeInfo: '/system/info/upgrade', }; diff --git a/zyplayer-doc-ui/es-ui/src/common/config/apimix.js b/zyplayer-doc-ui/es-ui/src/common/config/apimix.js index 6f7c366e..9da7d2e9 100644 --- a/zyplayer-doc-ui/es-ui/src/common/config/apimix.js +++ b/zyplayer-doc-ui/es-ui/src/common/config/apimix.js @@ -5,14 +5,14 @@ var href = window.location.href; var _fn = { href: href, // 本地启动时使用本地接口调试 - // HOST: 'http://local.zyplayer.com:8083/zyplayer-doc-manage', - // HOST1: 'http://local.zyplayer.com:8083/zyplayer-doc-manage', + HOST: 'http://local.zyplayer.com:8083/zyplayer-doc-manage', + HOST1: 'http://local.zyplayer.com:8083/zyplayer-doc-manage', // 也可以直接使用线上的服务调试 // HOST: 'http://doc.zyplayer.com/zyplayer-doc-manage', // HOST1: 'http://doc.zyplayer.com/zyplayer-doc-manage', // 打包时使用下面这两行,文件就放在根目录下,所以当前路劲就好 - HOST: './', - HOST1: './', + // HOST: './', + // HOST1: './', mixUrl: function (host, url) { var p; diff --git a/zyplayer-doc-ui/es-ui/src/common/lib/common/formatjson.js b/zyplayer-doc-ui/es-ui/src/common/lib/common/formatjson.js new file mode 100644 index 00000000..21b9f76a --- /dev/null +++ b/zyplayer-doc-ui/es-ui/src/common/lib/common/formatjson.js @@ -0,0 +1,122 @@ +/** + * 将对象处理成json格式化和着色的html + * @author 暮光:城中城 + * @since 2017年5月7日 + */ +export default { + // 需要在对象或列表后面添加注释的对象,例:{userList: "用户列表"} + // 那么在名字为userList的对象或列表后面都会加上:“用户列表” 这个注释 + annotationObject: {}, + tabStr: " ", + isArray: function (obj) { + return obj && typeof obj === 'object' && typeof obj.length === 'number' + && !(obj.propertyIsEnumerable('length')); + }, + processObjectToHtmlPre: function (obj, indent, addComma, isArray, isPropertyContent, showAnnotation) { + var htmlStr = this.processObject(obj, "", indent, addComma, isArray, isPropertyContent, showAnnotation); + htmlStr = '
' + htmlStr + '
'; + return htmlStr; + }, + processObject: function (obj, keyName, indent, addComma, isArray, isPropertyContent, showAnnotation) { + var html = ""; + var comma = (addComma) ? ", " : ""; + var type = typeof obj; + if (this.isArray(obj)) { + if (obj.length == 0) { + html += this.getRow(indent, "[ ]" + comma, isPropertyContent); + } else {// + var clpsHtml = '
'; + var annotation = ''; + if (showAnnotation && isNotEmpty(keyName) && isNotEmpty(this.annotationObject[keyName])) { + annotation = '// ' + this.annotationObject[keyName] + ''; + } + html += this.getRow(indent, "[" + clpsHtml + annotation, isPropertyContent); + for (var i = 0; i < obj.length; i++) { + html += this.processObject(obj[i], "", indent + 1, i < (obj.length - 1), true, false, showAnnotation); + } + clpsHtml = ""; + html += this.getRow(indent, clpsHtml + "]" + comma); + } + } else if (type == 'object' && obj == null) { + html += this.formatLiteral("null", "", comma, indent, isArray, "null"); + } else if (type == 'object') { + var numProps = 0; + for (var prop in obj) { + numProps++; + } + if (numProps == 0) { + html += this.getRow(indent, "{ }" + comma, isPropertyContent); + } else { // + var clpsHtml = '
'; + var annotation = ''; + if (showAnnotation && isNotEmpty(keyName) && isNotEmpty(this.annotationObject[keyName])) { + annotation = '// ' + this.annotationObject[keyName] + ''; + } + html += this.getRow(indent, "{" + clpsHtml + annotation, isPropertyContent); + var j = 0; + for (var prop in obj) { + var processStr = '"' + prop + '": ' + this.processObject(obj[prop], prop, indent + 1, ++j < numProps, false, true, showAnnotation); + html += this.getRow(indent + 1, processStr); + } + clpsHtml = ""; + html += this.getRow(indent, clpsHtml + "}" + comma); + } + } else if (type == 'number') { + html += this.formatLiteral(obj, "", comma, indent, isArray, "number"); + } else if (type == 'boolean') { + html += this.formatLiteral(obj, "", comma, indent, isArray, "boolean"); + } else if (type == 'function') { + obj = this.formatFunction(indent, obj); + html += this.formatLiteral(obj, "", comma, indent, isArray, "function"); + } else if (type == 'undefined') { + html += this.formatLiteral("undefined", "", comma, indent, isArray, "null"); + } else { + html += this.formatLiteral(obj, "\"", comma, indent, isArray, "string"); + } + return html; + }, + expImgClicked: function (img) { + var container = img.parentNode.nextSibling; + if (!container) return; + var disp = "none"; + var cls = "option-img img-collapsed"; + if (container.style.display == "none") { + disp = "inline"; + cls = "option-img img-expanded"; + } + img.className = cls; + container.style.display = disp; + }, + formatLiteral: function (literal, quote, comma, indent, isArray, style) { + if (typeof literal == 'string') { + literal = literal.split("<").join("<").split(">").join(">"); + } + var str = "" + quote + literal + quote + comma + ""; + if (isArray) { + str = this.getRow(indent, str); + } + return str; + }, + formatFunction: function (indent, obj) { + var tabs = ""; + for (var i = 0; i < indent; i++) { + tabs += this.tabStr; + } + var funcStrArray = obj.toString().split("\n"); + var str = ""; + for (var i = 0; i < funcStrArray.length; i++) { + str += ((i == 0) ? "" : tabs) + funcStrArray[i] + "\n"; + } + return str; + }, + getRow: function (indent, data, isPropertyContent) { + var tabs = ""; + for (var i = 0; i < indent && !isPropertyContent; i++) { + tabs += this.tabStr; + } + if (data != null && data.length > 0 && data.charAt(data.length - 1) != "\n") { + data = data + "\n"; + } + return tabs + data; + } +} diff --git a/zyplayer-doc-ui/es-ui/src/routes.js b/zyplayer-doc-ui/es-ui/src/routes.js index c95fb250..564f8e65 100644 --- a/zyplayer-doc-ui/es-ui/src/routes.js +++ b/zyplayer-doc-ui/es-ui/src/routes.js @@ -8,6 +8,7 @@ import DataDatasourceManage from './views/data/DatasourceManage.vue' import DataRouterView from './views/data/RouterView.vue' import IndexShow from './views/index/Show.vue' +import IndexExecuter from './views/index/Executer.vue' import IndexRouterView from './views/index/RouterView.vue' import CommonNoAuth from './views/common/NoAuth.vue' @@ -34,6 +35,7 @@ let routes = [ component: IndexRouterView, children: [ {path: 'show', name: '索引信息',component: IndexShow}, + {path: 'executer', name: '执行器',component: IndexExecuter}, ] }, { path: '/data', diff --git a/zyplayer-doc-ui/es-ui/src/views/index/Executer.vue b/zyplayer-doc-ui/es-ui/src/views/index/Executer.vue new file mode 100644 index 00000000..3e9bb2df --- /dev/null +++ b/zyplayer-doc-ui/es-ui/src/views/index/Executer.vue @@ -0,0 +1,145 @@ + + + + +