假设我们在ElasticSearch索引中有一堆文档.每个文档在数组中都有多个位置,如下所示:
{
"name": "foobar",
"locations": [
{
"lat": 40.708519,
"lon": -74.003212
},
{
"lat": 39.752609,
"lon": -104.998100
},
{
"lat": 51.506321,
"lon": -0.127140
}
]
}
Run Code Online (Sandbox Code Playgroud)
该
geo_distance过滤器可以用每份文件的多个位置/点工作.一旦单个位置/点与过滤器匹配,文档将包含在过滤器中.
那么,是否可以创建一个地理距离过滤器来检查阵列中的所有位置?
不幸的是,这似乎不起作用:
{
"filter": {
"geo_distance": {
"distance": "100 km",
"locations": "40, -105"
}
}
}
Run Code Online (Sandbox Code Playgroud)
抛出" QueryParsingException[[myIndex] failed to find geo_point field [locations]"既然locations不是一个单一的,geo_point而是一个geo_points 的数组.
Tho*_*ten 28
您是否为文档指定了geo_point映射?
curl -XDELETE 'http://localhost:9200/twitter/'
curl -XPUT 'http://localhost:9200/twitter/'
curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d '
{
"tweet" : {
"properties" : {
"locations" : {"type" : "geo_point"}
}
}
}'
curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '
{
"user": "kimchy",
"postDate": "2009-11-15T13:12:00",
"message": "Trying out Elastic Search, so far so good?",
"locations" : [{
"lat" : 50.00,
"lon" : 10.00
},
{
"lat" : 40.00,
"lon" : 9.00
}]
}'
curl -XPUT 'http://localhost:9200/twitter/tweet/2' -d '
{
"user": "kimchy",
"postDate": "2009-11-15T13:12:00",
"message": "Trying out Elastic Search, so far so good?",
"locations" : [{
"lat" : 30.00,
"lon" : 8.00
},
{
"lat" : 20.00,
"lon" : 7.00
}]
}'
curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
"query": {
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "20km",
"tweet.locations" : {
"lat" : 40.00,
"lon" : 9.00
}
}
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
小智 5
对于 Elasticsearch 5.1 版本,对于上述相同的索引,查询将像这样,
curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "200km",
"locations": {
"lat": 40.00,
"lon": 9.00
}
}
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10812 次 |
| 最近记录: |