Reindexing from HTML

Hey folks,

I'm trying to make a simple reindex request from a HTML button, but keep getting a ElasticsearchParseException. Can someone give me a hand on this?

This is the HTML I'm using:

<html>
<head>
    <script type="text/javascript"
            src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
            $('#button1').click(function() {
                var tabWindowId = window.open('about:blank', '_blank'); 
                var form = tabWindowId.document.createElement("form");
                
                form.setAttribute("method", "POST");
                form.setAttribute("action", "http://localhost:9200/_reindex");
		       
		        var hiddenField = document.createElement("input");
            	hiddenField.setAttribute("type", "hidden");
              	hiddenField.setAttribute("name", "source");
                hiddenField.setAttribute("value", "\"source\":{\"index\":\"log-*\",\"query\":{\"term\":{\"requestId\":\"reallyniceanduniqueid123\"}}},\"dest\":{\"index\":\"log-permanentIndex\",\"version_type:external\"}");

		        form.appendChild(hiddenField);

                tabWindowId.document.body.appendChild(form);
                form.submit();
            });
    </script>
</head>
<body>
<form enctype="multipart/form-data">
  <input id="button1" type="button" value="Create Permalink" />
</form>
</body>
</html>

and this is the stack trace from elasticsearch:

ElasticsearchParseException[Failed to derive xcontent]
at org.elasticsearch.common.xcontent.XContentFactory.xContent(XContentFactory.java:240)
at org.elasticsearch.index.reindex.RestReindexAction.handleRequest(RestReindexAction.java:67)
at org.elasticsearch.rest.BaseRestHandler.handleRequest(BaseRestHandler.java:54)
at org.elasticsearch.rest.RestController.executeHandler(RestController.java:205)
at org.elasticsearch.rest.RestController.dispatchRequest(RestController.java:166)
at org.elasticsearch.http.HttpServer.internalDispatchRequest(HttpServer.java:128)
at org.elasticsearch.http.HttpServer$Dispatcher.dispatchRequest(HttpServer.java:86)
at org.elasticsearch.http.netty.NettyHttpServerTransport.dispatchRequest(NettyHttpServerTransport.java:449)
at org.elasticsearch.http.netty.HttpRequestHandler.messageReceived(HttpRequestHandler.java:61)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.elasticsearch.http.netty.pipelining.HttpPipeliningHandler.messageReceived(HttpPipeliningHandler.java:60)
at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.jboss.netty.handler.codec.http.HttpChunkAggregator.messageReceived(HttpChunkAggregator.java:145)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
at org.jboss.netty.handler.codec.http.HttpContentDecoder.messageReceived(HttpContentDecoder.java:108)
at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)
at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)
at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)

Looks like unbalanced parenthesis but it is hard to tell.

I suggest dumping the request and having a look. Elasticearch doesn't support form encoded data and needs the body of the request to be a JSON object.

Is there a way to see that on Elasticsearch's log? I have set DEBUG level at logging.yml, but I can't see the request.

The easiest way is to grab it from your browser's debug tools.

Maybe because request body json is missing the initial "{"?

hiddenField.setAttribute("value", "\"source\":{\"index\":\"log-*\",\"query\":{\"term\":{\"requestId\":\"reallyniceanduniqueid123\"}}},\"dest\":{\"index\":\"log-permanentIndex\",\"version_type:external\"}");

Tried that, does not work as well. I've also tried this:

var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "source");
hiddenField.setAttribute("value", "{"source":{"index":"log-*","query":{"term":{"requestId":"reallyniceanduniqueid123"}}}}");

var hiddenField2 = document.createElement("input");
hiddenField2.setAttribute("type", "hidden");
hiddenField2.setAttribute("name", "dest");
hiddenField2.setAttribute("value", "{"dest":{"index":"log-permanentIndex","version_type:external"}}");

and combinations with or without the "{" and using "JSON.stringify" and "form.setAttribute("enctype", "application/json")", no success so far.

Hello mbagatini,

Doing a quick research, it seems that browsers does not supports submitting forms with application/json encoding. So, instead of struggling to make it work, why don't you try submitting through ajax (i.e. jQuery's $.ajax). If you follow this path, remember to enable CORS in elasticsearch (https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-http.html)

Cheers!