{"action":"created","comment":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/29351699","id":29351699,"diff_hunk":"@@ -79,35 +79,48 @@ public Result prepare(UpdateRequest request, IndexShard indexShard) {\n         final GetResult getResult = indexShard.getService().get(request.type(), request.id(),\n                 new String[]{RoutingFieldMapper.NAME, ParentFieldMapper.NAME, TTLFieldMapper.NAME, TimestampFieldMapper.NAME},\n                 true, request.version(), request.versionType(), FetchSourceContext.FETCH_SOURCE, false);\n+        return prepare(request, getResult);\n+    }\n \n+    /**\n+     * Prepares an update request by converting it into an index or delete request or an update response (no action).\n+     */\n+    @SuppressWarnings(\"unchecked\")\n+    public Result prepare(UpdateRequest request, final GetResult getResult) {\n+        long getDate = System.currentTimeMillis();\n         if (!getResult.isExists()) {\n             if (request.upsertRequest() == null && !request.docAsUpsert()) {\n-                throw new DocumentMissingException(new ShardId(indexShard.indexService().index().name(), request.shardId()), request.type(), request.id());\n+                throw new DocumentMissingException(new ShardId(request.index(), request.shardId()), request.type(), request.id());\n             }\n-            Long ttl = null;\n             IndexRequest indexRequest = request.docAsUpsert() ? request.doc() : request.upsertRequest();\n-            if (request.scriptedUpsert() && (request.script() != null)) {\n+            Long ttl = indexRequest.ttl();\n+            if (request.scriptedUpsert() && request.script() != null) {\n                 // Run the script to perform the create logic\n-                IndexRequest upsert = request.upsertRequest();               \n+                IndexRequest upsert = request.upsertRequest();\n                 Map<String, Object> upsertDoc = upsert.sourceAsMap();\n                 Map<String, Object> ctx = new HashMap<>(2);\n                 // Tell the script that this is a create and not an update\n                 ctx.put(\"op\", \"create\");\n                 ctx.put(\"_source\", upsertDoc);\n                 try {\n-                    ExecutableScript script = scriptService.executable(new Script(request.scriptLang, request.script, request.scriptType, request.scriptParams), ScriptContext.Standard.UPDATE);\n-                    script.setNextVar(\"ctx\", ctx);\n-                    script.run();\n-                    // we need to unwrap the ctx...\n-                    ctx = (Map<String, Object>) script.unwrap(ctx);\n+                    if (scriptService != null) {\n+                        ExecutableScript script = scriptService.executable(new Script(request.scriptLang, request.script, request.scriptType, request.scriptParams), ScriptContext.Standard.UPDATE);\n+                        script.setNextVar(\"ctx\", ctx);\n+                        script.run();\n+                        // we need to unwrap the ctx...\n+                        ctx = (Map<String, Object>) script.unwrap(ctx);\n+                    }\n                 } catch (Exception e) {\n                     throw new ElasticsearchIllegalArgumentException(\"failed to execute script\", e);\n-                }                \n+                }\n                 //Allow the script to set TTL using ctx._ttl\n-                ttl = getTTLFromScriptContext(ctx);\n+                if (ttl < 0) {","path":"src/main/java/org/elasticsearch/action/update/UpdateHelper.java","position":50,"original_position":50,"commit_id":"78bfef5a5fb58b78f98263f13a513405fa83c719","original_commit_id":"78bfef5a5fb58b78f98263f13a513405fa83c719","user":{"login":"s1monw","id":973334,"avatar_url":"https://avatars.githubusercontent.com/u/973334?v=3","gravatar_id":"","url":"https://api.github.com/users/s1monw","html_url":"https://github.com/s1monw","followers_url":"https://api.github.com/users/s1monw/followers","following_url":"https://api.github.com/users/s1monw/following{/other_user}","gists_url":"https://api.github.com/users/s1monw/gists{/gist_id}","starred_url":"https://api.github.com/users/s1monw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/s1monw/subscriptions","organizations_url":"https://api.github.com/users/s1monw/orgs","repos_url":"https://api.github.com/users/s1monw/repos","events_url":"https://api.github.com/users/s1monw/events{/privacy}","received_events_url":"https://api.github.com/users/s1monw/received_events","type":"User","site_admin":false},"body":"can ttl be null here?","created_at":"2015-04-29T16:10:54Z","updated_at":"2015-04-29T16:10:54Z","html_url":"https://github.com/elastic/elasticsearch/pull/8715#discussion_r29351699","pull_request_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/8715","_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments/29351699"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/8715#discussion_r29351699"},"pull_request":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/8715"}}},"pull_request":{"url":"https://api.github.com/repos/elastic/elasticsearch/pulls/8715","id":25237603,"html_url":"https://github.com/elastic/elasticsearch/pull/8715","diff_url":"https://github.com/elastic/elasticsearch/pull/8715.diff","patch_url":"https://github.com/elastic/elasticsearch/pull/8715.patch","issue_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8715","number":8715,"state":"open","locked":false,"title":"Upsert does not use ttl value","user":{"login":"dadoonet","id":274222,"avatar_url":"https://avatars.githubusercontent.com/u/274222?v=3","gravatar_id":"","url":"https://api.github.com/users/dadoonet","html_url":"https://github.com/dadoonet","followers_url":"https://api.github.com/users/dadoonet/followers","following_url":"https://api.github.com/users/dadoonet/following{/other_user}","gists_url":"https://api.github.com/users/dadoonet/gists{/gist_id}","starred_url":"https://api.github.com/users/dadoonet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dadoonet/subscriptions","organizations_url":"https://api.github.com/users/dadoonet/orgs","repos_url":"https://api.github.com/users/dadoonet/repos","events_url":"https://api.github.com/users/dadoonet/events{/privacy}","received_events_url":"https://api.github.com/users/dadoonet/received_events","type":"User","site_admin":false},"body":"When running an upsert which defines a ttl value, the ttl value is set to null and ignored.\r\n\r\nRelated to #3256#issuecomment-64963409","created_at":"2014-11-30T18:11:35Z","updated_at":"2015-04-29T16:10:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"05771c11aa784ee36c234ca07ee110552765a1d4","assignee":{"login":"dadoonet","id":274222,"avatar_url":"https://avatars.githubusercontent.com/u/274222?v=3","gravatar_id":"","url":"https://api.github.com/users/dadoonet","html_url":"https://github.com/dadoonet","followers_url":"https://api.github.com/users/dadoonet/followers","following_url":"https://api.github.com/users/dadoonet/following{/other_user}","gists_url":"https://api.github.com/users/dadoonet/gists{/gist_id}","starred_url":"https://api.github.com/users/dadoonet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dadoonet/subscriptions","organizations_url":"https://api.github.com/users/dadoonet/orgs","repos_url":"https://api.github.com/users/dadoonet/repos","events_url":"https://api.github.com/users/dadoonet/events{/privacy}","received_events_url":"https://api.github.com/users/dadoonet/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/8715/commits","review_comments_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/8715/comments","review_comment_url":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elastic/elasticsearch/issues/8715/comments","statuses_url":"https://api.github.com/repos/elastic/elasticsearch/statuses/78bfef5a5fb58b78f98263f13a513405fa83c719","head":{"label":"dadoonet:pr/upsert-with-ttl","ref":"pr/upsert-with-ttl","sha":"78bfef5a5fb58b78f98263f13a513405fa83c719","user":{"login":"dadoonet","id":274222,"avatar_url":"https://avatars.githubusercontent.com/u/274222?v=3","gravatar_id":"","url":"https://api.github.com/users/dadoonet","html_url":"https://github.com/dadoonet","followers_url":"https://api.github.com/users/dadoonet/followers","following_url":"https://api.github.com/users/dadoonet/following{/other_user}","gists_url":"https://api.github.com/users/dadoonet/gists{/gist_id}","starred_url":"https://api.github.com/users/dadoonet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dadoonet/subscriptions","organizations_url":"https://api.github.com/users/dadoonet/orgs","repos_url":"https://api.github.com/users/dadoonet/repos","events_url":"https://api.github.com/users/dadoonet/events{/privacy}","received_events_url":"https://api.github.com/users/dadoonet/received_events","type":"User","site_admin":false},"repo":{"id":2521491,"name":"elasticsearch","full_name":"dadoonet/elasticsearch","owner":{"login":"dadoonet","id":274222,"avatar_url":"https://avatars.githubusercontent.com/u/274222?v=3","gravatar_id":"","url":"https://api.github.com/users/dadoonet","html_url":"https://github.com/dadoonet","followers_url":"https://api.github.com/users/dadoonet/followers","following_url":"https://api.github.com/users/dadoonet/following{/other_user}","gists_url":"https://api.github.com/users/dadoonet/gists{/gist_id}","starred_url":"https://api.github.com/users/dadoonet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dadoonet/subscriptions","organizations_url":"https://api.github.com/users/dadoonet/orgs","repos_url":"https://api.github.com/users/dadoonet/repos","events_url":"https://api.github.com/users/dadoonet/events{/privacy}","received_events_url":"https://api.github.com/users/dadoonet/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dadoonet/elasticsearch","description":"Open Source, Distributed, RESTful Search Engine","fork":true,"url":"https://api.github.com/repos/dadoonet/elasticsearch","forks_url":"https://api.github.com/repos/dadoonet/elasticsearch/forks","keys_url":"https://api.github.com/repos/dadoonet/elasticsearch/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dadoonet/elasticsearch/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dadoonet/elasticsearch/teams","hooks_url":"https://api.github.com/repos/dadoonet/elasticsearch/hooks","issue_events_url":"https://api.github.com/repos/dadoonet/elasticsearch/issues/events{/number}","events_url":"https://api.github.com/repos/dadoonet/elasticsearch/events","assignees_url":"https://api.github.com/repos/dadoonet/elasticsearch/assignees{/user}","branches_url":"https://api.github.com/repos/dadoonet/elasticsearch/branches{/branch}","tags_url":"https://api.github.com/repos/dadoonet/elasticsearch/tags","blobs_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/refs{/sha}","trees_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dadoonet/elasticsearch/statuses/{sha}","languages_url":"https://api.github.com/repos/dadoonet/elasticsearch/languages","stargazers_url":"https://api.github.com/repos/dadoonet/elasticsearch/stargazers","contributors_url":"https://api.github.com/repos/dadoonet/elasticsearch/contributors","subscribers_url":"https://api.github.com/repos/dadoonet/elasticsearch/subscribers","subscription_url":"https://api.github.com/repos/dadoonet/elasticsearch/subscription","commits_url":"https://api.github.com/repos/dadoonet/elasticsearch/commits{/sha}","git_commits_url":"https://api.github.com/repos/dadoonet/elasticsearch/git/commits{/sha}","comments_url":"https://api.github.com/repos/dadoonet/elasticsearch/comments{/number}","issue_comment_url":"https://api.github.com/repos/dadoonet/elasticsearch/issues/comments{/number}","contents_url":"https://api.github.com/repos/dadoonet/elasticsearch/contents/{+path}","compare_url":"https://api.github.com/repos/dadoonet/elasticsearch/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dadoonet/elasticsearch/merges","archive_url":"https://api.github.com/repos/dadoonet/elasticsearch/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dadoonet/elasticsearch/downloads","issues_url":"https://api.github.com/repos/dadoonet/elasticsearch/issues{/number}","pulls_url":"https://api.github.com/repos/dadoonet/elasticsearch/pulls{/number}","milestones_url":"https://api.github.com/repos/dadoonet/elasticsearch/milestones{/number}","notifications_url":"https://api.github.com/repos/dadoonet/elasticsearch/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dadoonet/elasticsearch/labels{/name}","releases_url":"https://api.github.com/repos/dadoonet/elasticsearch/releases{/id}","created_at":"2011-10-05T19:24:28Z","updated_at":"2015-03-10T00:44:50Z","pushed_at":"2015-04-29T15:11:14Z","git_url":"git://github.com/dadoonet/elasticsearch.git","ssh_url":"git@github.com:dadoonet/elasticsearch.git","clone_url":"https://github.com/dadoonet/elasticsearch.git","svn_url":"https://github.com/dadoonet/elasticsearch","homepage":"http://elasticsearch.org","size":115829,"stargazers_count":2,"watchers_count":2,"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":2,"default_branch":"master"}},"base":{"label":"elastic:master","ref":"master","sha":"ab83eb036bdaff0c45bc21d040b538d4cb20284d","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-04-29T16:10:20Z","pushed_at":"2015-04-29T16:10:19Z","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":"http://elasticsearch.org","size":345494,"stargazers_count":10985,"watchers_count":10985,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3455,"mirror_url":null,"open_issues_count":1080,"forks":3455,"open_issues":1080,"watchers":10985,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/8715"},"html":{"href":"https://github.com/elastic/elasticsearch/pull/8715"},"issue":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/8715"},"comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/issues/8715/comments"},"review_comments":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/8715/comments"},"review_comment":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elastic/elasticsearch/pulls/8715/commits"},"statuses":{"href":"https://api.github.com/repos/elastic/elasticsearch/statuses/78bfef5a5fb58b78f98263f13a513405fa83c719"}}}}