使用Elasticsearch Search调用AJAX

时间:2012-10-30 17:47:22

标签: javascript jquery ajax couchdb elasticsearch

我一直试图弄清楚如何使用jQuery AJAX调用正确地从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);

    });
});

这是我的错误:

{
   "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
}
编辑:我明白了:

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);
            }
        });

2 个答案:

答案 0 :(得分:1)

您可以在此处查看:https://github.com/dadoonet/devoxxfr_demo/blob/gh-pages/index.html#L512

它可以帮助您解决问题。

答案 1 :(得分:0)

我建议您使用名为 Postman 的工具,而不是编写您的AJAX电话。邮差有一个简单的座右铭 -

  

简化API开发

因此,如果您在编写ES请求时遇到问题,或者您决定不使用jQuery AJAX / XHR,并且您可能希望使用cURL / Unirest / NSURL,那么您可以使用邮递员请求构建器记下你的简单Http请求,然后你会在下面的框中找到一个名为 code 的链接,你可以用你选择的语言生成请求。包括AJAX,是的。所以我建议您尝试使用它。

以下是您可以从 - https://www.getpostman.com/postman

下载邮递员的链接