Has anyone done a gqlquery with a like condition

7 views
Skip to first unread message

atomstore

unread,
Apr 14, 2008, 6:46:50 PM4/14/08
to Google App Engine
Does anyone know how to perform a LIKE condition on a select statement
in gql? I want to search a datastore and filter the query similar to
select * from emp where ename like 'S%' ;? In the normal Django api I
can do this as Person.objects.filter(name__icontains=value) on the
model.

Thanks

barryhunter

unread,
Apr 14, 2008, 7:48:13 PM4/14/08
to Google App Engine
Have a look at
http://code.google.com/appengine/docs/datastore/queriesandindexes.html
there is a Tip there on how to do that.

But it doesn't help with '%ending' style queries;p

Cary Palmer

unread,
Apr 14, 2008, 8:51:09 PM4/14/08
to google-a...@googlegroups.com
I tried to execute the query using the method described in the link
provided and I couldn't get it to work. I have this gqlquery gifts =
db.GqlQuery("SELECT * FROM Gift where name >= :1 AND name < :
2","dat", "dat") and its returns no rows. If I drop the second
parameter it returns all rows. COnsidering a like conditions are not
possible my thinking is to filter the resultset with a regular
expression, but that would mean a rather inefficient query if I had a
large resultset. These are my records in the store:

data
major tom
database
holiday

Benjamin Wohlwend

unread,
Apr 15, 2008, 1:14:16 AM4/15/08
to Google App Engine
Hi,

On Apr 15, 2:51 am, Cary Palmer <palmer.c...@gene.com> wrote:
> I tried to execute the query using the method described in the link  
> provided and I couldn't get it to work.  I have this gqlquery gifts =  
> db.GqlQuery("SELECT * FROM Gift where name >= :1 AND name < :
> 2","dat", "dat") and its returns no rows.

Try something like this:

the_string = 'dat'
gifts = db.GqlQuery('SELECT * FROM Gift WHERE name >= :1 AND name < :
2', the_string, the_string + 'z')

The docs (linked above by barryhunter) recommend to add "\xEF\xBF\xBD"
instead of 'z', because "\xEF\xBF\xBD" is the largest possible unicode
character, but that throws errors here and 'z' seems to work, too.

Regards,

Benjamin

Cary Palmer

unread,
Apr 15, 2008, 2:16:51 AM4/15/08
to google-a...@googlegroups.com
That worked great. I'm assuming the 'z' set the high order of the range properly. Thanks much!

Brett Morgan

unread,
Apr 15, 2008, 2:21:54 AM4/15/08
to google-a...@googlegroups.com
Assuming you are searching for 'foo' the query that DataStore executes
looks like this

SELECT * FROM Gift WHERE name >= 'foo' AND name < 'fooz'

Cary Palmer

unread,
Apr 15, 2008, 2:37:32 AM4/15/08
to google-a...@googlegroups.com
What I'm doing is an autocomplete ajax function that queries the database based on the characters entered on the ui. This worked with mysql and oracle. I want to return say the first five items from the query. I would thiink this query will do the job.

Brett Morgan

unread,
Apr 15, 2008, 3:51:07 AM4/15/08
to google-a...@googlegroups.com
Give it a try and get back to us with details on how it performs with
your dataset size?

If it doesn't perform, you have the option of building a trie over the
dataset in question:

http://en.wikipedia.org/wiki/Trie

Reply all
Reply to author
Forward
0 new messages