2014-04-10 60 views
1

我無法從ScoredDocument中獲取特定的文檔字段。這一定很簡單,但文檔似乎沒有涵蓋它。如何從GAE搜索API中的ScoredDocument返回字段 - Python

我已經正確創建了索引,索引中的文檔並搜索了包含結果的文檔。文檔只有一個標題屬性和一個註釋屬性。我如何獲得標題或說明?這是服務器代碼:

class SearchHandler(webapp2.RequestHandler): 
def get(self): 
    index = search.Index(name="myIndex") 
    query_string = "dog" 
    try: 
     results = index.search(query_string) 
     logging.info(results) 
     self.response.out.write("""<html><body> 
     Here are the results from the search for "dog":""") 
     # Iterate over the documents in the results 
     for note in results: 
      self.response.out.write(note.fields) 
      self.response.out.write(note.fields.title) # HERE IS PROBLEM 
     self.response.out.write("</body></html>") 
    except search.Error: 
     logging.exception('Search failed') 

沒有試圖拿到冠軍是正確的,我得到一個ScoredDocument場輸出:

[search.TextField(name=u'title', value=u'A note with a dog'),  search.TextField(name=u'note', value=u'hamster'), search.DateField(name=u'updated', value=datetime.datetime(2014, 4, 10, 0, 0)), search.DateField(name=u'created', value=datetime.datetime(2014, 4, 10, 0, 0))] 

的努力拿到冠軍的誤差這種方式是這樣的: 在GET self.response.out.write(note.fields.title) AttributeError的: '名單' 對象有沒有屬性 '標題'

任何人都可以在這方面幫助? 感謝

+0

你應該構建能在一個單一的去被寫入到寫方法的響應,它不會工作,把它在一起,這樣的零碎。追加到一個字符串,然後輸出。 –

+0

構建響應似乎不成問題。當我刪除響應位時,在嘗試獲取'title'的值時遇到同樣的錯誤:'list'對象沒有屬性'title'。我特別需要一個調用來從search.TextField等搜索字段列表中獲取'title'的值。 – Mike

+0

絕對如此。我應該注意到它作爲一個單獨的問題,它不會解決你的問題,但它會解決你將有的後續問題,如果你繼續這樣做:) –

回答

8

我相信它應該是

note.field('title').value 
+0

這就是東西。謝謝! – Mike

+2

是否有任何理由不在文檔中列出? https://cloud.google.com/appengine/docs/python/search/documentclass#Document_fields – SamuelN

相關問題