views:

526

answers:

2

Just starting with python on google appengine building a contact database. what is the best way to implement wildcard search?

For example can I do query('name=', %ewman%)

??

+3  A: 

Unfortunately, Google app engine can't do partial text matches

From the docs:

Tip: Query filters do not have an explicit way to match just part of a string value, but you can fake a prefix match using inequality filters:

db.GqlQuery("SELECT * FROM MyModel WHERE prop >= :1 AND prop < :2", "abc", u"abc" + u"\ufffd")

This matches every MyModel entity with a string property prop that begins with the characters abc. The unicode string u"\ufffd" represents the largest possible Unicode character. When the property values are sorted in an index, the values that fall in this range are all of the values that begin with the given prefix.

seth
Thanks seth. That seems a major limitation. Wonder how they implement partial matches in contacts.google.com? Maybe have to stick with a normal SQL database.
Peter Newman
I agree. There seems to be a few plugins that try and do a fulltext search but I have not tried them. This one 'seems' the most popular: http://code.google.com/p/app-engine-patch/
seth
Sticking with a 'normal SQL database' doesn't help, because an SQL DB does a full table scan to execute that query - not scalable at all! See my answer for details.
Nick Johnson
A: 

App Engine can't do 'like' queries, because it can't do them efficiently. Nor can your SQL database, though: A 'foo LIKE "%bar%"' query can only be executed by doing a sequential scan over the entire table.

What you need is an inverted index. Basic fulltext search is available in App Engine with SearchableModel. Bill Katz has written an enhanced version here, and there's a commercial solution for App Engine (with a free version) available here.

Nick Johnson