Query on partial string

144 views
Skip to first unread message

chromerunner

unread,
Jan 6, 2009, 3:59:01 AM1/6/09
to Google App Engine
A noob question. I have a series of names in the datastore. They are
stored in this format: <surname>-<first initial>. Examples would be:

Smith-J
Python-M
Smith-KS
Doe-J

Users will want to find names by partial matching. Thus a query string
of 'Smith' should find both J and KS above... I just can't find a way
of doing it, am I missing what's glaringly obvious? Any pointers
please? Thanks

Marzia Niccolai

unread,
Jan 6, 2009, 12:45:07 PM1/6/09
to google-a...@googlegroups.com
Hi,

With App Engine, there is not a great way to do partial string searches.  To do prefix matching, which seems to be your use case, you can use inequality filters and the method described here:
http://code.google.com/appengine/docs/datastore/queriesandindexes.html

"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."

-Marzia

chromerunner

unread,
Jan 6, 2009, 1:44:34 PM1/6/09
to Google App Engine
Thanks Marzia, for two reasons (a) it is a valid workaround that I can
use and (b) I feel less incompetent in not having discovered an
obvious feature
Reply all
Reply to author
Forward
0 new messages