【问题标题】:How order searchkick search by distance?如何按距离排序 searchkick 搜索?
【发布时间】:2019-01-25 17:48:47
【问题描述】:
@professional_services = 
  Professional::Service.search @keywords, where: {
    location: { 
      near: { lat: @cordinate[0], lon: @cordinate[1] }, 
      within: "100km"
    }
  }  

如何按距离排序?

【问题讨论】:

    标签: ruby-on-rails elasticsearch searchkick


    【解决方案1】:

    我使用查询按距离对结果进行排序:

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

    【讨论】:

      【解决方案2】:

      如果你只想按距离排序可以使用下面的代码 sn -p

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

      【讨论】:

        猜你喜欢
        • 2017-07-22
        • 2012-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-11
        相关资源
        最近更新 更多