如何从GAE Search API中的ScoredDocument返回一个字段 - Python

Mik*_*ike 2 python google-app-engine google-search-api

我在ScoredDocument中获取特定文档字段时遇到问题.这必须非常简单,但文档似乎并未涵盖它.

我已在索引中正确创建索引,文档并搜索带有结果的文档.文档只有一个title属性和一个note属性.如何获得标题或说明?这是服务器代码:

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')
Run Code Online (Sandbox Code Playgroud)

没有尝试获取标题的输出是正确的,我得到一个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))] 
Run Code Online (Sandbox Code Playgroud)

尝试以这种方式获得标题的错误是:在get self.response.out.write(note.fields.title)中AttributeError:'list'对象没有属性'title'

有人能帮忙吗?谢谢

asc*_*d00 9

我相信它应该是

note.field('title').value
Run Code Online (Sandbox Code Playgroud)

  • 有没有理由说明他们的文档中没有列出这些内容?https://cloud.google.com/appengine/docs/python/search/documentclass#Document_fields (2认同)