2

我想實現的東西像Airbnb的地圖上拖動搜索(https://www.airbnb.com/s/Paris--France?source=ds&page=1&s_tag=PNoY_mlz&allow_override%5B%5D=基於地理空間和位置搜索

我節省了數據這樣的數據存儲

user.lat = float(lat) 
    user.lon = float(lon) 
    user.geoLocation = ndb.GeoPt(float(lat),float(lon)) 

,每當我在拖&降地圖或放大或縮小,我得到下面的參數在我的控制器

def get(self): 
    """ 
    This is an ajax function. It gets the place name, north_east, and south_west 
    coordinates. Then it fetch the results matching the search criteria and 
    create a result list. After that it returns the result in json format. 
    :return: result 
    """ 
    self.response.headers['Content-type'] = 'application/json' 
    results = [] 
    north_east_latitude = float(self.request.get('nelat')) 
    north_east_longitude = float(self.request.get('nelon')) 
    south_west_latitude = float(self.request.get('swlat')) 
    south_west_longitude = float(self.request.get('swlon')) 
    points = Points.query(Points.lat<north_east_latitude,Points.lat>south_west_latitude) 
    for row in points: 
     if row.lon > north_east_longitude and row.lon < south_west_longitude: 
      listingdic = {'name': row.name, 'desc': row.description, 'contact': row.contact, 'lat': row.lat, 'lon': row.lon} 
      results.append(listingdic) 
    self.write(json.dumps({'listings':results})) 

我的模型類爲g伊芬下面

class Points(ndb.Model): 
    name = ndb.StringProperty(required=True) 
    description = ndb.StringProperty(required=True) 
    contact = ndb.StringProperty(required=True) 
    lat = ndb.FloatProperty(required=True) 
    lon = ndb.FloatProperty(required=True) 
    geoLocation = ndb.GeoPtProperty() 

我想提高查詢。

在此先感謝。

+1

你工作的是什麼?它不夠好嗎?究竟需要改進什麼?順便說一句,你可能不需要三個不同的屬性來存儲位置,因爲這個'user.geoLocation = ndb.GeoPt(float(lat),float(lon))'應該足夠了。另外,不知道數據存儲有多好的地理位置搜索功能(從我聽說的 - 這不是),但我有很好的經驗(GAE的搜索API)(https://cloud.google.com/appengine/ docs/python/search /)及其[GeoPoint類](https://cloud.google.com/appengine/docs/python/search/geopointclass)。 –

+0

@MihailRussu我只想得到那些駐留在當前地圖邊界的結果。爲此,我們必須檢查4個條件,我在m個查詢中檢查其中的兩個條件,並且在循環中檢查其中的兩個條件以進一步對其進行過濾,以便進一步優化此條件,例如,應僅在查詢中檢查4個條件。因爲如果我將循環播放結果,它會減慢整個過程。如果我們談論ndb.GeoPtProperty(),我無法訪問它們中的單個lat和lng。 –

回答

2

不,您無法通過檢查查詢中的所有4個條件來改進解決方案,因爲ndb查詢不支持多個屬性上的不等式過濾器。從NDB Queries(重點煤礦):

限制:的數據存儲強制對查詢一些限制。 違反這些將導致它引發異常。例如, 組合太多的過濾器,使用不等式的多個 屬性,或者將不等式與 不同屬性上的排序順序組合在一起當前都是不允許的。另外過濾器 引用多個屬性有時需要二級索引 進行配置。

注:如前所述,數據存儲區拒絕使用不等式在一個以上的屬性過濾查詢。