python GAE搜索增强型NDB模型:使可搜索的索引与数据存储模型中的最新信息保持同步。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python GAE搜索增强型NDB模型:使可搜索的索引与数据存储模型中的最新信息保持同步。相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Search-augmented NDB Models for Google App Engine.
Usage:
import search_models
# Define an index for the document type.
PEOPLE_INDEX = search.Index(name='people')
# Define a `_document` property on the Model and add the hooks.
class Person(ndb.Model):
first_name = ndb.StringProperty()
last_name = ndb.StringProperty()
full_name = ndb.ComputedProperty(lambda self: self.first_name + ' ' + self.last_name)
birthday = ndb.DateProperty()
...
@property
def _document(self):
return search.Document(doc_id=self.key.id(), fields=[
search.TextField(name='first', value=self.first_name),
search.TextField(name='last', value=self.last_name),
search.TextField(name='name', value=self.full_name),
search.TextField(name='first', value=self.first_name),
search.DateField(name='birthday', value=self.birthday),
...
])
def _post_put_hook(self, future):
PEOPLE_INDEX.put(self._document)
@classmethod
def _post_delete_hook(cls, key, future):
PEOPLE_INDEX.delete(key.string_id())
# Handle search requests
class SearchHandler(webapp2.RequestHandler):
def get(self, *args, **kwargs):
query = self.request.get('query')
cursor = self.request.get('cursor', '')
results = self.do_search(query, cursor)
instances = self.format_documents(results.results)
self.render(instances)
def do_search(self, query, cursor=None):
query_opts = search.QueryOptions(limit=15)
query_instance = search.Query(query_string=query, options=query_opts)
return PEOPLE_INDEX.search(query_instance)
def format_documents(self, documents):
results = []
for doc in documents:
fmt_doc = {'id': doc.doc_id}
for field in doc.fields:
fmt_doc[field.name] = field.value
results.append(fmt_doc)
return results
"""
__author__ = 'Eric Higgins'
__copyright__ = 'Copyright 2013, Eric Higgins'
__version__ = '0.0.1'
__email__ = 'erichiggins@gmail.com'
__status__ = 'Development'
以上是关于python GAE搜索增强型NDB模型:使可搜索的索引与数据存储模型中的最新信息保持同步。的主要内容,如果未能解决你的问题,请参考以下文章
GAE数据存储区查询ConjunctionNode错误
带有 GAE ndb 的 Flask Flask 安全性?
GAE,删除 NDB 命名空间
NDB 层次结构和实体组的 GAE 含义
GAE ndb 存储大型一对多关系的最佳实践
多对多关系 GAE NDB 需要父母还是子女?