GAE搜索:索引中文档之间的关系

时间:2014-05-11 15:56:17

标签: python google-app-engine google-search app-engine-ndb google-search-api

我希望从另一个实体返回包含索引文档和其他信息的结果对象,索引文档与该实体有关系。

所以,让我们说我有两种种类:

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)的结果对象)?

0 个答案:

没有答案