3 回答

TA貢獻1878條經驗 獲得超4個贊
q := datastore.NewQuery("Products").Filter("Name >", name).Limit(20)
like
應用引擎上沒有任何操作,但是您可以使用“ <”和“>”
例子:
'moguz'>'moguzalp'

TA貢獻1712條經驗 獲得超3個贊
嘎!我剛剛意識到您的問題是特定于Go的。我下面的代碼適用于Python。道歉。我對Go運行時也很熟悉,以后可以將其翻譯成Python。但是,如果所描述的原理足以使您朝正確的方向前進,請告訴我,我也不會打擾。
AppEngine數據存儲區不直接支持此類操作,因此您必須滾動自己的功能來滿足此需求。這是一個快速,可行的解決方案:
class StringIndex(db.Model):
matches = db.StringListProperty()
@classmathod
def GetMatchesFor(cls, query):
found_index = cls.get_by_key_name(query[:3])
if found_index is not None:
if query in found_index.matches:
# Since we only query on the first the characters,
# we have to roll through the result set to find all
# of the strings that matach query. We keep the
# list sorted, so this is not hard.
all_matches = []
looking_at = found_index.matches.index(query)
matches_len = len(foundIndex.matches)
while start_at < matches_len and found_index.matches[looking_at].startswith(query):
all_matches.append(found_index.matches[looking_at])
looking_at += 1
return all_matches
return None
@classmethod
def AddMatch(cls, match) {
# We index off of the first 3 characters only
index_key = match[:3]
index = cls.get_or_insert(index_key, list(match))
if match not in index.matches:
# The index entity was not newly created, so
# we will have to add the match and save the entity.
index.matches.append(match).sort()
index.put()
要使用此模型,您每次添加可能在其上進行搜索的實體時,都需要調用AddMatch方法。在您的示例中,您有一個Product模型,用戶將在上進行搜索Name。在您的Product類中,您可能具有AddNewProduct創建新實體并將其放入數據存儲區的方法。您將添加該方法StringIndex.AddMatch(new_product_name)。
然后,在從AJAXy搜索框調用的請求處理程序中,您將使用StringIndex.GetMatchesFor(name)來查看所有以中的字符串開頭的存儲產品name,并以JSON或其他形式返回這些值。
代碼內部發生的事情是,名稱的前三個字符用于包含字符串列表的實體的key_name,所有存儲的名稱均以這三個字符開頭。使用三(相對于其他數字)絕對是任意的。系統的正確數字取決于您索引的數據量??梢源鎯υ赟tringListProperty中的字符串數量有限制,但是您還想平衡數據存儲中StringString實體的數量。進行一點數學運算即可為您提供合理數量的字符。
- 3 回答
- 0 關注
- 247 瀏覽
添加回答
舉報