使用Elasticsearch的Ajax查询格式 [英] Ajax query format with Elasticsearch
问题描述
我正在尝试使用AJAX向我的elasticsearch索引发出发布请求. cURL结果为:
I am trying to make a post request with AJAX to my elasticsearch index. The cURL result is:
[~]$ 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"}}]}}
浏览器结果为:这也可以正常工作.意味着已经创建了索引,并且cURL/GET可以获取结果.
Browser result is:This is also working correctly. Meaning the index has been created and cURL/ GET is able to get the result.
当我尝试执行AJAX请求时,我可能会在查询格式方面苦苦挣扎.我不知道.
When I am trying to have an AJAX request do the same, I am struggling with the query format probably. I am not able to figure out.
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:
基本上是400错误的请求错误.我无法弄清楚我的查询或创建Ajax请求的方式是否有问题.为什么会有回调问题!任何帮助,将不胜感激.我已经在网上搜索了此问题,并尝试了各种组合.
Basically it's a 400 Bad Request error. I am not able to figure out if there is something wrong with my query or the way Ajax request is being created. 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.
推荐答案
将方法更改为GET
,将dateType更改为json
.此外,querystring还需要一个q
参数.
Change the method to GET
and dateType to json
. Also the querystring requires a q
parameter.
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'
});
这篇关于使用Elasticsearch的Ajax查询格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!