title: AJAX Call with Elasticsearch Search
body: <p>I've been trying to figure out how to correctly request data from elasticsearch using a jQuery AJAX call.  I either get an parsing error or I'll get every document in the index I'm searching under.</p>
  
  <pre><code>    $(document).ready(function() {
  
      var timer = null;
      function dicom_search() {
          var box = $('#s_box').val();
  
          $.ajax({
              url: 'http://localhost:9200/dicoms/dicoms/_search',
              type: 'POST',
              //contentType: 'application/json; charset=UTF-8',
              crossDomain: true,
              dataType: 'json',
              data: {
                  query:{match:{_all:$('#s_box').val()}},
                  pretty: true,
                  fields: '_id'
              },
              success: function(response) {
                  var data = response.hits.hits;
                  var doc_ids = [];
                  var source = null;
                  var content = '';
  
                  if (data.length &gt; 0) {
                      for (var i = 0; i &lt; data.length; i++) {
                          source = data[i].fields;
                          doc_ids.push(source._id);
                          content = content + ' ' + source._id + '&lt;br /&gt;';
                      }
  
                      $('#res').removeClass('text-error').addClass('text-success').html(content);
                  } else {
                      $('#res').removeClass('text-success').addClass('text-error').html('No results found.');
                  }
  
  
              }
          });
      }
  
      $('#s_box').live('keyup', function() {
  
          if (timer) {
              clearTimeout(timer);
          }
          timer = setTimeout(dicom_search, 600);
  
      });
  });
  </code></pre>
  
  <p>Here is my error:</p>
  
  <pre><code>{
     "error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[GUiivW0TQVSNv2HQyxu8Vw][dicoms][0]: SearchParseException[[dicoms][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from org.elasticsearch.common.bytes.ChannelBufferBytesReference@779479bb]; }{[GUiivW0TQVSNv2HQyxu8Vw][dicoms][3]: SearchParseException[[dicoms][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from org.elasticsearch.common.bytes.ChannelBufferBytesReference@779479bb]; }{[GUiivW0TQVSNv2HQyxu8Vw][dicoms][1]: SearchParseException[[dicoms][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from org.elasticsearch.common.bytes.ChannelBufferBytesReference@779479bb]; }{[GUiivW0TQVSNv2HQyxu8Vw][dicoms][4]: SearchParseException[[dicoms][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from org.elasticsearch.common.bytes.ChannelBufferBytesReference@779479bb]; }{[GUiivW0TQVSNv2HQyxu8Vw][dicoms][2]: SearchParseException[[dicoms][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from org.elasticsearch.common.bytes.ChannelBufferBytesReference@779479bb]; }]",
     "status":500
  }
  </code></pre>
  
  <p>EDIT: I figured it out: </p>
  
  <pre><code>var data = {
              query: {
                  match: {
                      _all: $('#s_box').val()
                  }
              },
              fields: '_id'
          }; 
  
  $.ajax({
              url: 'http://localhost:9200/dicoms/dicoms/_search',
              type: 'POST',
              //contentType: 'application/json; charset=UTF-8',
              crossDomain: true,
              dataType: 'json',
              data: JSON.stringify(data),
              success: function(response) {
                  var data = response.hits.hits;
                  var doc_ids = [];
                  var source = null;
                  var content = '';
  
                  if (data.length &gt; 0) {
                      for (var i = 0; i &lt; data.length; i++) {
                          source = data[i].fields;
                          doc_ids.push(source._id);
                          content = content + ' ' + source._id + '&lt;br /&gt;';
                      }
  
                      $('#res').removeClass('text-error').addClass('text-success').html(content);
                  } else {
                      $('#res').removeClass('text-success').addClass('text-error').html('No results found.');
                  }
  
  
              },
              error: function(jqXHR, textStatus, errorThrown) {
                  var jso = jQuery.parseJSON(jqXHR.responseText);
                  error_note('section', 'error', '(' + jqXHR.status + ') ' + errorThrown + ' --&lt;br /&gt;' + jso.error);
              }
          });
  </code></pre>