我建立了一个数据库,希望能够使用GAE中的搜索API进行搜索。我点击了Google关于API的教程,但我缺少的一件事是如何将数据存储真正变成"文档"。有什么好的教程吗?感谢
您不能转换数据库。模型ndb。要搜索的模型。文件
为什么?因为它没有太大的价值。
我给你举个例子——你在数据库中有一个字符串"this is black tag"。StringProperty()如何转换:
- 你可以用它作为原子-所以匹配将是如果完全匹配
- 您可以将其用作字符串,因此它将被分解为"this"、"is"、"black"、"tag",而不是标记化的"t"、"th"、"thi"、
- 你可以决定这是不可见的,因为在搜索中没有帮助,但提供了虚假的点击
您需要自己的设计搜索功能,应该是手动设计才是答案。
你只需要:
- 创建搜索。文件
- 添加字段
- 将文档添加到索引
阅读参考:https://developers.google.com/appengine/docs/python/search/documentclass
不幸的是,这是不可能的。
看看Index构造函数(python),我们可以发现在早期阶段已经尝试过实现它,但它从未真正起作用。指定索引的源已经被弃用一段时间了,现在已经不起作用了。
这里的构造函数pydoc:
class Index(object):
[...]
def __init__(self, name, namespace=None, source=SEARCH):
"""Initializer.
Args:
name: The name of the index. An index name must be a visible printable
ASCII string not starting with '!'. Whitespace characters are excluded.
namespace: The namespace of the index name. If not set, then the current
namespace is used.
source: Deprecated as of 1.7.6. The source of
the index:
SEARCH - The Index was created by adding documents throught this
search API.
DATASTORE - The Index was created as a side-effect of putting entities
into Datastore.
CLOUD_STORAGE - The Index was created as a side-effect of adding
objects into a Cloud Storage bucket.
[...]
"""
因此,至少目前(?),唯一的解决方案,如Tim Hoffman所提到的,是将文档和索引与数据存储数据分开处理。
您仍然可以向https://code.google.com/p/googleappengine/issues/list就在那里。
我有数千个实体想要为其建立索引,然后使用mapreduce为我的实体建立索引,并通过搜索API进行搜索。地图还原作业是
- name: buildindex
mapper:
input_reader: mapreduce.input_readers.DatastoreInputReader
handler: main.buildindex
params:
- name: entity_kind
default: main.Article
功能
def buildindex(entity):
try:
doc = search.Document(doc_id=str(entity.key()), fields=[
search.TextField(name='title', value=entity.title),
search.TextField(name='text', value=entity.text),
])
search.Index(name='myIndex').put(doc)
except Exception, e:
logging.exception('Mapreduce has exception:%s' % str(e))