From f7e225f6b10c3fad9f2e995431bc0aae028f59a5 Mon Sep 17 00:00:00 2001 From: Jun Ohtani Date: Wed, 17 Jan 2018 19:31:43 +0900 Subject: [PATCH] Upgrade 6.1.3 support in memory history --- README.md | 2 +- index.js | 7 - package.json | 8 +- public/analyzeui_controller.js | 314 ++++++++++++++++++++++++++++++++ public/app.js | 317 +-------------------------------- public/pagestate.js | 22 +++ public/storage.js | 60 +++++++ public/utils.js | 53 ++++++ server/routes/analyze.js | 48 ++++- 9 files changed, 496 insertions(+), 335 deletions(-) create mode 100644 public/analyzeui_controller.js create mode 100644 public/pagestate.js create mode 100644 public/storage.js create mode 100644 public/utils.js diff --git a/README.md b/README.md index ebc03f5..8434efa 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ > UI for elasticsearch analyze API -This is for kibana 6.0.0_rc1. +This is for kibana 6.1.2. Analyze text with Aalyzer ![Sample image](./sample_image.png) diff --git a/index.js b/index.js index 845450b..9c47d81 100644 --- a/index.js +++ b/index.js @@ -6,22 +6,17 @@ export default function (kibana) { require: ['elasticsearch'], name: 'analyze-api-ui-plugin', uiExports: { - app: { title: 'Analyze UI', description: 'UI for elasticsearch analyze API', main: 'plugins/analyze-api-ui-plugin/app' }, - translations: [ resolve(__dirname, './translations/es.json') ], - - hacks: [ ] - }, config(Joi) { @@ -29,13 +24,11 @@ export default function (kibana) { enabled: Joi.boolean().default(true), }).default(); }, - init(server, options) { // Add server routes and initalize the plugin here analyzeRoute(server); } - }); }; diff --git a/package.json b/package.json index ff0ba78..254d5b3 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "analyze-api-ui-plugin", - "version": "6.0.0", + "version": "6.1.2", "description": "UI for elasticsearch analyze API", "main": "index.js", "kibana": { - "version": "6.0.0-rc1", + "version": "6.1.2", "templateVersion": "7.2.0" }, "scripts": { @@ -17,7 +17,9 @@ "gather-info": "node gather-info.js" }, "dependencies": { - "boom": "2.8.0" + "boom": "2.8.0", + "joi": "6.10.1", + "lodash": "3.10.1" }, "devDependencies": { "@elastic/eslint-config-kibana": "^0.6.1", diff --git a/public/analyzeui_controller.js b/public/analyzeui_controller.js new file mode 100644 index 0000000..575f173 --- /dev/null +++ b/public/analyzeui_controller.js @@ -0,0 +1,314 @@ +import utils from "./utils"; + +export function analyzeApiUiPluginController($http, $scope, $timeout, chrome, Notifier) { + const notify = new Notifier(); + $scope.services = ['analyzer', 'custom_analyzer', 'field', 'compare analyzers']; + $scope.currentTab = $scope.services[0]; + $scope.title = 'Analyze Api Ui Plugin'; + $scope.description = 'UI for elasticsearch analyze API'; + $scope.showAllAttr = false; + + // load formValues & currentTab from sessionStorage + utils.loadSavedState($scope); + + let timer; + const saveDelay = 500; + + function saveState() { + if (timer) { + timer = $timeout.cancel(timer); + } + timer = $timeout(utils.saveState, saveDelay, true, $scope); + } + + $scope.$watch('formValues', saveState, true); + $scope.$watch('currentTab', saveState); + + this.initializeError = () => { + $scope.detail = {}; + $scope.show_result = false; + $scope.esrequest = {}; + $scope.show_esrequest = false; + $scope.textError = null; + $scope.indexNameError = null; + $scope.analyzerError = null; + $scope.resultAnalyzers = []; + } + + this.alwaysShowTokenProperties = [ + "token", + "position" + ]; + this.hiddenTokenProperties = [ + "bytes","pronunciation (en)", "reading (en)", "partOfSpeech (en)", "inflectionType (en)", "inflectionForm (en)" + ]; + + // switch tab + this.changeTab = (tab) => { + $scope.currentTab = tab; + this.initializeError(); + }; + + // add input of charfilter function + this.addCharfilter = ($index) => { + //TODO check not having '' in current charfilters + $scope.formValues.charfilters.push({'item': '', 'id': $scope.formValues.charfilters.length}); + }; + // remove input of charfilter function + this.removeCharfilter = ($index) => { + $scope.formValues.charfilters.splice($index, 1); + }; + + // add input of filter function + this.addFilter = ($index) => { + //TODO check not having '' in current filters + $scope.formValues.filters.push({'item': '', 'id': $scope.formValues.filters.length}); + }; + // remove input of charfilter function + this.removeFilter = ($index) => { + $scope.formValues.filters.splice($index, 1); + }; + + // add input of analyzer function + this.addAnalyzer = ($index) => { + //TODO check not having '' in current analyzers + $scope.formValues.analyzersForCompare.push({'item': '', 'id': $scope.formValues.charfilters.length}); + }; + // remove input of analyzer function + this.removeAnalyzer = ($index) => { + $scope.formValues.analyzersForCompare.splice($index, 1); + }; + + // + this.parseCustom = (target) => { + if (typeof target === 'string' && target.startsWith('{')) { + try { + var tmpJson = JSON.parse(target); + if (tmpJson !== null && typeof tmpJson === 'object') { + return tmpJson; + } else { + $scope.analyzerError = 'charfilter has wrong custom charfilter'; + return -1; + } + } catch (e) { + $scope.analyzerError = e.message; + return -1; + } + } else { + return target; + } + }; + + // show short name + this.shortenName = (name) => { + if (name.indexOf('.') > 0) { + return name.substr(name.lastIndexOf('.')+1); + } + return name; + } + + // count tokenstream length + this.countTokenSteamLength = (detail) => { + // FIXME tokens length is not fit if it has synonym token/compound token... + var tokenStreamLength = 0; + + if (detail.tokenizer) { + tokenStreamLength = this.getLength(tokenStreamLength, detail.tokenizer.tokens); + } else if (detail.analyzer) { + tokenStreamLength = this.getLength(tokenStreamLength, detail.analyzer.tokens); + } + if (detail.tokenfilters) { + detail.tokenfilters.forEach( (filter) => { + tokenStreamLength = this.getLength(tokenStreamLength, filter.tokens); + }); + } + $scope.tokenIndicesArray = []; + for (var i = 0; i < tokenStreamLength; i++) { + $scope.tokenIndicesArray.push(i); + } + }; + + // compare and swap tokenStreamLength + this.getLength = (current, tokenArray) => { + // FIXME check if there is synonyms or compound + var length = current; + if (tokenArray != null) { + // FIXME must consider the situation if positionIncrements != 1 + if (tokenArray[tokenArray.length -1].position >= current) { + length = tokenArray[tokenArray.length -1].position + 1; + } + } + return length; + }; + + // for display + this.getTokenFromTokenstream = (index, target1, target2) => { + var target = target1; + if (target == null && target2 != null) { + target = target2; + } + $scope.currentLevelTokenList = []; + for (var token of target.tokens) { + if (token.position > index) { + break; + } + if (token.position == index) { + $scope.currentLevelTokenList.push(token); + } + } + return $scope.currentLevelTokenList.length > 0; + } + + // filter token properties + this.filteredCurrentTokenInfo = (token) => { + if (token != null) { + var result = {}; + Object.keys(token).forEach((key) => { + if (!this.hiddenTokenProperties.includes(key)) { + result[key] = token[key]; + } + }); + return result; + } else { + return null; + } + }; + + // switch show/hide properties + this.hideTokenProperty = (propertyName) => { + if (this.alwaysShowTokenProperties.includes(propertyName)) { + return true; + } else { + // TODO should we handle each attribute to show/hide? + return $scope.showAllAttr; + } + }; + + // Call analyze function + this.performAnalyze = () => { + // initialize + this.initializeError(); + + // FIXME validation logic, text is required + let param = { + text: $scope.formValues.text + }; + if ($scope.formValues.text.trim().length == 0) { + $scope.textError = 'text should be not null!'; + return; + } + if ($scope.formValues.indexName.length > 0) + param.indexName = $scope.formValues.indexName.trim(); + + if ($scope.currentTab == 'analyzer') { + if ($scope.formValues.analyzer.length > 0) + param.analyzer = $scope.formValues.analyzer.trim(); + } else if ($scope.currentTab == 'field') { + if ($scope.formValues.field.trim().length == 0) + $scope.analyzerError = 'field is required. '; + if ($scope.formValues.indexName.trim().length == 0) + $scope.analyzerError += 'index name is required for "field". '; + if ($scope.analyzerError) { + return; + } + param.field = $scope.formValues.field.trim(); + } else if ($scope.currentTab == 'custom_analyzer'){ + if ($scope.formValues.tokenizer) { + var tmpObj = this.parseCustom($scope.formValues.tokenizer.trim()); + if (tmpObj != -1) { + param.tokenizer = tmpObj; + tmpObj = null; + } else { + return; + } + } + // FIXME get tokenizer/char_filter/filter + if ($scope.formValues.charfilters.length > 0) { + $scope.formValues.charfilters.forEach( (charfilter) => { + if (charfilter && charfilter.item && charfilter.item.trim().length > 0 ) { + if(param.charfilters == null) param.charfilters = []; + var tmpCharfilter = this.parseCustom(charfilter.item.trim()); + if (tmpCharfilter != -1) { + param.charfilters.push(tmpCharfilter); + } else { + return; + } + } + }); + } + if ($scope.formValues.filters.length > 0) { + $scope.formValues.filters.forEach( (filter) => { + if (filter && filter.item && filter.item.trim().length > 0 ) { + if(param.filters == null) param.filters = []; + var tmpFilter = this.parseCustom(filter.item.trim()); + if (tmpFilter != -1) { + param.filters.push(tmpFilter); + } else { + return; + } + } + }); + } + } else if ($scope.currentTab == 'compare analyzers') { + $scope.formValues.analyzersForCompare.forEach( (analyzer) => { + if (!(analyzer && analyzer.item && analyzer.item.trim().length > 0)) { + console.log("some analyzer is null"); + return; + } + }); + } + + if ($scope.currentTab !== 'compare analyzers'){ + // call kibana server API + $http.post(chrome.addBasePath('/api/analyze-api-ui-plugin/analyze'), param) + .then( + (response) => { + $scope.detail = response.data.detail; + this.countTokenSteamLength(response.data.detail); + $scope.esrequest = response.data.request; + $scope.show_esrequest = true; + $scope.show_result = true; + $scope.showAllAttr = false; + }) + .catch( error => { + if (error.data.statusCode == 404) { + $scope.indexNameError = error.data.message; + } else if (error.data.statusCode == 400) { + $scope.analyzerError = error.data.message; + } else { + notify.error(error.data); + } + }); + } else { + console.log("comparing!!!"); + // multiple analyzers + param.analyzers = $scope.formValues.analyzersForCompare; + + $http.post(chrome.addBasePath('/api/analyze-api-ui-plugin/multi_analyze'), param) + .then( + (response) => { + $scope.resultAnalyzers = response.data.resultAnalyzers; + $scope.show_result = true; + var tokenStreamLength = 0; + $scope.resultAnalyzers.forEach( (result) => { + + tokenStreamLength = this.getLength(tokenStreamLength, result.tokens); + $scope.tokenIndicesArray = []; + for (var i = 0; i < tokenStreamLength; i++) { + $scope.tokenIndicesArray.push(i); + } + }); + }) + .catch( error => { + if (error.data.statusCode == 404) { + $scope.indexNameError = error.data.message; + } else if (error.data.statusCode == 400) { + $scope.analyzerError = error.data.message; + } else { + notify.error(error.data); + } + }); + } + }; + +} \ No newline at end of file diff --git a/public/app.js b/public/app.js index 7ed67f1..a712195 100644 --- a/public/app.js +++ b/public/app.js @@ -4,7 +4,7 @@ import uiRoutes from 'ui/routes'; import 'ui/autoload/styles'; import './less/main.less'; import template from './templates/index.html'; -//import { IndicesGetIndicesProvider } from 'ui/indices/get_indices'; +import { analyzeApiUiPluginController } from './analyzeui_controller'; uiRoutes.enable(); uiRoutes @@ -15,317 +15,4 @@ uiRoutes uiModules .get('app/analyze-api-ui-plugin', []) -.controller('analyzeApiUiPluginController', function ($http, $scope, $route, $interval, chrome, Notifier) { - const notify = new Notifier(); - $scope.services = ['analyzer', 'custom_analyzer', 'field', 'compare analyzers']; - $scope.currentTab = $scope.services[0]; - $scope.title = 'Analyze Api Ui Plugin'; - $scope.description = 'UI for elasticsearch analyze API'; - $scope.showAllAttr = false; - - $scope.formValues = { - indexName: '', - text: '', - analyzer: '', - tokenizer: '', - charfilters: [{'item': '', 'id': 0}], - filters: [{'item': '', 'id': 0}], - field: '', - analyzersForCompare: [{'item': '', 'id': 0},{'item': '', 'id': 1}] - }; - this.initializeError = () => { - $scope.detail = {}; - $scope.show_result = false; - $scope.esrequest = {}; - $scope.show_esrequest = false; - $scope.textError = null; - $scope.indexNameError = null; - $scope.analyzerError = null; - $scope.resultAnalyzers = []; - } - - this.alwaysShowTokenProperties = [ - "token", - "position" - ]; - this.hiddenTokenProperties = [ - "bytes","pronunciation (en)", "reading (en)", "partOfSpeech (en)", "inflectionType (en)", "inflectionForm (en)" - ]; - - // UI state. - // FIXME change index name input to "select" - // this.indexNameOptions = []; - // this.indexNameOptionsError = null; - - // FIXME call _cat/indices and return indices list + no index name - // just now, using text box instead of select - - // switch tab - this.changeTab = (tab) => { - $scope.currentTab = tab; - this.initializeError(); - }; - - // add input of charfilter function - this.addCharfilter = ($index) => { - //TODO check not having '' in current charfilters - $scope.formValues.charfilters.push({'item': '', 'id': $scope.formValues.charfilters.length}); - }; - // remove input of charfilter function - this.removeCharfilter = ($index) => { - $scope.formValues.charfilters.splice($index, 1); - }; - - // add input of filter function - this.addFilter = ($index) => { - //TODO check not having '' in current filters - $scope.formValues.filters.push({'item': '', 'id': $scope.formValues.filters.length}); - }; - // remove input of charfilter function - this.removeFilter = ($index) => { - $scope.formValues.filters.splice($index, 1); - }; - - // add input of analyzer function - this.addAnalyzer = ($index) => { - //TODO check not having '' in current analyzers - $scope.formValues.analyzersForCompare.push({'item': '', 'id': $scope.formValues.charfilters.length}); - }; - // remove input of analyzer function - this.removeAnalyzer = ($index) => { - $scope.formValues.analyzersForCompare.splice($index, 1); - }; - - // - this.parseCustom = (target) => { - if (typeof target === 'string' && target.startsWith('{')) { - try { - var tmpJson = JSON.parse(target); - if (tmpJson !== null && typeof tmpJson === 'object') { - return tmpJson; - } else { - $scope.analyzerError = 'charfilter has wrong custom charfilter'; - return -1; - } - } catch (e) { - $scope.analyzerError = e.message; - return -1; - } - } else { - return target; - } - }; - - // show short name - this.shortenName = (name) => { - if (name.indexOf('.') > 0) { - return name.substr(name.lastIndexOf('.')+1); - } - return name; - } - - // count tokenstream length - this.countTokenSteamLength = (detail) => { - // FIXME tokens length is not fit if it has synonym token/compound token... - var tokenStreamLength = 0; - - if (detail.tokenizer) { - tokenStreamLength = this.getLength(tokenStreamLength, detail.tokenizer.tokens); - } else if (detail.analyzer) { - tokenStreamLength = this.getLength(tokenStreamLength, detail.analyzer.tokens); - } - if (detail.tokenfilters) { - detail.tokenfilters.forEach( (filter) => { - tokenStreamLength = this.getLength(tokenStreamLength, filter.tokens); - }); - } - $scope.tokenIndicesArray = []; - for (var i = 0; i < tokenStreamLength; i++) { - $scope.tokenIndicesArray.push(i); - } - }; - - // compare and swap tokenStreamLength - this.getLength = (current, tokenArray) => { - // FIXME check if there is synonyms or compound - var length = current; - if (tokenArray != null) { - // FIXME must consider the situation if positionIncrements != 1 - if (tokenArray[tokenArray.length -1].position >= current) { - length = tokenArray[tokenArray.length -1].position + 1; - } - } - return length; - }; - - // for display - this.getTokenFromTokenstream = (index, target1, target2) => { - var target = target1; - if (target == null && target2 != null) { - target = target2; - } - $scope.currentLevelTokenList = []; - for (var token of target.tokens) { - if (token.position > index) { - break; - } - if (token.position == index) { - $scope.currentLevelTokenList.push(token); - } - } - return $scope.currentLevelTokenList.length > 0; - } - - // filter token properties - this.filteredCurrentTokenInfo = (token) => { - if (token != null) { - var result = {}; - Object.keys(token).forEach((key) => { - if (!this.hiddenTokenProperties.includes(key)) { - result[key] = token[key]; - } - }); - return result; - } else { - return null; - } - }; - - // switch show/hide properties - this.hideTokenProperty = (propertyName) => { - if (this.alwaysShowTokenProperties.includes(propertyName)) { - return true; - } else { - // TODO should we handle each attribute to show/hide? - return $scope.showAllAttr; - } - }; - - // Call analyze function - this.performAnalyze = () => { - // initialize - this.initializeError(); - - // FIXME validation logic, text is required - let param = { - text: $scope.formValues.text - }; - if ($scope.formValues.text.trim().length == 0) { - $scope.textError = 'text should be not null!'; - return; - } - if ($scope.formValues.indexName.length > 0) - param.indexName = $scope.formValues.indexName.trim(); - - if ($scope.currentTab == 'analyzer') { - if ($scope.formValues.analyzer.length > 0) - param.analyzer = $scope.formValues.analyzer.trim(); - } else if ($scope.currentTab == 'field') { - if ($scope.formValues.field.trim().length == 0) - $scope.analyzerError = 'field is required. '; - if ($scope.formValues.indexName.trim().length == 0) - $scope.analyzerError += 'index name is required for "field". '; - if ($scope.analyzerError) { - return; - } - param.field = $scope.formValues.field.trim(); - } else if ($scope.currentTab == 'custom_analyzer'){ - if ($scope.formValues.tokenizer) { - var tmpObj = this.parseCustom($scope.formValues.tokenizer.trim()); - if (tmpObj != -1) { - param.tokenizer = tmpObj; - tmpObj = null; - } else { - return; - } - } - // FIXME get tokenizer/char_filter/filter - if ($scope.formValues.charfilters.length > 0) { - $scope.formValues.charfilters.forEach( (charfilter) => { - if (charfilter && charfilter.item && charfilter.item.trim().length > 0 ) { - if(param.charfilters == null) param.charfilters = []; - var tmpCharfilter = this.parseCustom(charfilter.item.trim()); - if (tmpCharfilter != -1) { - param.charfilters.push(tmpCharfilter); - } else { - return; - } - } - }); - } - if ($scope.formValues.filters.length > 0) { - $scope.formValues.filters.forEach( (filter) => { - if (filter && filter.item && filter.item.trim().length > 0 ) { - if(param.filters == null) param.filters = []; - var tmpFilter = this.parseCustom(filter.item.trim()); - if (tmpFilter != -1) { - param.filters.push(tmpFilter); - } else { - return; - } - } - }); - } - } else if ($scope.currentTab == 'compare analyzers') { - $scope.formValues.analyzersForCompare.forEach( (analyzer) => { - if (!(analyzer && analyzer.item && analyzer.item.trim().length > 0)) { -console.log("some analyzer is null"); - return; - } - }); - } - - if ($scope.currentTab !== 'compare analyzers'){ - // call kibana server API - $http.post(chrome.addBasePath('/api/analyze-api-ui-plugin/analyze'), param) - .then( - (response) => { - $scope.detail = response.data.detail; - this.countTokenSteamLength(response.data.detail); - $scope.esrequest = response.data.request; - $scope.show_esrequest = true; - $scope.show_result = true; - $scope.showAllAttr = false; - }) - .catch( error => { - if (error.data.statusCode == 404) { - $scope.indexNameError = error.data.message; - } else if (error.data.statusCode == 400) { - $scope.analyzerError = error.data.message; - } else { - notify.error(error.data); - } - }); - } else { -console.log("comparing!!!"); - // multiple analyzers - param.analyzers = $scope.formValues.analyzersForCompare; - - $http.post(chrome.addBasePath('/api/analyze-api-ui-plugin/multi_analyze'), param) - .then( - (response) => { - $scope.resultAnalyzers = response.data.resultAnalyzers; - $scope.show_result = true; - var tokenStreamLength = 0; - $scope.resultAnalyzers.forEach( (result) => { - - tokenStreamLength = this.getLength(tokenStreamLength, result.tokens); - $scope.tokenIndicesArray = []; - for (var i = 0; i < tokenStreamLength; i++) { - $scope.tokenIndicesArray.push(i); - } - }); - }) - .catch( error => { - if (error.data.statusCode == 404) { - $scope.indexNameError = error.data.message; - } else if (error.data.statusCode == 400) { - $scope.analyzerError = error.data.message; - } else { - notify.error(error.data); - } - }); - } - }; - -}); +.controller('analyzeApiUiPluginController', analyzeApiUiPluginController); diff --git a/public/pagestate.js b/public/pagestate.js new file mode 100644 index 0000000..b56d19b --- /dev/null +++ b/public/pagestate.js @@ -0,0 +1,22 @@ +const storage = require('./storage'); + +const pagestate = { + + updateCurrentState(content) { + const timestamp = new Date().getTime(); + storage.set("page_state", { + time: timestamp, + content: content + }); + }, + + getSavedEditorState() { + const saved = storage.get('page_state'); + if (!saved) return; + const { time, content } = saved; + return { time, content }; + } + +}; + +export default pagestate; diff --git a/public/storage.js b/public/storage.js new file mode 100644 index 0000000..6276945 --- /dev/null +++ b/public/storage.js @@ -0,0 +1,60 @@ +const { transform, keys, startsWith } = require('lodash'); + +class Storage { + constructor(engine, prefix) { + this.engine = engine; + this.prefix = prefix; + } + + encode(val) { + return JSON.stringify(val); + } + + decode(val) { + if (typeof val === 'string') { + return JSON.parse(val); + } + } + + encodeKey(key) { + return `${this.prefix}${key}`; + } + + decodeKey(key) { + if (startsWith(key, this.prefix)) { + return `${key.slice(this.prefix.length)}`; + } + } + + set(key, val) { + this.engine.setItem(this.encodeKey(key), this.encode(val)); + return val; + } + + has(key) { + return this.engine.getItem(this.encodeKey(key)) != null; + } + + get(key, _default) { + if (this.has(key)) { + return this.decode(this.engine.getItem(this.encodeKey(key))); + } else { + return _default; + } + } + + delete(key) { + return this.engine.removeItem(this.encodeKey(key)); + } + + keys() { + return transform(keys(this.engine), (ours, key) => { + const ourKey = this.decodeKey(key); + if (ourKey != null) ours.push(ourKey); + }); + } +} + +const instance = new Storage(sessionStorage, 'analyzeui:'); + +export default instance; diff --git a/public/utils.js b/public/utils.js new file mode 100644 index 0000000..007861e --- /dev/null +++ b/public/utils.js @@ -0,0 +1,53 @@ +import pagestate from './pagestate'; + +const utils = {}; + +// initialize array +utils.initialItems = (size) => { + const items = []; + for (let i = 0; i < size; i++) { + items.push({ 'item': '', 'id': i }); + } + return items; +}; + +// initialize form values +utils.initializeFormValues = (scope) => { + scope.formValues = { + indexName: '', + text: '', + analyzer: '', + tokenizer: '', + charfilters: utils.initialItems(1), + filters: utils.initialItems(1), + field: '', + analyzersForCompare: utils.initialItems(2) + }; +}; + +// load saved state from storage +utils.loadSavedState = (scope) => { + const preState = pagestate.getSavedEditorState(); + + if (preState) { + scope.formValues = preState.content.formValues; + scope.currentTab = preState.content.currentTab; + } else { + utils.initializeFormValues(scope); + scope.currentTab = scope.services[0]; + } +}; + +// save state +utils.saveState = (scope) => { + try { + pagestate.updateCurrentState({ + currentTab: scope.currentTab, + formValues: scope.formValues + }); + } catch (e) { + console.log("Ignoring saving error: " + e); + } +}; + +export default utils; diff --git a/server/routes/analyze.js b/server/routes/analyze.js index 8db12d3..a5834de 100644 --- a/server/routes/analyze.js +++ b/server/routes/analyze.js @@ -1,3 +1,4 @@ +import Joi from 'joi'; import { convertEsError } from './handle_es_error'; export default function (server) { @@ -7,6 +8,19 @@ export default function (server) { server.route({ path: '/api/analyze-api-ui-plugin/analyze', method: 'POST', + config: { + validate: { + payload: Joi.object().keys({ + text: Joi.string().required(), + indexName: Joi.string().allow(null).optional(), + analyzer: Joi.string().allow(null).optional(), + tokenizer: Joi.string().allow(null).optional(), + field: Joi.string().allow(null).optional(), + filters: Joi.array().allow(null).items(Joi.string(), Joi.object()).optional(), + charfilters: Joi.array().allow(null).items(Joi.string(), Joi.object()).optional() + }).required() + } + }, handler(req, reply) { // get params from req @@ -40,6 +54,18 @@ export default function (server) { server.route({ path: '/api/analyze-api-ui-plugin/multi_analyze', method: 'POST', + config: { + validate: { + payload: Joi.object().keys({ + text: Joi.string().required(), + indexName: Joi.string().allow(null).optional(), + analyzers: Joi.array().allow(null).items(Joi.object().keys({ + item: [Joi.string(), Joi.object()], + id: Joi.number().optional() + })).min(2) + }).required() + } + }, handler(req, reply) { // get params from req @@ -51,7 +77,7 @@ export default function (server) { } }; if (req.payload.indexName) param.index = req.payload.indexName; - var res = { + const res = { resultAnalyzers: [] }; @@ -69,14 +95,18 @@ export default function (server) { }); }; - Promise.all( - req.payload.analyzers.map(getAnalyzerResult)) - .then(function (response) { - reply(res); - }) - .catch(error => { - reply(convertEsError(param.index, error)); - }); + if (Array.isArray(req.payload.analyzers) && req.payload.analyzers.length >= 1) { + Promise.all( + req.payload.analyzers.map(getAnalyzerResult)) + .then(function (response) { + reply(res); + }) + .catch(error => { + reply(convertEsError(param.index, error)); + }); + } else { + reply(res); + } } });