开发者

Matching all records in a datastore query

Is there a way to substitute:

def get_objects(attr1,attr2,..):
    objects = Entities.all()

    if attr1 != None:
        objects.filter('attr1',attr1)
    if attr2 != N开发者_开发百科one:
        objects.filter('attr2',attr2)
    ....
    return objects

With a single query:

Entities.all().filter('attr1',attr1).filter('attr2',attr2)

By using some sort of 'match all' sign ( maybe a regexp query )? The problem with the first query is that ( apart from being ugly ) it creates indexes for all possible filter sequences.


The datastore doesn't support regex queries or OR queries.

However, if you're only using equality filters, indexes shouldn't be automatically created; these types of queries can be served using a merge-join strategy as long as the number of filters remains low (if you try to add too many filters, you'll get an error indicating that the existing indexes can't be used to execute the query efficiently; however, trying to add the required indexes in a case like this will usually result in the exploding indexes problem.)

The ugliness in the first approach can probably be solved by passing a list to your function instead of individual variables, then using a list comprehension instead of a bunch of if statements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜