简体   繁体   English

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

[英]How to implement the Search API on my GAE datastore?

I built a database that I would like to be able to search using the Search API in GAE. 我建立了一个数据库,希望能够使用GAE中的Search API进行搜索。 I clicked through the tutorials that Google has on the API, but the one thing I'm missing is how to actually turn a datastore kind into a "document". 我单击了Google在API上的教程,但是我所缺少的一件事是如何将数据存储类型实际转换为“文档”。 Is there a good tutorial for that somewhere? 在某个地方有一个好的教程吗? Thank you 谢谢

You can not convert db.Model ndb.Model to search.Document. 您不能将db.Model ndb.Model转换为search.Document。

Why? 为什么? because it does not have too much value. 因为它没有太多价值。

I give you some example - you have string 'this-is-black-tag' in db.StringProperty() how to convert it: 我给你举个例子-db.StringProperty()中的字符串'this-is-black-tag'如何转换:

  1. you can use it as Atoms - so match will be if exact match 您可以将其用作Atoms-如果完全匹配,则匹配
  2. you can use it as String - so it will be broken into 'this', 'is', 'black', 'tag' than tokenized 't', 'th', 'thi', 'this', ... 您可以将其用作字符串-它将比标记化的't','th','thi','this',...分成'this','is','black','tag'
  3. you can decide that is not visible since not help in search but give false hits 您可以确定它是不可见的,因为这对搜索没有帮助,但会带来错误的点击

You need design search feature yourself that it should be manual design that is answer . 您需要自己的设计搜索功能,它应该是答案的手动设计

You just need: 您只需要:

  1. create search.Document 创建search.Document
  2. add fields 添加字段
  3. add document to index 将文档添加到索引

Read reference: https://developers.google.com/appengine/docs/python/search/documentclass 阅读参考资料: https : //developers.google.com/appengine/docs/python/search/documentclass

Unfortunately this is not possible. 不幸的是,这是不可能的。

Looking at the Index constructor (python), we can se that there has been some tries on implementing that in early phases but it never actually worked. 查看Index构造函数(python),我们可以发现有一些尝试可以在早期阶段实现它,但实际上并没有奏效。 Specifying the source of the index has been deprecated for a while now and doesn't work anymore. 指定索引的来源已经过了一阵子了,现在不再起作用了。

Here the constructor pydoc: 这是构造函数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.
  [...]
  """

So, at least for now (?), the only solution, like mentioned by Tim Hoffman , is to handle your documents and index separately from your datastore data. 因此,至少就目前而言(?),唯一的解决方案(如Tim Hoffman所述 )是将文档和索引与数据存储区数据分开处理。

You can still file a feature request to https://code.google.com/p/googleappengine/issues/list ans there where that goes. 您仍然可以向https://code.google.com/p/googleappengine/issues/list ans提出功能请求。

I had thousands of entities that I wanted to build an index for and then used mapreduce to build an index for my entities and that became searchable with the search API. 我有成千上万个实体,我想为其建立索引,然后使用mapreduce为我的实体建立索引,并且可以使用搜索API进行搜索。 The mapreduce job was mapreduce工作是

- name: buildindex
  mapper:
    input_reader: mapreduce.input_readers.DatastoreInputReader
    handler: main.buildindex
    params:
    - name: entity_kind
      default: main.Article

Function 功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM