如何在我的GAE数据存储上实现Search API?

时间:2014-04-17 23:50:30

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

我建立了一个数据库,我希望能够使用GAE中的Search API进行搜索。我点击了Google在API上的教程,但我忽略的一件事是如何将数据存储类型转换为"文档"。那个地方有一个很好的教程吗?谢谢

3 个答案:

答案 0 :(得分:3)

您无法将db.Model ndb.Model转换为search.Document。

为什么呢?因为它没有太大的价值。

我举一些例子 - 你在db.StringProperty()中有字符串'this-is-black-tag'如何转换它:

  1. 您可以将其用作Atoms - 因此匹配将是完全匹配
  2. 你可以把它当作字符串 - 所以它会被分解为'this','is','black','tag'而不是标记化't','th','thi','this',. ..
  3. 你可以决定不可见,因为在搜索中没有帮助但是会给出错误的点击
  4. 您自己需要设计搜索功能,应该是应答的手动设计

    你只需要:

    1. 创建search.Document
    2. 添加字段
    3. 将文档添加到索引
    4. 阅读参考:https://developers.google.com/appengine/docs/python/search/documentclass

答案 1 :(得分:0)

不幸的是,这是不可能的。

看看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提交功能请求,并在那里提交。

答案 2 :(得分:0)

我有数以千计的实体要构建索引,然后使用mapreduce为我的实体构建索引,并且可以使用搜索API进行搜索。 mapreduce工作是

- 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))