elastic4s 地理距离排序查询语法

elastic4s geodistance sort query syntax

我正在使用 elastic4s 版本 1.6.2,需要编写一个查询来搜索给定的地理位置,按距离和 returns 距离对结果进行排序。 我可以使用 curl 中的 get request 来做到这一点,但努力寻找正确的语法和示例进行地理定位排序。

case class Store(id: Int, name: String, number: Int, building: Option[String] =     None, street: String, postCode: String,county: String, geoLocation: GeoLocation)

case class GeoLocation(lat: Double, lon: Double) 

val createMappings = client.execute {
create index "stores" mappings (
  "store" as(
    "store" typed StringType,
    "geoLocation" typed GeoPointType
    )
  )
}

def searchByGeoLocation(geoLocation: GeoLocation) = {
client.execute {
  search in "stores" -> "store" postFilter {
    geoDistance("geoLocation") point(geoLocation.lat, geoLocation.lon) distance(2, KILOMETERS)
  }


}
}

有人知道如何从地理位置添加按距离排序并获取距离吗 以下 curl 命令按预期工作

curl -XGET 'http://localhost:9200/stores/store/_search?pretty=true' -d '
{
"sort" : [
  {
      "_geo_distance" : {
          "geoLocation" : {
                "lat" : 52.0333793839746,
                "lon" : -0.768937531935448
          }, 
          "order" : "asc",
          "unit" : "km"
      }
  }
],
"query": {
"filtered" : {
    "query" : {
        "match_all" : {}
    },
    "filter" : {
        "geo_distance" : {
            "distance" : "20km",
            "geoLocation" : {
                "lat" : 52.0333793839746,
                "lon" : -0.768937531935448
            }
        }
    }
}

} }'

不是 elasti4s 方面的专家,但此查询应该等同于您的 curl 命令:

val geoLocation = GeoLocation(52.0333793839746, -0.768937531935448)
search in "stores" -> "store" query {
    filteredQuery filter {
      geoDistance("geoLocation") point(geoLocation.lat, geoLocation.lon) distance(20, KILOMETERS)
    }
  } sort {
    geoSort("geoLocation") point (geoLocation.lat, geoLocation.lon) order SortOrder.ASC
  }

它打印以下查询:

{
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : { }
      },
      "filter" : {
        "geo_distance" : {
          "geoLocation" : [ -0.768937531935448, 52.0333793839746 ],
          "distance" : "20.0km"
        }
      }
    }
  },
  "sort" : [ {
    "_geo_distance" : {
      "geoLocation" : [ {
        "lat" : 52.0333793839746,
        "lon" : -0.768937531935448
      } ]
    }
  } ]
}

asc 是排序的默认值,因此您可以删除 order SortOrder.ASC。只是想在此示例中明确说明。