如何从GAE NDB中的searchResults对象获取实体

时间:2013-03-25 07:01:53

标签: google-app-engine app-engine-ndb

我不确定我是否正在使用NDB Search API,因为它意味着要使用。我已经阅读了文档,但我认为我要么缺少某些东西,要么缺乏我的python技能。任何人都可以确认/改善使用搜索的这种进展吗?

# build the query object
query_options = search.QueryOptions(limit=results_per_page, offset=number_to_offset)
query_object = search.Query(query_string=escaped_param, options=query_options)

# searchResults object
video_search_results = videos.INDEX.search(query_object)

# ScoredDocuments list
video_search_docs = video_search_results.results

# doc_ids
video_ids = [x.doc_id for x in video_search_docs]

# entities
video_entities = [Video.GetById(x) for x in video_ids]

2 个答案:

答案 0 :(得分:1)

我可能会亲自写下这样的内容:

# build the query object
query_options = search.QueryOptions(limit=results_per_page, offset=number_to_offset)
query_object = search.Query(query_string=escaped_param, options=query_options)

# do the search
video_search = search.Index(name=VIDEO_INDEX).search(query_object)

# list of matching video keys
video_keys = [ndb.Key(Video, x.doc_id) for x in video_search.results]

# get video entities
video_entities = ndb.get_multi(video_keys)

使用ndb.get_multi会更有效率。您可以使用AppStats来验证。如果您在RPC未完成时可以执行其他处理,则还可以查看异步等效项。

我不确定Video.GetById方法实际上是什么,但我建议您在Model.get_by_id上看到ndb文档。

答案 1 :(得分:0)

您的代码似乎一切正常。它有什么问题吗?