如何在GAE搜索API中返回ScoredDocument的字段 - Python
我在从一个叫 ScoredDocument 的东西里提取特定的文档字段时遇到了麻烦。这应该很简单,但文档里似乎没有说明。
我已经正确地创建了一个索引,索引里有文档,并且搜索了这些文档,得到了结果。这些文档只有两个属性:标题和备注。我该怎么提取标题或备注呢?这是服务器的代码:
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: 'list' 对象没有 'title' 属性
有人能帮我解决这个问题吗? 谢谢
1 个回答
10
我认为应该是这样的:
note.field('title').value