[英]AJAX Call with Elasticsearch Search
I've been trying to figure out how to correctly request data from elasticsearch using a jQuery AJAX call. 我一直试图弄清楚如何使用jQuery AJAX调用正确地从elasticsearch请求数据。 I either get an parsing error or I'll get every document in the index I'm searching under.
我要么得到一个解析错误,要么我将得到我正在搜索的索引中的每个文档。
$(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);
});
});
Here is my error: 这是我的错误:
{
"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
}
EDIT: I figured it out: 编辑:我想通了:
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);
}
});
You can have a look here: https://github.com/dadoonet/devoxxfr_demo/blob/gh-pages/index.html#L512 你可以看看这里: https : //github.com/dadoonet/devoxxfr_demo/blob/gh-pages/index.html#L512
It could help you to solve your issue. 它可以帮助您解决您的问题。
Instead of writing your AJAX call, I'd suggest you use this tool called Postman . 我建议您使用名为Postman的工具,而不是编写AJAX调用。 Postman has a simple motto -
邮差有一个简单的座右铭 -
Making API development easy
使API开发变得简单
So in case you have trouble writing your ES requests, or maybe you decide not to use jQuery AJAX/XHR, and maybe you'd like to use cURL/Unirest/NSURL and what not, you can just use the Postman request builder to write down your simple Http Request, then you'll find a link called code in the box right below that, and you can generate requests in the language of your choice using that. 因此,如果你在编写ES请求时遇到问题,或者你决定不使用jQuery AJAX / XHR,也许你想使用cURL / Unirest / NSURL,那么你可以使用Postman请求构建器来编写在你的简单Http请求下,然后你会在下面的框中找到一个名为code的链接,你可以用你选择的语言生成请求。 Including AJAX, yes.
包括AJAX,是的。 So I'd recommend you try using that.
所以我建议你尝试使用它。
Here's the link where you can download Postman from - https://www.getpostman.com/postman 这是您可以从https://www.getpostman.com/postman下载Postman的链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.