Using __key__ and SearchableModel

3 views
Skip to first unread message

acuth

unread,
Jun 11, 2009, 7:09:17 PM6/11/09
to Google App Engine
Hi everyone,

just caught up with one of those Google IO videos about developing
apps on GAE. I was already using separated out index-blocks so that I
could restrict what was indexed for SearchableModel. Now I just want
to return the keys rather than the object themselves.

Does anybody know if it is possible to return just the keys - as per
SELECT __KEY__ or Query(keys_only=True) - on queries/searches using
the SearchableModel class, for example:

from google.appengine.ext import search

class ItemIB(search.SearchableModel):
name = db.StringProperty()

....
# search for items with name that matches q, but returns entire
ItemIB
query = ItemIB.all().search(q)

# try replacing Model.all() with db.Query(Model) doesn't work
query = db.Query(ItemIB,keys_only=True).search(q)
....


Any help very gratefully received, Adrian

Antoniov

unread,
Jun 11, 2009, 10:48:54 PM6/11/09
to Google App Engine
SELECT [* | __key__ ] FROM <entity>
[WHERE <condition> [AND <condition> ...]]
[ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]]
[LIMIT [<offset>,]<count>]
[OFFSET <offset>]
[HINT (ORDER_FIRST | HINT FILTER_FIRST | HINT ANCESTOR_FIRST)]

Is this what you're looking for?

acuth

unread,
Jun 12, 2009, 3:40:57 AM6/12/09
to Google App Engine
With the SearchableModel class you get a new method called search()
that takes as a parameter the terms you are searching on. AFAIK there
isn't a GQL construct that matches it.

Antoniov

unread,
Jun 12, 2009, 8:53:31 AM6/12/09
to Google App Engine
The following code may help, or you could extends your own class from
SearchableModel.


class SearchableModel2(db.Model):

class Query(db.Query):
"""A subclass of db.Query that supports full text search."""

def __int__(self, model_class, key_only):
is_key_only = key_only
super(Query, self).__int__(self, model_class, is_key_only)

_search_query = None

def search(self, search_query):
"""Adds a full text search to this query.

Args:
search_query, a string containing the full text search query.

Returns:
self
"""
self._search_query = search_query
return self

def _get_query(self):
"""Wraps db.Query._get_query() and injects SearchableQuery."""
query = db.Query._get_query(self,
_query_class=SearchableQuery,

_multi_query_class=SearchableMultiQuery)
if self._search_query:
query.Search(self._search_query)
return query

def _populate_internal_entity(self):
"""Wraps db.Model._populate_internal_entity() and injects
SearchableEntity."""
return db.Model._populate_internal_entity(self,

_entity_class=SearchableEntity)

@classmethod
def from_entity(cls, entity):
"""Wraps db.Model.from_entity() and injects SearchableEntity."""
if not isinstance(entity, SearchableEntity):
entity = SearchableEntity(entity)
return super(SearchableModel2, cls).from_entity(entity)

@classmethod
def all(cls,key_only):
"""Returns a SearchableModel2.Query for this kind."""
return SearchableModel2.Query(cls, SearchableModel2, key_only)

twan...@googlemail.com

unread,
Jun 29, 2009, 11:55:46 AM6/29/09
to Google App Engine


On 12 Jun., 01:09, acuth <adrian.cuthb...@gmail.com> wrote:
> Hi everyone,
>
> just caught up with one of those Google IO videos about developing
> apps on GAE. I was already using separated out index-blocks so that I
> could restrict what was indexed for SearchableModel. Now I just want
> to return the keys rather than the object themselves.
>
> Does anybody know if it is possible to return just the keys - as per
> SELECT __KEY__ or Query(keys_only=True) - on queries/searches using
> the SearchableModel class, for example:
>
> from google.appengine.ext importsearch
>
> class ItemIB(search.SearchableModel):
>     name = db.StringProperty()
>
> ....
>     #searchfor items with name that matches q, but returns entire
> ItemIB
>     query = ItemIB.all().search(q)
>
>     # try replacing Model.all() with db.Query(Model) doesn't work
>     query = db.Query(ItemIB,keys_only=True).search(q)
> ....
>
> Any help very gratefully received, Adrian

Hi,
you can use gae-search for this. Visit http://gae-full-text-search.appspot.com/
and take a look at the documentation/demos. I hope you will like it.

Best regards
Thomas
Reply all
Reply to author
Forward
0 new messages