title: How to generate a UUID in Elasticsearch reindex painless script
body: <p>I am trying to use the reindex api to create a copy of a set of documents. One of the fields (<code>uuid</code>) of the document is a UUID. I need the copied documents to have new UUIDs for the <code>uuid</code> field.</p>
  
  <p>According to [1] and [2] the method <code>java.util.UUID.randomUUID()</code> is not whitelisted for use in painless scripts.</p>
  
  <p><em>Questions:</em></p>
  
  <p>1) How can I generate a UUID in painless?</p>
  
  <p>2) Why is <code>UUID.randomUUID()</code> considered an unsafe operation? Or is it just an oversight that it is not whitelisted?</p>
  
  <p>3) How can I whitelist <code>UUID.randomUUID()</code> in the <code>"reindex"</code> context? I tried to build my own elasticsearch painless extension/plugin to do this based on the example in [3]. The problem is it only works for the <code>"SearchScript"</code> context. There does not seem to be an equivalent <code>"ReindexContext"</code>.</p>
  
  <p>Here is a simplified version of what I am trying:</p>
  
  <pre><code>curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
  {
    "source": {
      "index": "product1"
    },
    "dest": {
      "index": "product2"
    },
    "script": {
      "source": "ctx._source.uuid = java.util.UUID.randomUUID().toString()",
      "lang": "painless"
    }
  }
  '
  </code></pre>
  
  <p>Which produces the following error:</p>
  
  <pre><code>{
    "error" : {
      "root_cause" : [
        {
          "type" : "script_exception",
          "reason" : "compile error",
          "script_stack" : [
            "... rce.uuid = java.util.UUID.randomUUID().toString()",
            "                             ^---- HERE"
          ],
          "script" : "ctx._source.uuid = java.util.UUID.randomUUID().toString()",
          "lang" : "painless"
        }
      ],
      "type" : "script_exception",
      "reason" : "compile error",
      "script_stack" : [
        "... rce.uuid = java.util.UUID.randomUUID().toString()",
        "                             ^---- HERE"
      ],
      "script" : "ctx._source.uuid = java.util.UUID.randomUUID().toString()",
      "lang" : "painless",
      "caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "method [java.util.UUID, randomUUID/0] not found"
      }
    },
    "status" : 500
  }
  </code></pre>
  
  <p>I know my approach is valid and that the above is a painless whitelisting issue because when I try a different method (<code>fromString()</code>) I get no errors:</p>
  
  <pre><code>curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
  {
    "source": {
      "index": "product1"
    },
    "dest": {
      "index": "product2"
    },
    "script": {
      "source": "ctx._source.uuid = java.util.UUID.fromString(\u0027ad139caa-5b54-4179-b812-5015daecad1e\u0027).toString()",
      "lang": "painless"
    }
  }
  '
  </code></pre>
  
  <hr>
  
  <p><em>References:</em></p>
  
  <p>[1] - <a href="https://discuss.elastic.co/t/generate-a-uuid-using-randomuuid-in-painless/144354/3" rel="nofollow noreferrer">https://discuss.elastic.co/t/generate-a-uuid-using-randomuuid-in-painless/144354/3</a></p>
  
  <p>[2] - <a href="https://www.elastic.co/guide/en/elasticsearch/painless/6.6/painless-api-reference.html" rel="nofollow noreferrer">https://www.elastic.co/guide/en/elasticsearch/painless/6.6/painless-api-reference.html</a></p>
  
  <p>[3] - <a href="https://github.com/elastic/elasticsearch/tree/v6.6.0/plugins/examples/painless-whitelist" rel="nofollow noreferrer">https://github.com/elastic/elasticsearch/tree/v6.6.0/plugins/examples/painless-whitelist</a></p>
  
  <p><em>Other Notes:</em></p>
  
  <ul>
  <li><p>Elasticsearch 6.6</p></li>
  <li><p>I've already asked this question on the elasticsearch forum here (no responses):
  <a href="https://discuss.elastic.co/t/need-to-generate-uuid-in-painless-script/165318" rel="nofollow noreferrer">https://discuss.elastic.co/t/need-to-generate-uuid-in-painless-script/165318</a></p></li>
  <li><p>I've also asked about whitelisting the <code>UUID.randomUUID()</code> method here (no responses):
  <a href="https://discuss.elastic.co/t/question-about-painless-whitelist/165523" rel="nofollow noreferrer">https://discuss.elastic.co/t/question-about-painless-whitelist/165523</a> </p></li>
  </ul>