{"action":"created","comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/43733745","id":43733745,"diff_hunk":"@@ -0,0 +1,312 @@\n+/*\n+ * Licensed to Elasticsearch under one or more contributor\n+ * license agreements. See the NOTICE file distributed with\n+ * this work for additional information regarding copyright\n+ * ownership. Elasticsearch licenses this file to you under\n+ * the Apache License, Version 2.0 (the \"License\"); you may\n+ * not use this file except in compliance with the License.\n+ * 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,\n+ * software distributed under the License is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied.  See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+\n+package org.elasticsearch.ingest.processor.mutate;\n+\n+import org.elasticsearch.ingest.Data;\n+import org.elasticsearch.ingest.processor.Processor;\n+\n+import java.util.*;\n+import java.util.stream.Collectors;\n+\n+public final class MutateProcessor implements Processor {\n+\n+    public static final String TYPE = \"mutate\";\n+\n+    private final Map<String, Object> update;\n+    private final Map<String, String> rename;\n+    private final Map<String, String> convert;\n+    private final Map<String, String> split;\n+    private final Map<String, List<String>> gsub;\n+    private final Map<String, String> join;\n+    private final List<String> remove;\n+    private final List<String> strip;\n+    private final List<String> uppercase;\n+    private final List<String> lowercase;\n+\n+    public MutateProcessor(Map<String, Object> update,\n+                           Map<String, String> rename,\n+                           Map<String, String> convert,\n+                           Map<String, String> split,\n+                           Map<String, List<String>> gsub,\n+                           Map<String, String> join,\n+                           List<String> remove,\n+                           List<String> strip,\n+                           List<String> uppercase,\n+                           List<String> lowercase) {\n+        this.update = update;\n+        this.rename = rename;\n+        this.convert = convert;\n+        this.split = split;\n+        this.gsub = gsub;\n+        this.join = join;\n+        this.remove = remove;\n+        this.strip = strip;\n+        this.uppercase = uppercase;\n+        this.lowercase = lowercase;\n+    }\n+\n+    @Override\n+    public void execute(Data data) {\n+        doUpdate(data);\n+        doRename(data);\n+        doConvert(data);\n+        doSplit(data);\n+        doGsub(data);\n+        doJoin(data);\n+        doRemove(data);\n+        doStrip(data);\n+        doUppercase(data);\n+        doLowercase(data);\n+    }\n+\n+    private void doUpdate(Data data) {\n+        if (update == null) { return; }\n+\n+        for(Map.Entry<String, Object> entry : update.entrySet()) {\n+            data.addField(entry.getKey(), entry.getValue());\n+        }\n+    }\n+\n+    private void doRename(Data data) {\n+        if (rename == null) { return; }\n+\n+        for(Map.Entry<String, String> entry : rename.entrySet()) {\n+            Object oldVal = data.getProperty(entry.getKey());\n+            data.addField(entry.getValue(), oldVal);\n+        }\n+    }\n+\n+    private Object parseValueAsType(Object oldVal, String toType) {\n+        if(\"integer\".equals(toType)) {\n+            oldVal = Integer.parseInt(oldVal.toString());\n+        } else if(\"float\".equals(toType)) {\n+            oldVal = Float.parseFloat(oldVal.toString());\n+        } else if(\"string\".equals(toType)) {\n+            oldVal = oldVal.toString();\n+        } else if(\"boolean\".equals(toType)) {\n+            String val = oldVal.toString();\n+            if (val.equals(\"true\") || val.equals(\"t\") || val.equals(\"yes\") || val.equals(\"y\") || val.equals(\"1\")) {\n+                oldVal = true;\n+            } else if (val.equals(\"false\") || val.equals(\"f\") || val.equals(\"no\") || val.equals(\"n\") || val.equals(\"0\")) {\n+                oldVal = false;\n+            } else {\n+                throw new IllegalArgumentException(\"Cannot convert value: \" + oldVal.toString() + \"into a boolean\");\n+            }\n+        }\n+        return oldVal;\n+    }\n+\n+    @SuppressWarnings(\"unchecked\")\n+    private void doConvert(Data data) {\n+        if (convert == null) { return; }\n+\n+        for(Map.Entry<String, String> entry : convert.entrySet()) {\n+            String toType = entry.getValue();\n+\n+            Object oldVal = data.getProperty(entry.getKey());\n+            Object newVal;\n+\n+            if (oldVal instanceof List) {\n+                newVal = new ArrayList<>();\n+                for (Object e : ((List<Object>) oldVal)) {\n+                    ((List<Object>) newVal).add(parseValueAsType(e, toType));\n+                }\n+            } else {\n+                newVal = parseValueAsType(oldVal, toType);\n+            }\n+\n+            data.addField(entry.getKey(), newVal);\n+        }\n+    }\n+\n+    private void doSplit(Data data) {\n+        if (split == null) { return; }\n+\n+        for(Map.Entry<String, String> entry : split.entrySet()) {\n+            Object oldVal = data.getProperty(entry.getKey());\n+            if (oldVal instanceof String) {\n+                data.addField(entry.getKey(), Arrays.asList(((String) oldVal).split(entry.getValue())));\n+            } else {\n+                throw new IllegalArgumentException(\"Cannot split a field that is not a String type\");\n+            }\n+        }\n+    }\n+\n+    private void doGsub(Data data) {\n+        if (gsub == null) { return; }\n+\n+        for (Map.Entry<String, List<String>> entry : gsub.entrySet()) {\n+            String fieldName = entry.getKey();\n+            List<String> matchAndReplaceList = entry.getValue();\n+            String oldVal = data.getProperty(fieldName);\n+            String newVal = oldVal.replaceAll(matchAndReplaceList.get(0), matchAndReplaceList.get(1));\n+            data.addField(entry.getKey(), newVal);\n+        }\n+    }\n+\n+    @SuppressWarnings(\"unchecked\")\n+    private void doJoin(Data data) {\n+        if (join == null) { return; }\n+\n+        for(Map.Entry<String, String> entry : join.entrySet()) {\n+            Object oldVal = data.getProperty(entry.getKey());\n+            if (oldVal instanceof List) {\n+                String joined = (String) ((List) oldVal)\n+                        .stream()\n+                        .map(Object::toString)\n+                        .collect(Collectors.joining(entry.getValue()));\n+\n+                data.addField(entry.getKey(), joined);\n+            } else {\n+                throw new IllegalArgumentException(\"Cannot join field:\" + entry.getKey() + \" with type: \" + oldVal.getClass());\n+            }\n+        }\n+    }\n+\n+    private void doRemove(Data data) {\n+        if (remove == null) { return; }\n+        for(String field : remove) {\n+            data.getDocument().remove(field);\n+        }\n+    }\n+\n+    private void doStrip(Data data) {\n+        if (strip == null) { return; }\n+\n+        for(String field : strip) {\n+            Object val = data.getProperty(field);\n+            if (val instanceof String) {\n+                data.addField(field, ((String) val).trim());\n+            } else {\n+                throw new IllegalArgumentException(\"Cannot strip field:\" + field + \" with type: \" + val.getClass());\n+            }\n+        }\n+    }\n+\n+    private void doUppercase(Data data) {\n+        if (uppercase == null) { return; }\n+\n+        for(String field : uppercase) {\n+            Object val = data.getProperty(field);\n+            if (val instanceof String) {\n+                data.addField(field, ((String) val).toUpperCase(Locale.US));\n+            } else {\n+                throw new IllegalArgumentException(\"Cannot uppercase field:\" + field + \" with type: \" + val.getClass());","path":"plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/mutate/MutateProcessor.java","position":211,"original_position":211,"commit_id":"2d6b3d5aa5ec18621df02719bfc4f43bfe452759","original_commit_id":"2d6b3d5aa5ec18621df02719bfc4f43bfe452759","user":{"login":"martijnvg","id":580421,"avatar_url":"https://avatars.githubusercontent.com/u/580421?v=3","gravatar_id":"","url":"https://api.github.com/users/martijnvg","html_url":"https://github.com/martijnvg","followers_url":"https://api.github.com/users/martijnvg/followers","following_url":"https://api.github.com/users/martijnvg/following{/other_user}","gists_url":"https://api.github.com/users/martijnvg/gists{/gist_id}","starred_url":"https://api.github.com/users/martijnvg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martijnvg/subscriptions","organizations_url":"https://api.github.com/users/martijnvg/orgs","repos_url":"https://api.github.com/users/martijnvg/repos","events_url":"https://api.github.com/users/martijnvg/events{/privacy}","received_events_url":"https://api.github.com/users/martijnvg/received_events","type":"User","site_admin":false},"body":"should we be strict or lenient when a field isn't a string.","created_at":"2015-11-03T10:36:37Z","updated_at":"2015-11-03T10:36:37Z","html_url":"https://github.com/elastic/elasticsearch/pull/14253#discussion_r43733745","pull_request_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/14253","_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/43733745"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/14253#discussion_r43733745"},"pull_request":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/14253"}}},"pull_request":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/14253","id":48467016,"html_url":"https://github.com/elastic/elasticsearch/pull/14253","diff_url":"https://github.com/elastic/elasticsearch/pull/14253.diff","patch_url":"https://github.com/elastic/elasticsearch/pull/14253.patch","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/14253","number":14253,"state":"open","locked":false,"title":"[WIP] Mutate Processor","user":{"login":"talevy","id":388837,"avatar_url":"https://avatars.githubusercontent.com/u/388837?v=3","gravatar_id":"","url":"https://api.github.com/users/talevy","html_url":"https://github.com/talevy","followers_url":"https://api.github.com/users/talevy/followers","following_url":"https://api.github.com/users/talevy/following{/other_user}","gists_url":"https://api.github.com/users/talevy/gists{/gist_id}","starred_url":"https://api.github.com/users/talevy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/talevy/subscriptions","organizations_url":"https://api.github.com/users/talevy/orgs","repos_url":"https://api.github.com/users/talevy/repos","events_url":"https://api.github.com/users/talevy/events{/privacy}","received_events_url":"https://api.github.com/users/talevy/received_events","type":"User","site_admin":false},"body":"meant to have feature parity with [logstash-filter-mutate](https://github.com/logstash-plugins/logstash-filter-mutate)","created_at":"2015-10-22T14:48:28Z","updated_at":"2015-11-03T10:36:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0bee1089f08ce2b63680200457048b51b7c53fec","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/14253/commits","review_comments_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/14253/comments","review_comment_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/14253/comments","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/2d6b3d5aa5ec18621df02719bfc4f43bfe452759","head":{"label":"talevy:ingest/mutate","ref":"ingest/mutate","sha":"2d6b3d5aa5ec18621df02719bfc4f43bfe452759","user":{"login":"talevy","id":388837,"avatar_url":"https://avatars.githubusercontent.com/u/388837?v=3","gravatar_id":"","url":"https://api.github.com/users/talevy","html_url":"https://github.com/talevy","followers_url":"https://api.github.com/users/talevy/followers","following_url":"https://api.github.com/users/talevy/following{/other_user}","gists_url":"https://api.github.com/users/talevy/gists{/gist_id}","starred_url":"https://api.github.com/users/talevy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/talevy/subscriptions","organizations_url":"https://api.github.com/users/talevy/orgs","repos_url":"https://api.github.com/users/talevy/repos","events_url":"https://api.github.com/users/talevy/events{/privacy}","received_events_url":"https://api.github.com/users/talevy/received_events","type":"User","site_admin":false},"repo":{"id":25600001,"name":"elasticsearch","full_name":"talevy/elasticsearch","owner":{"login":"talevy","id":388837,"avatar_url":"https://avatars.githubusercontent.com/u/388837?v=3","gravatar_id":"","url":"https://api.github.com/users/talevy","html_url":"https://github.com/talevy","followers_url":"https://api.github.com/users/talevy/followers","following_url":"https://api.github.com/users/talevy/following{/other_user}","gists_url":"https://api.github.com/users/talevy/gists{/gist_id}","starred_url":"https://api.github.com/users/talevy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/talevy/subscriptions","organizations_url":"https://api.github.com/users/talevy/orgs","repos_url":"https://api.github.com/users/talevy/repos","events_url":"https://api.github.com/users/talevy/events{/privacy}","received_events_url":"https://api.github.com/users/talevy/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/talevy/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":true,"url":"https://api.github.com/repos/talevy/elasticsearch","forks_url":"https://api.github.com/repos/talevy/elasticsearch/forks","keys_url":"https://api.github.com/repos/talevy/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/talevy/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/talevy/elasticsearch/teams","hooks_url":"https://api.github.com/repos/talevy/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/talevy/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/talevy/elasticsearch/events","assignees_url":"https://api.github.com/repos/talevy/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/talevy/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/talevy/elasticsearch/tags","blobs_url":"https://api.github.com/repos/talevy/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/talevy/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/talevy/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/talevy/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/talevy/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/talevy/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/talevy/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/talevy/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/talevy/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/talevy/elasticsearch/subscription","commits_url":"https://api.github.com/repos/talevy/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/talevy/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/talevy/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/talevy/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/talevy/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/talevy/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/talevy/elasticsearch/merges","archive_url":"https://api.github.com/repos/talevy/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/talevy/elasticsearch/downloads","issues_url":"https://api.github.com/repos/talevy/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/talevy/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/talevy/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/talevy/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/talevy/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/talevy/elasticsearch/releases{/id}","created_at":"2014-10-22T19:25:53Z","updated_at":"2015-04-14T21:19:32Z","pushed_at":"2015-11-03T05:32:13Z","git_url":"git://github.com/talevy/elasticsearch.git","ssh_url":"git@github.com:talevy/elasticsearch.git","clone_url":"https://github.com/talevy/elasticsearch.git","svn_url":"https://github.com/talevy/elasticsearch","homepage":"http://elasticsearch.org","size":170706,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"elastic:feature/ingest","ref":"feature/ingest","sha":"a8d76f011783c8b03464e94ea45210952b56c821","user":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"repo":{"id":507775,"name":"elasticsearch","full_name":"elastic/elasticsearch","owner":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elastic/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":false,"url":"https://api.github.com/repos/elastic/elasticsearch","forks_url":"https://api.github.com/repos/elastic/elasticsearch/forks","keys_url":"https://api.github.com/repos/elastic/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elastic/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elastic/elasticsearch/teams","hooks_url":"https://api.github.com/repos/elastic/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/elastic/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/elastic/elasticsearch/events","assignees_url":"https://api.github.com/repos/elastic/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/elastic/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/elastic/elasticsearch/tags","blobs_url":"https://api.github.com/repos/elastic/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elastic/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elastic/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/elastic/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/elastic/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/elastic/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/elastic/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/elastic/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/elastic/elasticsearch/subscription","commits_url":"https://api.github.com/repos/elastic/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/elastic/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/elastic/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/elastic/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/elastic/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elastic/elasticsearch/merges","archive_url":"https://api.github.com/repos/elastic/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elastic/elasticsearch/downloads","issues_url":"https://api.github.com/repos/elastic/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/elastic/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/elastic/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/elastic/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elastic/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/elastic/elasticsearch/releases{/id}","created_at":"2010-02-08T13:20:56Z","updated_at":"2015-11-03T10:25:18Z","pushed_at":"2015-11-03T10:35:44Z","git_url":"git://github.com/elastic/elasticsearch.git","ssh_url":"git@github.com:elastic/elasticsearch.git","clone_url":"https://github.com/elastic/elasticsearch.git","svn_url":"https://github.com/elastic/elasticsearch","homepage":"https://www.elastic.co/products/elasticsearch","size":481756,"stargazers_count":13395,"watchers_count":13395,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4385,"mirror_url":null,"open_issues_count":1282,"forks":4385,"open_issues":1282,"watchers":13395,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/14253"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/14253"},"issue":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/14253"},"comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/14253/comments"},"review_comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/14253/comments"},"review_comment":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/14253/commits"},"statuses":{"href":"https://api.github.com/repos/elastic/elasticsearch/statuses/2d6b3d5aa5ec18621df02719bfc4f43bfe452759"}}}}