{"action":"created","comment":{"url":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/comments/46880445","id":46880445,"diff_hunk":"@@ -0,0 +1,369 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with\n+ * this work for additional information regarding copyright ownership.\n+ * The ASF licenses this file to You under the Apache License, Version 2.0\n+ * (the \"License\"); you may not use this file except in compliance with\n+ * the License.  You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package org.apache.zeppelin.elasticsearch;\n+\n+import java.io.IOException;\n+import java.net.InetAddress;\n+import java.util.Arrays;\n+import java.util.List;\n+import java.util.Map;\n+import java.util.Properties;\n+import java.util.Set;\n+import java.util.TreeSet;\n+\n+import org.apache.commons.lang.StringUtils;\n+import org.apache.zeppelin.interpreter.Interpreter;\n+import org.apache.zeppelin.interpreter.InterpreterContext;\n+import org.apache.zeppelin.interpreter.InterpreterPropertyBuilder;\n+import org.apache.zeppelin.interpreter.InterpreterResult;\n+import org.elasticsearch.action.delete.DeleteResponse;\n+import org.elasticsearch.action.get.GetResponse;\n+import org.elasticsearch.action.index.IndexResponse;\n+import org.elasticsearch.action.search.SearchAction;\n+import org.elasticsearch.action.search.SearchRequestBuilder;\n+import org.elasticsearch.action.search.SearchResponse;\n+import org.elasticsearch.client.Client;\n+import org.elasticsearch.client.transport.TransportClient;\n+import org.elasticsearch.common.settings.Settings;\n+import org.elasticsearch.common.transport.InetSocketTransportAddress;\n+import org.elasticsearch.search.SearchHit;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import com.google.gson.Gson;\n+import com.google.gson.GsonBuilder;\n+\n+/**\n+ * Elasticsearch Interpreter for Zeppelin.\n+ *\n+ * @author Bruno Bonnin\n+ */\n+public class ElasticsearchInterpreter extends Interpreter {\n+\n+  private static Logger logger = LoggerFactory.getLogger(ElasticsearchInterpreter.class);\n+\n+  public static final String ELASTICSEARCH_HOST = \"elasticsearch.host\";\n+  public static final String ELASTICSEARCH_PORT = \"elasticsearch.port\";\n+  public static final String ELASTICSEARCH_CLUSTER_NAME = \"elasticsearch.cluster.name\";\n+\n+  static {\n+    Interpreter.register(\n+      \"elasticsearch\",\n+      \"elasticsearch\",\n+      ElasticsearchInterpreter.class.getName(),\n+        new InterpreterPropertyBuilder()\n+          .add(ELASTICSEARCH_HOST, \"localhost\", \"The host for Elasticsearch\")\n+          .add(ELASTICSEARCH_PORT, \"9300\", \"The port for Elasticsearch\")\n+          .add(ELASTICSEARCH_CLUSTER_NAME, \"elasticsearch\", \"The cluster name for Elasticsearch\")\n+          .build());\n+  }\n+\n+  private final Gson gson = new GsonBuilder().setPrettyPrinting().create();\n+  private Client client;\n+  private String host = \"localhost\";\n+  private int port = 9300;\n+  private String clusterName = \"elasticsearch\";\n+\n+  public ElasticsearchInterpreter(Properties property) {\n+    super(property);\n+    this.host = getProperty(ELASTICSEARCH_HOST);\n+    this.port = Integer.parseInt(getProperty(ELASTICSEARCH_PORT));\n+    this.clusterName = getProperty(ELASTICSEARCH_CLUSTER_NAME);\n+  }\n+\n+  @Override\n+  public void open() {\n+    try {\n+      final Settings settings = Settings.settingsBuilder()\n+        .put(\"cluster.name\", clusterName).build();\n+      client = TransportClient.builder().settings(settings).build()\n+        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));\n+    }\n+    catch (IOException e) {\n+      logger.error(\"Open connection with Elasticsearch\", e);\n+    }\n+  }\n+\n+  @Override\n+  public void close() {\n+    if (client != null) {\n+      client.close();\n+    }\n+  }\n+\n+  @Override\n+  public InterpreterResult interpret(String cmd, InterpreterContext interpreterContext) {\n+    logger.info(\"Run Elasticsearch command '\" + cmd + \"'\");\n+    // RULES for an elasticsearch command:\n+    // <Method> /<index>/<type>/ <option> <JSON>\n+    // Method = search, get, index, delete, count\n+\n+    final String[] items = StringUtils.split(cmd.trim(), \" \", 3);\n+\n+    if (items.length < 2) {\n+      return new InterpreterResult(InterpreterResult.Code.ERROR, \"Arguments missing\");","path":"elasticsearch/src/main/java/org/apache/zeppelin/elasticsearch/ElasticsearchInterpreter.java","position":119,"original_position":119,"commit_id":"a10a5ec262b0c3e4dfbafdfe1958f1d0cf6129f6","original_commit_id":"a10a5ec262b0c3e4dfbafdfe1958f1d0cf6129f6","user":{"login":"felixcheung","id":8969467,"avatar_url":"https://avatars.githubusercontent.com/u/8969467?v=3","gravatar_id":"","url":"https://api.github.com/users/felixcheung","html_url":"https://github.com/felixcheung","followers_url":"https://api.github.com/users/felixcheung/followers","following_url":"https://api.github.com/users/felixcheung/following{/other_user}","gists_url":"https://api.github.com/users/felixcheung/gists{/gist_id}","starred_url":"https://api.github.com/users/felixcheung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/felixcheung/subscriptions","organizations_url":"https://api.github.com/users/felixcheung/orgs","repos_url":"https://api.github.com/users/felixcheung/repos","events_url":"https://api.github.com/users/felixcheung/events{/privacy}","received_events_url":"https://api.github.com/users/felixcheung/received_events","type":"User","site_admin":false},"body":"perhaps return error with the message like in the comment above:\r\n```\r\n// <Method> /<index>/<type>/ <option> <JSON>\r\n// Method = search, get, index, delete, count\r\n```","created_at":"2015-12-07T21:30:45Z","updated_at":"2015-12-07T21:30:45Z","html_url":"https://github.com/apache/incubator-zeppelin/pull/520#discussion_r46880445","pull_request_url":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/520","_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/comments/46880445"},"html":{"href":"https://github.com/apache/incubator-zeppelin/pull/520#discussion_r46880445"},"pull_request":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/520"}}},"pull_request":{"url":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/520","id":52890521,"html_url":"https://github.com/apache/incubator-zeppelin/pull/520","diff_url":"https://github.com/apache/incubator-zeppelin/pull/520.diff","patch_url":"https://github.com/apache/incubator-zeppelin/pull/520.patch","issue_url":"https://api.github.com/repos/apache/incubator-zeppelin/issues/520","number":520,"state":"open","locked":false,"title":"Add an Elasticsearch interpreter","user":{"login":"bbonnin","id":1908593,"avatar_url":"https://avatars.githubusercontent.com/u/1908593?v=3","gravatar_id":"","url":"https://api.github.com/users/bbonnin","html_url":"https://github.com/bbonnin","followers_url":"https://api.github.com/users/bbonnin/followers","following_url":"https://api.github.com/users/bbonnin/following{/other_user}","gists_url":"https://api.github.com/users/bbonnin/gists{/gist_id}","starred_url":"https://api.github.com/users/bbonnin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbonnin/subscriptions","organizations_url":"https://api.github.com/users/bbonnin/orgs","repos_url":"https://api.github.com/users/bbonnin/repos","events_url":"https://api.github.com/users/bbonnin/events{/privacy}","received_events_url":"https://api.github.com/users/bbonnin/received_events","type":"User","site_admin":false},"body":"### Elasticsearch Interpreter\r\nInterpreter for querying ElasticSearch .\r\nSupported requests are \"get document by id\" , \"search documents\" , \"delete by id\" , \"count documents\" and \"index / update a document\".\r\nSupported versions of Elasticsearch : >= 2.1","created_at":"2015-12-07T19:41:20Z","updated_at":"2015-12-07T21:30:45Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f94ff283bccaa69b8ac80ee9971c1c61e0566707","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/520/commits","review_comments_url":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/520/comments","review_comment_url":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/incubator-zeppelin/issues/520/comments","statuses_url":"https://api.github.com/repos/apache/incubator-zeppelin/statuses/a10a5ec262b0c3e4dfbafdfe1958f1d0cf6129f6","head":{"label":"bbonnin:master","ref":"master","sha":"a10a5ec262b0c3e4dfbafdfe1958f1d0cf6129f6","user":{"login":"bbonnin","id":1908593,"avatar_url":"https://avatars.githubusercontent.com/u/1908593?v=3","gravatar_id":"","url":"https://api.github.com/users/bbonnin","html_url":"https://github.com/bbonnin","followers_url":"https://api.github.com/users/bbonnin/followers","following_url":"https://api.github.com/users/bbonnin/following{/other_user}","gists_url":"https://api.github.com/users/bbonnin/gists{/gist_id}","starred_url":"https://api.github.com/users/bbonnin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbonnin/subscriptions","organizations_url":"https://api.github.com/users/bbonnin/orgs","repos_url":"https://api.github.com/users/bbonnin/repos","events_url":"https://api.github.com/users/bbonnin/events{/privacy}","received_events_url":"https://api.github.com/users/bbonnin/received_events","type":"User","site_admin":false},"repo":{"id":47541467,"name":"incubator-zeppelin","full_name":"bbonnin/incubator-zeppelin","owner":{"login":"bbonnin","id":1908593,"avatar_url":"https://avatars.githubusercontent.com/u/1908593?v=3","gravatar_id":"","url":"https://api.github.com/users/bbonnin","html_url":"https://github.com/bbonnin","followers_url":"https://api.github.com/users/bbonnin/followers","following_url":"https://api.github.com/users/bbonnin/following{/other_user}","gists_url":"https://api.github.com/users/bbonnin/gists{/gist_id}","starred_url":"https://api.github.com/users/bbonnin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbonnin/subscriptions","organizations_url":"https://api.github.com/users/bbonnin/orgs","repos_url":"https://api.github.com/users/bbonnin/repos","events_url":"https://api.github.com/users/bbonnin/events{/privacy}","received_events_url":"https://api.github.com/users/bbonnin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bbonnin/incubator-zeppelin","description":"Mirror of Apache Zeppelin (Incubating)","fork":true,"url":"https://api.github.com/repos/bbonnin/incubator-zeppelin","forks_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/forks","keys_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/teams","hooks_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/hooks","issue_events_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/issues/events{/number}","events_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/events","assignees_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/assignees{/user}","branches_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/branches{/branch}","tags_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/tags","blobs_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/git/refs{/sha}","trees_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/statuses/{sha}","languages_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/languages","stargazers_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/stargazers","contributors_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/contributors","subscribers_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/subscribers","subscription_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/subscription","commits_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/commits{/sha}","git_commits_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/git/commits{/sha}","comments_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/comments{/number}","issue_comment_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/issues/comments{/number}","contents_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/contents/{+path}","compare_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/merges","archive_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/downloads","issues_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/issues{/number}","pulls_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/pulls{/number}","milestones_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/milestones{/number}","notifications_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/labels{/name}","releases_url":"https://api.github.com/repos/bbonnin/incubator-zeppelin/releases{/id}","created_at":"2015-12-07T09:12:02Z","updated_at":"2015-12-07T09:12:04Z","pushed_at":"2015-12-07T17:46:31Z","git_url":"git://github.com/bbonnin/incubator-zeppelin.git","ssh_url":"git@github.com:bbonnin/incubator-zeppelin.git","clone_url":"https://github.com/bbonnin/incubator-zeppelin.git","svn_url":"https://github.com/bbonnin/incubator-zeppelin","homepage":null,"size":14517,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"52b41438bea41b1b7ca605a7cdad80f6bd84c2a6","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":32848140,"name":"incubator-zeppelin","full_name":"apache/incubator-zeppelin","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/incubator-zeppelin","description":"Mirror of Apache Zeppelin (Incubating)","fork":false,"url":"https://api.github.com/repos/apache/incubator-zeppelin","forks_url":"https://api.github.com/repos/apache/incubator-zeppelin/forks","keys_url":"https://api.github.com/repos/apache/incubator-zeppelin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/incubator-zeppelin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/incubator-zeppelin/teams","hooks_url":"https://api.github.com/repos/apache/incubator-zeppelin/hooks","issue_events_url":"https://api.github.com/repos/apache/incubator-zeppelin/issues/events{/number}","events_url":"https://api.github.com/repos/apache/incubator-zeppelin/events","assignees_url":"https://api.github.com/repos/apache/incubator-zeppelin/assignees{/user}","branches_url":"https://api.github.com/repos/apache/incubator-zeppelin/branches{/branch}","tags_url":"https://api.github.com/repos/apache/incubator-zeppelin/tags","blobs_url":"https://api.github.com/repos/apache/incubator-zeppelin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/incubator-zeppelin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/incubator-zeppelin/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/incubator-zeppelin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/incubator-zeppelin/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/incubator-zeppelin/languages","stargazers_url":"https://api.github.com/repos/apache/incubator-zeppelin/stargazers","contributors_url":"https://api.github.com/repos/apache/incubator-zeppelin/contributors","subscribers_url":"https://api.github.com/repos/apache/incubator-zeppelin/subscribers","subscription_url":"https://api.github.com/repos/apache/incubator-zeppelin/subscription","commits_url":"https://api.github.com/repos/apache/incubator-zeppelin/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/incubator-zeppelin/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/incubator-zeppelin/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/incubator-zeppelin/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/incubator-zeppelin/contents/{+path}","compare_url":"https://api.github.com/repos/apache/incubator-zeppelin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/incubator-zeppelin/merges","archive_url":"https://api.github.com/repos/apache/incubator-zeppelin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/incubator-zeppelin/downloads","issues_url":"https://api.github.com/repos/apache/incubator-zeppelin/issues{/number}","pulls_url":"https://api.github.com/repos/apache/incubator-zeppelin/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/incubator-zeppelin/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/incubator-zeppelin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/incubator-zeppelin/labels{/name}","releases_url":"https://api.github.com/repos/apache/incubator-zeppelin/releases{/id}","created_at":"2015-03-25T07:00:06Z","updated_at":"2015-12-07T12:21:55Z","pushed_at":"2015-12-07T19:41:22Z","git_url":"git://github.com/apache/incubator-zeppelin.git","ssh_url":"git@github.com:apache/incubator-zeppelin.git","clone_url":"https://github.com/apache/incubator-zeppelin.git","svn_url":"https://github.com/apache/incubator-zeppelin","homepage":null,"size":82524,"stargazers_count":932,"watchers_count":932,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":443,"mirror_url":"git://git.apache.org/incubator-zeppelin.git","open_issues_count":84,"forks":443,"open_issues":84,"watchers":932,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/520"},"html":{"href":"https://github.com/apache/incubator-zeppelin/pull/520"},"issue":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/issues/520"},"comments":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/issues/520/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/520/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/pulls/520/commits"},"statuses":{"href":"https://api.github.com/repos/apache/incubator-zeppelin/statuses/a10a5ec262b0c3e4dfbafdfe1958f1d0cf6129f6"}}}}