简体   繁体   中英

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?

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"
    }
}

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"}
}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM