Léo Rocha
Léo Rocha

Reputation: 324

How order searchkick search by distance?

@professional_services = 
  Professional::Service.search @keywords, where: {
    location: { 
      near: { lat: @cordinate[0], lon: @cordinate[1] }, 
      within: "100km"
    }
  }  

How order this search by distance?

Upvotes: 2

Views: 904

Answers (2)

Asad
Asad

Reputation: 41

If you just want to sort by distance can use below code snippet

@professional_services =  
Professional::Service.search @keywords, order: {
    _geo_distance: {
        location: "#{@cordinate[0]}, #{@cordinate[1]}",
        order: "asc"
    }
}

Upvotes: 2

Léo Rocha
Léo Rocha

Reputation: 324

I sort results by distance with the query:

@professional_services =  
Professional::Service.search "*", where: {
  location: {
    near: {lat: @cordinate[0], lon: @cordinate[1]}, within: "10000km"
  }
}, 
order: [{
  _score: :desc}, {_geo_distance: {location: "#{@cordinate[0]},  
  #{@cordinate[1]}", order: "asc",unit: "km"}
}]

Upvotes: 2

Related Questions