简体   繁体   English

使用Elasticsearch的Ajax查询格式

[英]Ajax query format with Elasticsearch

I am trying to make a post request with AJAX to my elasticsearch index. 我正在尝试使用AJAX向我的elasticsearch索引发出发布请求。 The cURL result is: cURL结果为:

[~]$ curl -XGET 'http://localhost:9200/firebase/_search?q=song:i%20am%20in'

{"took":172,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.82749283,"hits":[{"_index":"firebase","_type":"song","_id":"0001","_score":0.82749283,"_source":{"song":"i am in california","song_name":"hello","song_url":"https://s3.ap-south-1.amazonaws.com/songapp-dump/media/songs/Adele_-_Hello-_i_am_in_california.mp3"}},{"_index":"firebase","_type":"song","_id":"0002","_score":0.28582606,"_source":{"song":"i must have called a thousand times","song_name":"hello","song_url":"https://s3.ap-south-1.amazonaws.com/songapp-dump/media/songs/Adele_-_Hello-_i_must_have_called_a_thousand_times.mp3"}}]}} 

Browser result is: 浏览器结果是: 在此处输入图片说明 This is also working correctly. 这也可以正常工作。 Meaning the index has been created and cURL/ GET is able to get the result. 意味着已经创建了索引,并且cURL / GET可以获取结果。

When I am trying to have an AJAX request do the same, I am struggling with the query format probably. 当我试图让AJAX请求执行相同操作时,我可能正在努力查询格式。 I am not able to figure out. 我不知道。

Ajax.js Ajax.js

$(function() {
    $('#message').keyup(function() {
        // console.log(JSON.stringify());
        var data = {
                'song': $('#message').val()
            };
        console.log(JSON.stringify(data));
        $.ajax({
            type: "POST",
            url: "http://localhost:9200/firebase/_search",
            contentType: 'application/json',
            // data: {
            //     'q': $('#message').val()
            // },
            data: JSON.stringify(data),
            success: searchSuccess,
            dataType: 'jsonp'
        });

    });

});

The console logs the following error: 控制台记录以下错误: 在此处输入图片说明

Basically it's a 400 Bad Request error. 基本上是400错误的请求错误。 I am not able to figure out if there is something wrong with my query or the way Ajax request is being created. 我无法弄清楚我的查询或创建Ajax请求的方式是否有问题。 Why am I having callback issues! 为什么会有回调问题! Any help would be appreciated. 任何帮助,将不胜感激。 I have scoured the web on this issue and have tried various combinations as well. 我已经在网上搜索了这个问题,并尝试了各种组合。

Change the method to GET and dateType to json . 将方法更改为GET ,将dateType更改为json Also the querystring requires a q parameter. 此外,querystring还需要q参数。

     var data = {
         'q': 'song:' + $('#message').val()
     };

     $.ajax({
        type: "GET",
        url: "http://localhost:9200/firebase/_search",
        contentType: 'application/json',
        data: JSON.stringify(data),
        success: searchSuccess,
        dataType: 'json'
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM