2

UPDATE I've also updated the mapping to include pin as the examples seem to suggest. Also, here's an temporary instance with some data to work with: https://21991e47cdc7caa8000.qbox.io/profiles/lead/_search

I've followed the instructions by ElasticSearch. Using this request:

$.ajax({
        url: 'https://21991e47cdc7caa8000.qbox.io/profiles/lead/_search', 
        method: 'GET', 
        data: {
            sort: [{
                _geo_distance: {
                    "pin.location": [49.8998, -97.1375], 
                    order: "asc", 
                    unit: "km"
                }
            }]
        }, 
        success: function(response) {
            console.log(response);
        }
    });

Does not return with calcuated distance or sorted by distance. I've also tried using "location".

Here's my mapping: https://21991e47cdc7caa8000.qbox.io/profiles/lead/_mapping

Any ideas?

Matt
  • 5,547
  • 23
  • 82
  • 121

2 Answers2

5

I've managed to get it working, Please see the difference,

I converted data as json before quering, and added some configuration( changed dataType to json, and request type to POST, as we know GET request generally don't have body, only POST request does.

var request = {
    sort: [{
        _geo_distance: {
            "pin.location": [
            49.8998, -97.1375],
            order: "asc",
            unit: "km"
        }
    }]
};


$.ajax({
    url: 'https://21991e47cdc7caa8000.qbox.io/profiles/lead/_search',
    type: 'POST',
    crossDomain: true,
    dataType: 'json',
    data: JSON.stringify(request),
    success: function (response) {
        console.log(JSON.stringify(response));

    }
});

Hope this helps.

I've tested, and it should work for you too.

progrrammer
  • 4,475
  • 2
  • 30
  • 38
0

The example given uses pin.location, not location or lead.location.

Additionally, pin.location is an array of length 2, not an object with two fields as you are using it. This seems pretty counter-intuitive to me, as the location field in most other api calls is an object like you are using.

Try this:

$.ajax({
    url: "https://myhostedes.com/profiles/lead/_search", 
    method: "GET", 
    data: {
        "sort": [{
            "_geo_distance": {
                "pin.location": [49.8998, -97.1375], 
                "order": "asc", 
                "unit": "km"
            }
        }]
    }, 
    success: function(response) {
        console.log(response);
    }
});

Note: I don't have access to an elastisearch instance at the moment, so I haven't been able to run this yet.

Luke Willis
  • 8,429
  • 4
  • 46
  • 79
  • Doesn't work. I wonder if I actually need a pin property on the entity? Right now I just have location... Assumed pin was the example entity they were using--this from their docs: "For example, lets take an example of a “pin” that we want to index its location and maybe some tags its associated with". Mapping here all shows "pin" as well though: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-geo-point-type.html – Matt Aug 07 '14 at 22:52
  • I've included a link to an ES server where you can try this out. If you can get a request to work there it should work for me as well. – Matt Aug 08 '14 at 00:08
  • Looks like it should work: http://www.elasticsearchtutorial.com/spatial-search-tutorial.html – Dmitry Polushkin Aug 08 '14 at 01:02
  • I agree... Does the request on my server work for you? – Matt Aug 08 '14 at 01:13