2

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?

1 Answer 1

3

Okay, problem resolved. My code is okay. I changed method to "POST". Using Google Chrome Developer Tools I noticed that the other part of code wasn't good. In my code

"location": { 
   "lat" :  40.715,
   "lon": -73.998
}

lat and lon was a variables (I changed them to coords for StackOverflow question) wchich was empty, because the other part of my code couldn't assign a new value from getCurrentPosition() (default value was 0, that's why my code didn't work). So, correct version of the code is:

var data = {
  "query": {
    "filtered": {
      "filter": {
        "geo_distance": {
          "distance": "1km", 
          "location": { 
            "lat" :  lat,
            "lon": lon
          }
        }
      }
    }
  }
}

$.ajax({
  method: "GET",
  url: "http://localhost:9200/locations/_search?pretty=true",
  crossDomain: true,  
  async: false,
  data: JSON.stringify(data),
  dataType : 'json',
  contentType: 'application/json',
})
.done(function( data ) {
  console.log(data);
})
.fail(function( data ) {
  console.log(data);
});

Notice that lat and lon are variables which need parameters to be a geopoint

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.