5

I would like to use Search API with an application that is already using a defined model (db.Model).

For example, suppose that my model looks like this:

class Greeting(db.Model):
    author = db.StringProperty()
    content = db.StringProperty()
    date = db.DateTimeProperty()

Now how do I use the Search API to query the Greeting entity?

I have read the documentation but honestly I don't understand that.

Please give me a very simple example.

Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
gather
  • 51
  • 3

1 Answers1

5

You don't.

The search API needs to search "documents" that you have created, not models from the datastore.

  1. Build documents structured with fields to describe the data you wish to search
  2. Create an index of documents you wish to search
  3. Construct queries to search the index
  4. Build search requests to run queries against the documents in your application Score results and customize their presentation to the user

You'll have to write a converter that loads data from your models and creates searchable documents that can then be put into the index.

E.G. from the docs to create a document:

from google.appengine.api import search

search.Document(
    doc_id='document id',
    fields=[search.TextField(name='subject', value='going for dinner'),
            search.HtmlField(name='body', value='<html>I found a place.</html>'),
            search.TextField(name='signature', value='brzydka pogoda', language='pl')],
    language='en')

So that document has 3 separate fields that can be searched individually.

The Document Class

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
  • Thanks. So Document class is a new way for store data ? There any advantage or disadvantage of using documents instead db.Model (of course except Search API) ? Documents are store using BigTable ? – gather Nov 09 '12 at 12:53
  • They are totally different things. So it's not a case of advantages or disadvantages, it's about what it is you need to do and which option allows that. It does not matter how your source data is currently stored, you'll still have to write a mechanism to convert it into searchable documents. – Paul Collingwood Nov 09 '12 at 13:22