我希望从另一个实体返回包含索引文档和其他信息的结果对象,索引文档与该实体有关系。
所以,让我们说我有两种种类:
class Store(BaseHandler):
store_name = ndb.StringProperty()
logo_url = ndb.StringProperty()
about_store = ndb.TextProperty()
class Product(BaseHandler):
product_name = ndb.StringProperty
store_key = ndb.KeyProperty() #Store entity which created this product.
然后,我将每个新的Product实体添加到索引中,如下所示:
class NewProduct(BaseHandler):
def get(self, store_id):
self.render('new-product.html')
def post(self, store_id):
product_name = self.request.get('product_name')
store_key = ndb.Key('Store', store_id)
try:
p = Product(
store_key = store_key,
product_name = product_name)
p.put()
# Add p to index
p_doc = search.Document(
doc_id = str(p.key.id()),
fields = [
search.AtomField(name = 'store_id', value = str(str_id)),
search.TextField(name = 'product_name', value = e.product_name)])
index = search.Index('product_index')
index.put(p_doc)
except:
# handle error
现在,如果我使用...
运行搜索查询index = search.Index('product_index')
index.search('PRODUCT_NAME')
我应该能够通过查询字符串从索引返回所有Product
文档。
我的问题是:我如何有效地返回包含两者产品文档及其Store
种类信息(store_name,logo_url,about_store)的结果对象)?