2013-02-11 37 views
0

問題:用戶(B)需要從一組基於特定標準的用戶(A)的幫助。此標準由用戶(A)在其個人資料中設置。獲取錯誤「無法滿足查詢 - 太多IN /!=值」。如何解決這個問題?

class UsersAProfiles(db.Model): 
    industries = db.StringListProperty() #technology, etc. (total 20) 
    agegroups = db.StringListProperty() #teenagers, etc. (total 10) 
    tags  = db.StringListProperty() #cooking, etc. 
    (while each User A can enter at most 10 tags, but there is no limit on 
    what tags are used, e.g., sql, gym, etc. (limited by dictionary!) 
    ...         #there are many other properties 

用戶(B)的規定,單獨存儲

class UserBRequestForHelp(db.Model): 
    myindustries = db.StringListProperty() #technology, etc. (<20) 
    myagegroups = db.StringListProperty() #teenagers, etc. (<10) 
    mytags  = db.StringListProperty() #cooking, etc. 
    ...          #there are many other properties 

現在我需要的所有用戶的列表中的條件的,可以幫助潛在B.對於我嘗試運行下面的查詢:

query = db.GqlQuery("SELECT * FROM UsersAProfiles WHERE 
     industries IN :1 AND 
     agegroups IN :2 AND 
     tags  IN :3", 
     userB_obj.myindustries , userB_obj.myagegroups, userB_obj.mytags) 

,但我得到了以下錯誤:

Cannot satisfy query -- too many IN/!= values. 

我真的被困在這裏,不知道如何解決這個問題。如何運行這樣的查詢。此外,我是否需要不同地設計模型類,以便可以運行此類查詢?如果是的話,有人可以幫忙。

由於一噸提前!

回答

0

對於上述問題,以下方法可能會有用。

  1. 在單獨的模型中列出標籤,其中包含所有匹配UserA配置文件的列表。

    class TAGS(db.Model): 
        UserAIds = db.StringListProperty() 
    

    在上面,每個標籤是關鍵。 (標籤=技術,青少年,烹飪等)

  2. 當用戶B設定的標準,則找到匹配的用戶A,我們可以運行的查詢如下:

    i = 0 
    for industry in userB_obj.myindustries: 
         t1_obj[i] = TAGS.get_by_key_name(industry) 
         i = i + 1 
    

    (在上述t1_obj [I]您有具有匹配產業用戶A簡檔列表)

    j = 0 
    for agegroup in userB_obj.myagegroups: 
         t2_obj[j] = TAGS.get_by_key_name(agegroup) 
         j = j + 1 
    

    (在上述t2_obj [j]的你有具有匹配agegroups用戶A簡檔)

    列表

    (在上面t3_obj [K]你有具有匹配標籤用戶一個配置文件列表)

  3. 接下來,你需要做的是找到存在於所有三種,即t1_obj用戶A型材,t2_obj,t3_obj就是這樣! 現在要查找上面所有3中存在的UserA配置文件,不確定是否有可以做到這一點的python函數。但是,使用模型實例可以按如下

    class MatchingUserAs(db.Model): 
         count = db.IntegerProperty(default=0) 
         source = db.StringProperty(default=None) 
    

    (在上面的模型類,用戶A的ID是重點解決這個問題。此UserAids存儲在t1_key [I] .UserAIds,t2_key [j]的.UserAids,t3_key [K] .UserAIds)

  4. 現在,通過t1_obj環[I],t2_obj [j]時,t3_obj [k]和將用戶標識插入到MatchingUserAs中,並在每次插入一行/更新一行時將計數增加1。

    <"loop through t1_obj[i]">: 
         Matchkey = MatchingUserAs.get_or_insert(t1_obj[i].UserAId) 
         Matchkey.count = 1 
         Matchkey.source = 'industry' 
         Matchkey.put() 
    
    <"loop through t2_obj[j]">: 
         Matchkey = MatchingUserAs.get_or_insert(t2_obj[j].UserAId) 
         #the following if check has been added to avoid incrementing the counter 
         #when same UserAid is present in, say, t2_obj[0], and t2_obj[1], etc. 
         if(Matchkey.source != 'agegroup') 
          Matchkey.count = Matchkey.count + 1 
          Matchkey.source = 'agegroup' 
         Matchkey.put() 
    
    <"loop through t3_obj[j]">: 
         Matchkey = MatchingUserAs.get_or_insert(t3_obj[j].UserAId) 
         if(Matchkey.source != 'tags') 
          Matchkey.count = Matchkey.count + 1 
          Matchkey.source = 'tags' 
         Matchkey.put() 
    
  5. 現在,所有你需要做的是從獲取那些MatchingUserAs與UserAs 3計數(因爲也有,我們要匹配的標籤清單3:工業,agegroups和標籤)。

上述代碼示例中可能會有一些語法錯誤,特別是對於鍵和對象的使用;並且在某些情況下使用了僞代碼。我只想給出一個解決方案的概述。希望這可以幫助。隨意分享任何意見。

0

當您使用在查詢中,GAE必須打破查詢分成若干「指數=價值」的子查詢,執行每收集結果組裝它們,就像他們曾經一次搜索。查詢可擴展到的子查詢數量有限制,並且that limit is 30。如果您使用31個子查詢創建查詢,這將解釋您爲何遇到此問題。換句話說,您的情況是len(userB_obj.myindustries)+ len(userB_obj.myagegroups)+ len(userB_obj.mytags)> 30.

+0

感謝您的信息。非常感謝。我也想知道解決方案應該是什麼。 (剛剛提出了一種替代解決方案(睡覺考慮解決方案,讓潛意識頭腦昨晚工作:-))。很快會發布答案。再次感謝您的信息! – 2013-02-11 19:26:50

相關問題