ElasticsearchInterpreter extends Interpreter { @Override public InterpreterResult interpret(String cmd, InterpreterContext interpreterContext) { logger.info("Run Elasticsearch command '" + cmd + "'"); if (StringUtils.isEmpty(cmd) || StringUtils.isEmpty(cmd.trim())) { return new InterpreterResult(InterpreterResult.Code.SUCCESS); } int currentResultSize = resultSize; if (client == null) { return new InterpreterResult(InterpreterResult.Code.ERROR, "Problem with the Elasticsearch client, please check your configuration (host, port,...)"); } String[] items = StringUtils.split(cmd.trim(), " ", 3); if ("help".equalsIgnoreCase(items[0])) { return processHelp(InterpreterResult.Code.SUCCESS, null); } if ("size".equalsIgnoreCase(items[0])) { final String[] lines = StringUtils.split(cmd.trim(), "\n", 2); if (lines.length < 2) { return processHelp(InterpreterResult.Code.ERROR, "Size cmd must be followed by a search"); } final String[] sizeLine = StringUtils.split(lines[0], " ", 2); if (sizeLine.length != 2) { return processHelp(InterpreterResult.Code.ERROR, "Right format is : size <value>"); } currentResultSize = Integer.parseInt(sizeLine[1]); items = StringUtils.split(lines[1].trim(), " ", 3); } if (items.length < 2) { return processHelp(InterpreterResult.Code.ERROR, "Arguments missing"); } final String method = items[0]; final String url = items[1]; final String data = items.length > 2 ? items[2].trim() : null; final String[] urlItems = StringUtils.split(url.trim(), "/"); try { if ("get".equalsIgnoreCase(method)) { return processGet(urlItems); } else if ("count".equalsIgnoreCase(method)) { return processCount(urlItems, data); } else if ("search".equalsIgnoreCase(method)) { return processSearch(urlItems, data, currentResultSize); } else if ("index".equalsIgnoreCase(method)) { return processIndex(urlItems, data); } else if ("delete".equalsIgnoreCase(method)) { return processDelete(urlItems); } return processHelp(InterpreterResult.Code.ERROR, "Unknown command"); } catch (Exception e) { return new InterpreterResult(InterpreterResult.Code.ERROR, "Error : " + e.getMessage()); } } ElasticsearchInterpreter(Properties property); @Override void open(); @Override void close(); @Override InterpreterResult interpret(String cmd, InterpreterContext interpreterContext); @Override void cancel(InterpreterContext interpreterContext); @Override FormType getFormType(); @Override int getProgress(InterpreterContext interpreterContext); @Override List<InterpreterCompletion> completion(String s, int i); static final String ELASTICSEARCH_HOST; static final String ELASTICSEARCH_PORT; static final String ELASTICSEARCH_CLUSTER_NAME; static final String ELASTICSEARCH_RESULT_SIZE; }
@Test public void testCount() { InterpreterResult res = interpreter.interpret("count /unknown", null); assertEquals(Code.ERROR, res.code()); res = interpreter.interpret("count /logs", null); assertEquals("50", res.message()); }
@Test public void testGet() { InterpreterResult res = interpreter.interpret("get /logs/http/unknown", null); assertEquals(Code.ERROR, res.code()); res = interpreter.interpret("get /logs/http/10", null); assertEquals(Code.SUCCESS, res.code()); }
@Test public void testSearch() { InterpreterResult res = interpreter.interpret("size 10\nsearch /logs *", null); assertEquals(Code.SUCCESS, res.code()); res = interpreter.interpret("search /logs {{{hello}}}", null); assertEquals(Code.ERROR, res.code()); res = interpreter.interpret("search /logs { \"query\": { \"match\": { \"status\": 500 } } }", null); assertEquals(Code.SUCCESS, res.code()); res = interpreter.interpret("search /logs status:404", null); assertEquals(Code.SUCCESS, res.code()); res = interpreter.interpret("search /logs { \"fields\": [ \"date\", \"request.headers\" ], \"query\": { \"match\": { \"status\": 500 } } }", null); assertEquals(Code.SUCCESS, res.code()); }
@Test public void testAgg() { InterpreterResult res = interpreter.interpret("search /logs { \"aggs\" : { \"distinct_status_count\" : " + " { \"cardinality\" : { \"field\" : \"status\" } } } }", null); assertEquals(Code.SUCCESS, res.code()); res = interpreter.interpret("search /logs { \"aggs\" : { \"content_length_stats\" : " + " { \"extended_stats\" : { \"field\" : \"content_length\" } } } }", null); assertEquals(Code.SUCCESS, res.code()); res = interpreter.interpret("search /logs { \"aggs\" : { " + " \"200_OK\" : { \"filter\" : { \"term\": { \"status\": \"200\" } }, " + " \"aggs\" : { \"avg_length\" : { \"avg\" : { \"field\" : \"content_length\" } } } } } }", null); assertEquals(Code.SUCCESS, res.code()); res = interpreter.interpret("search /logs { \"aggs\" : { \"status_count\" : " + " { \"terms\" : { \"field\" : \"status\" } } } }", null); assertEquals(Code.SUCCESS, res.code()); }
@Test public void testIndex() { InterpreterResult res = interpreter.interpret("index /logs { \"date\": \"" + new Date() + "\", \"method\": \"PUT\", \"status\": \"500\" }", null); assertEquals(Code.ERROR, res.code()); res = interpreter.interpret("index /logs/http { \"date\": \"2015-12-06T14:54:23.368Z\", \"method\": \"PUT\", \"status\": \"500\" }", null); assertEquals(Code.SUCCESS, res.code()); }
@Test public void testDelete() { InterpreterResult res = interpreter.interpret("delete /logs/http/unknown", null); assertEquals(Code.ERROR, res.code()); res = interpreter.interpret("delete /logs/http/11", null); assertEquals("11", res.message()); }
@Test public void testMisc() { InterpreterResult res = interpreter.interpret(null, null); assertEquals(Code.SUCCESS, res.code()); res = interpreter.interpret(" \n \n ", null); assertEquals(Code.SUCCESS, res.code()); }