Próbowałem dowiedzieć się, jak poprawnie żądania danych z elasticsearch za pomocą wywołania jQuery AJAX. Otrzymuję błąd parsowania lub każdy dokument znajduję się w indeksie, którego szukam.Wywołanie AJAX z wyszukiwaniem Elasticsearch
$(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 > 0) {
for (var i = 0; i < data.length; i++) {
source = data[i].fields;
doc_ids.push(source._id);
content = content + ' ' + source._id + '<br />';
}
$('#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);
});
});
Oto mój błąd:
{
"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 [email protected]9bb]; }{[GUiivW0TQVSNv2HQyxu8Vw][dicoms][3]: SearchParseException[[dicoms][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from [email protected]9bb]; }{[GUiivW0TQVSNv2HQyxu8Vw][dicoms][1]: SearchParseException[[dicoms][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from [email protected]9bb]; }{[GUiivW0TQVSNv2HQyxu8Vw][dicoms][4]: SearchParseException[[dicoms][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from [email protected]9bb]; }{[GUiivW0TQVSNv2HQyxu8Vw][dicoms][2]: SearchParseException[[dicoms][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [_na_]]]; nested: ElasticSearchParseException[Failed to derive xcontent from [email protected]9bb]; }]",
"status":500
}
EDIT: I zdobione:
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 > 0) {
for (var i = 0; i < data.length; i++) {
source = data[i].fields;
doc_ids.push(source._id);
content = content + ' ' + source._id + '<br />';
}
$('#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 + ' --<br />' + jso.error);
}
});
Kluczową różnicą między oryginalnego kodu i edytowanego kodu jest to, że przekształcone JavaScript obiektu do JSON i podał, że Into the $ .ajax() 's właściwość "data", wykonując JSON.stringify (dane). Uważam, że to był klucz do tego, aby to zadziałało. –
Czy mógłbyś gdzieś umieścić swoją stronę html? –