本文整理汇总了Python中google.appengine.api.search.Document方法的典型用法代码示例。如果您正苦于以下问题:Python search.Document方法的具体用法?Python search.Document怎么用?Python search.Document使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.appengine.api.search
的用法示例。
在下文中一共展示了search.Document方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: to_document
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def to_document(self):
"""Creates a search.Document representation of a model.
Returns:
search.Document of the current model to be indexed with a valid doc_id. A
doc_id will be autogenerated when inserted into the Index if one is
not provided.
Raises:
DocumentCreationError: when unable to create a document for the
model.
"""
try:
return search.Document(
doc_id=six.ensure_str(self.key.urlsafe()),
fields=self._get_document_fields())
except (TypeError, ValueError) as e:
raise DocumentCreationError(e)
示例2: test_clear_index
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def test_clear_index(self):
test_index = Test.get_index()
test_index.put([
search.Document(
doc_id='test_id_one', fields=[
search.TextField(name='field_one', value='value_one')]),
search.Document(
doc_id='test_id_two', fields=[
search.AtomField(name='field_two', value='value_two')]),
search.Document(
doc_id='test_id_three', fields=[
search.NumberField(name='field_three', value=3)])])
# Ensure both docs were added to the index.
self.assertLen(test_index.get_range(ids_only=True), 3)
Test.clear_index()
# Ensure the index was cleared.
self.assertFalse(test_index.get_range(ids_only=True))
示例3: test_search
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def test_search(self):
index = Test.get_index()
test_doc = search.Document(
doc_id='test_doc_id_1',
fields=[search.TextField(name='text_field', value='item 1')])
not_used_doc = search.Document(
doc_id='test_doc_id_2',
fields=[search.TextField(name='text_field', value='item 2')])
not_used_doc2 = search.Document(
doc_id='test_doc_id_3',
fields=[search.TextField(name='text_field', value='notused')])
index.put(test_doc)
index.put(not_used_doc)
index.put(not_used_doc2)
actual_search_result = Test.search(query_string='item', query_limit=1)
self.assertEqual(actual_search_result.number_found, 2)
self.assertLen(actual_search_result.results, 1)
no_search_results = Test.search(query_string='does_not_exist')
self.assertEqual(no_search_results.number_found, 0)
示例4: _create_doc
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def _create_doc(self, report):
doc_fields = [
search.TextField(name='report_type', value=report.report_type),
search.TextField(name='thread_id', value=report.thread_id),
search.TextField(name='history_id', value=report.history_id),
search.DateField(name='date_received', value=report.date_received),
search.DateField(name='date_reported', value=report.date_reported),
search.DateField(name='date_responded', value=report.date_responded),
search.TextField(name='has_responded', value=str(report.has_responded)),
search.TextField(name='status', value=report.status),
search.TextField(name='sender', value=str(report.sender)),
search.TextField(name='reported_by', value=report.reported_by),
search.TextField(name='subject', value=report.subject),
search.HtmlField(name='html', value=report.html),
search.TextField(name='text', value=report.text)
]
return search.Document(doc_id=str(report.key.id()), fields=doc_fields)
示例5: create_document
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def create_document():
document = search.Document(
# Setting the doc_id is optional. If omitted, the search service will
# create an identifier.
doc_id='PA6-5000',
fields=[
search.TextField(name='customer', value='Joe Jackson'),
search.HtmlField(
name='comment', value='this is <em>marked up</em> text'),
search.NumberField(name='number_of_visits', value=7),
search.DateField(name='last_visit', value=datetime.now()),
search.DateField(
name='birthday', value=datetime(year=1960, month=6, day=19)),
search.GeoField(
name='home_location', value=search.GeoPoint(37.619, -122.37))
])
return document
示例6: _index_employees
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def _index_employees(employees):
logging.info('Indexing employees... {}MB'.format(memory_usage().current()))
index = search.Index(name=INDEX_NAME)
# According to appengine, put can handle a maximum of 200 documents,
# and apparently batching is more efficient
for chunk_of_200 in chunk(employees, 200):
documents = []
for employee in chunk_of_200:
if employee is not None:
# Gross hack to support prefix matching, see documentation for _generate_substrings
substrings = u' '.join([
_generate_substrings(employee.first_name),
_generate_substrings(employee.last_name),
_generate_substrings(employee.username),
])
doc = search.Document(fields=[
# Full name is already unicode
search.TextField(name='full_name', value=employee.full_name),
search.TextField(name='username', value=unicode(employee.username)),
search.TextField(name='substrings', value=substrings),
])
documents.append(doc)
index.put(documents)
logging.info('Done indexing employees. {}MB'.format(memory_usage().current()))
示例7: put_one_document
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def put_one_document(self, msg):
doc_id = '{channel_id}_{user}_{ts}'.format(channel_id=msg.channel_id, user=msg.user, ts=int(msg.ts))
doc = search.Document(
doc_id=doc_id,
fields=[search.TextField(name='text', value=msg.text),
search.AtomField(name='user_name', value=msg.get_user_name()),
search.AtomField(name='channel_id', value=msg.channel_id),
search.AtomField(name='msg_key', value=str(msg.key.id())),
search.DateField(name='ts', value=msg.get_datetime()),
]
)
# Index the document.
try:
self.index.put(doc)
except search.PutError, e:
result = e.results[0]
if result.code == search.OperationResult.TRANSIENT_ERROR:
# possibly retry indexing result.object_id
return self.put_one_document(msg)
示例8: test_index_entities_for_search
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def test_index_entities_for_search(self, mock_to_document):
base_model.search.MAXIMUM_DOCUMENTS_PER_PUT_REQUEST = 1
test_entity_1_key = Test(text_field='item_1').put()
test_entity_2_key = Test(text_field='item_2').put()
documents = [
search.Document(
doc_id=test_entity_1_key.urlsafe(),
fields=[search.TextField(name='text_field', value='item_1')]),
search.Document(
doc_id=test_entity_2_key.urlsafe(),
fields=[search.TextField(name='text_field', value='item_2')]),]
mock_to_document.side_effect = documents
Test.index_entities_for_search()
self.assertEqual(mock_to_document.call_count, 2)
示例9: test_add_docs_to_index
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def test_add_docs_to_index(self):
base_model.BaseModel._INDEX_NAME = 'test'
test_index = base_model.BaseModel.get_index()
base_model.BaseModel.add_docs_to_index([search.Document(
doc_id='test_id', fields=[
search.TextField(name='field_one', value='value_one')])])
self.assertIsInstance(
test_index.get_range(
start_id='test_id', limit=1, include_start_object=True)[0],
search.Document)
示例10: test_add_docs_to_index_put_error
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def test_add_docs_to_index_put_error(self, mock_put):
mock_put.side_effect = [
search.PutError(message='Fail!', results=[
search.PutResult(code=search.OperationResult.TRANSIENT_ERROR)]),
None]
base_model.BaseModel.add_docs_to_index([search.Document(
doc_id='test_id', fields=[
search.TextField(name='field_one', value='value_one')])])
self.assertEqual(mock_put.call_count, 2)
示例11: test_add_docs_to_index_error
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def test_add_docs_to_index_error(self, mock_error, mock_put, mock_logging):
mock_put.side_effect = mock_error
base_model.BaseModel.add_docs_to_index([search.Document(
doc_id='test_id', fields=[
search.TextField(name='field_one', value='value_one')])])
self.assertEqual(mock_put.call_count, 1)
self.assertEqual(mock_logging.error.call_count, 1)
示例12: test_remove_doc_by_id
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def test_remove_doc_by_id(self):
base_model.BaseModel._INDEX_NAME = 'test'
test_index = base_model.BaseModel.get_index()
test_index.put([search.Document(
doc_id='test_id', fields=[
search.TextField(name='field_one', value='value_one')])])
base_model.BaseModel.remove_doc_by_id('test_id')
test_response = test_index.get_range(
start_id='test_id', limit=1, include_start_object=True)
self.assertIsInstance(test_response, search.GetResponse)
self.assertEqual(test_response.results, [])
示例13: test_to_document
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def test_to_document(self, mock_get_document_fields):
test_model = Test(id='fake')
fields = [search.AtomField(name='text_field', value='12345ABC')]
mock_get_document_fields.return_value = fields
test_document = search.Document(
doc_id=six.ensure_str(test_model.key.urlsafe()), fields=fields)
result = test_model.to_document()
self.assertEqual(result, test_document)
示例14: add_faceted_document
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def add_faceted_document(index):
document = search.Document(
doc_id='doc1',
fields=[
search.AtomField(name='name', value='x86')],
facets=[
search.AtomFacet(name='type', value='computer'),
search.NumberFacet(name='ram_size_gb', value=8)])
index.put(document)
示例15: document
# 需要导入模块: from google.appengine.api import search [as 别名]
# 或者: from google.appengine.api.search import Document [as 别名]
def document():
return search.Document(
doc_id='doc1',
fields=[
search.TextField(name='title', value='Meep: A biography')])