[英]Using elastic4s for GeoDistance queries
我在理解用於形成GeoDistance查詢的DSL語法時遇到了一些麻煩(使所有條目都離點有一定距離);
當前,在id = 1
ES索引"places" -> "shops"
,我有一個示例條目;
{
_index: "places",
_type: "shops",
_id: "1",
_version: 5,
found: true,
_source: {
location: {
lat: 50,
lon: 50
},
name: "coffee store",
posted: "2014-09-12T08:53:01.673Z"
}
}
然后,使用Elastic4s DSL建立一個搜索查詢。
val nearbyStoresFuture:Future[SearchResponse] = client execute {
search in "places" -> "shops" filter {
geoDistance("location") point(lat, lon) distance(distance)
}
}
nearbyStoresFuture onComplete {
case Success(s) => {
s
client.close
}
case Failure(t) => {
t
client.close
}
}
在這種情況下:
lat: Double = 50.0
lon: Double = 50.0
distance: String = "50km"
因此,無論如何,如果查詢是正確的,這應該為我提供包含ES中條目的結果,因為它與條目在同一點,因此在50公里之內。
查詢成功,但結果為空。
編輯:這是映射的樣子;
ES.execute {
create index "places" mappings(
"shops" as (
"location" typed GeoPointType,
"name" typed StringType,
"posted" typed DateType
)
)
}
這怎么了
這不是一個elastic4s問題,而是一個通用的elasticsearch問題:tl; dr您沒有索引任何字段。
對數據建立索引時,可以指定要為哪些字段建立索引,然后將這些字段用於搜索。 另外,您可以存儲一個文檔,當搜索與該文檔匹配時可以獲取該文檔。 如果您希望能夠返回原始文檔,但希望在建立索引時使用字段的子集或派生的字段,這將很有用。
在elastic4s中
index into "myindex"->"mytype" fields ("name"->name, "location"->latlong) source srcdoc
http://www.elasticsearch.org/guide/zh-CN/elasticsearch/reference/current/mapping-source-field.html http://www.elasticsearch.org/guide/zh-CN/elasticsearch/guide/current/index-doc html的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.