Matt
Matt

Reputation: 5595

Geo sorting and distance calculation in ElasticSearch not working

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?

Upvotes: 2

Views: 2345

Answers (2)

progrrammer
progrrammer

Reputation: 4489

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.

Upvotes: 5

Luke Willis
Luke Willis

Reputation: 8580

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.

Upvotes: 0

Related Questions