views:

260

answers:

2

Hi,

Am wondering if anyone can provide some guidance on how I might implement a starts with or ends with query against a Datastore model using Python?

In pseudo code, it would work something like...

Query for all entities A where property P starts with X

or

Query for all entities B where property P ends with X

Thanks, Matt

+1  A: 

Seems you can't do it for the general case, but can do it for prefix searches (starts with):

http://stackoverflow.com/questions/1402769/wildcard-search-on-appengine-in-python

Lee B
+2  A: 

You can do a 'starts with' query by using inequality filters:

MyModel.all().filter('prop >=', prefix).filter('prop <', prefix + u'\ufffd')

Doing an 'ends with' query would require storing the reverse of the string, then applying the same tactic as above.

Nick Johnson
Thanks. This worked in combination with storing the data in same case (either lower or upper). Appreciate the help :)
Matty
@Matty, yet another case in which nonrelational DBs require denormalization (based on knowing the important queries) for efficiency. Sigh, but, we'd better all get used to it!-)
Alex Martelli