簡體   English   中英

使用Elasticsearch Search調用AJAX

[英]AJAX Call with Elasticsearch Search

我一直試圖弄清楚如何使用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

它可以幫助您解決您的問題。

我建議您使用名為Postman的工具,而不是編寫AJAX調用。 郵差有一個簡單的座右銘 -

使API開發變得簡單

因此,如果你在編寫ES請求時遇到問題,或者你決定不使用jQuery AJAX / XHR,也許你想使用cURL / Unirest / NSURL,那么你可以使用Postman請求構建器來編寫在你的簡單Http請求下,然后你會在下面的框中找到一個名為code的鏈接,你可以用你選擇的語言生成請求。 包括AJAX,是的。 所以我建議你嘗試使用它。

這是您可以從https://www.getpostman.com/postman下載Postman的鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM