elasticsearch ajax,Elasticsearch query with jQuery/Ajax

齐志勇
2023-12-01

Today I have a problem with ElasticSearch and jQuery. I've searched for solution, but I found no solution.

I want to get some data from ElasticSearch using jQuery. It works when I use curl instead:

curl -XGET "http://localhost:9200/locations/_search?pretty=true" -d '

{

"sort": [

"_score",

{

"_geo_distance": {

"location": {

"lat" : 40.715,

"lon": -73.998

},

"order": "asc",

"unit": "km",

"distance_type": "plane"

}

}

]

}'

but it doesn't work when I use jQuery. I tried:

var data = {

"query": {

"filtered": {

"filter": {

"geo_distance": {

"distance": "1km",

"location": {

"lat" : 40.715,

"lon": -73.998

}

}

}

}

}

}

$.ajax({

method: "GET",

url: "http://localhost:9200/locations/_search?pretty=true",

crossDomain: true,

async: false,

data: data,

dataType : 'json',

contentType: 'application/json',

})

.done(function( data ) {

console.log(data);

})

.fail(function( data ) {

console.log(data);

});

I tried to pass data with JSON.stringify(data) and it also doesn't work. I even tried to change method from "GET" to "POST" and no results. Well, it's working. ElasticSearch returns a response with some locations, but my locations are far far away from location I requested. 40.715, -73.998 location is New York and when I use curl it returns locations in New York, but when I use jQuery I get locations in Mexico so I think my "data" variable is ignored. I mentioned I tried to use json.Stringify. Yes, but it returns error:

Object { readyState: 0, getResponseHeader: .ajax/y.getResponseHeader(), getAllResponseHeaders: .ajax/y.getAllResponseHeaders(), setRequestHeader: .ajax/y.setRequestHeader(), overrideMimeType: .ajax/y.overrideMimeType(), statusCode: .ajax/y.statusCode(), abort: .ajax/y.abort(), state: .Deferred/e.state(), always: .Deferred/e.always(), catch: .Deferred/e.catch(),… }

So, how to make it work?

 类似资料: