2017-05-10 39 views
1

對不起noob問題...我在Elasticsearch 2.3中有餐廳對象,每個對象都有一個GeoPoint和一個送貨上門距離偏好。在僞Elasticsearch geosearch with distance preference

restaurant: { 
    location: (x, y) 
    deliveryPreference: 10km 
} 

和用戶:

user { 
    location: (a,b) 
} 

我怎麼會發出尋找所有的餐館,可以在他的領域裏提供用戶的搜索?

回答

0

的解決方案包括使用geo_shapes指數restraunts文件。您需要到餐廳文檔型號如下:

PUT restaurants 
{ 
    "mappings": { 
     "restaurant": { 
      "properties": { 
       "name": { 
        "type": "text" 
       }, 
       "location": { 
        "type": "geo_point" 
       }, 
       "delivery_area": { 
        "type": "geo_shape", 
        "tree": "quadtree", 
        "precision": "1m" 
       } 
      } 
     } 
    } 
} 

然後,您可以索引你的餐館如下:

POST /restaurants/restaurant/1 
{ 
    "name": "My Food place", 
    "location": [-45.0, 45.0],   <-- lon, lat !!! 
    "delivery_area": { 
     "type": "circle", 
     "coordinates" : [-45.0, 45.0], <-- lon, lat !!! 
     "radius" : "10km" 
    } 
} 

每家餐廳都將因此與集中在它的位置,並用圓圈形狀有關適當的半徑。

最後,當用戶想要知道哪些餐廳可在她目前的位置交付,您可以發出以下geo_shape query

POST /restaurants/_search 
{ 
    "query":{ 
     "bool": { 
      "filter": { 
       "geo_shape": { 
        "delivery_area": { 
         "shape": { 
          "type": "point", 
          "coordinates" : [<user_lon>, <user_lat>] 
         }, 
         "relation": "contains" 
        } 
       } 
      } 
     } 
    } 
} 

在此查詢中,我們檢索的餐館,其delivery_area形狀包含該用戶當前位於point

0

與以下映射

{ 
    "mappings": { 
     "type_name": { 
      "properties": { 
       "name": { 
        "type": "text" 
       }, 
       "location": { 
        "type": "text" 
       }, 
       "location_geo": { 
        "type": "geo_point" 
       } 

      } 
     } 
    } 
} 

使用geo_filter查詢

{ 
    "query": { 
     "bool": { 
      "filter": { 
       "geo_distance": { 
        "distance": "10km", 
        "location_geo": { 
         "lat": 40, 
         "lon": -70 
        } 
       } 
      } 
     } 
    } 
} 
+0

距離並不總是10km,這取決於餐廳自己的偏好。 – florind