我一直在尝试找出如何使用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);
            }
        });

最佳答案

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

它可以帮助您解决问题。

09-17 07:13