开发者

How to implement Django like 'contains' filter query with Google App Engine?

开发者 https://www.devze.com 2023-01-08 15:14 出处:网络
Is there some way to implement Django like contains or icontains filters with Google App Engine as well? I am using app engine patch.

Is there some way to implement Django like contains or icontains filters with Google App Engine as well? I am using app engine patch.

I have used the below query earlier to match the start of a string but now the requirement is to find a string pattern wit开发者_运维知识库hin Datastore.

Employee.all().filter('name >=',value).filter('name <',unicode(value) + u'\ufffd')

Basically the requirement is to implement a search functionality with all the strings containing the search string as a result.

Please suggest.

Thanks in advance.


What you need to do is create a string list property with the different string permutations and query on that list.

Take a look at appengine.ext.search.SearchableModel for an example implementation. You can also check out nonrel-search.


No, a true substring search isn't really possible in the datastore.

The best you could do would be to add a keyword index and do the "begins with" type query from your question on that, but even then for an entity like "foo bar baz" you could match when searching for "ba" but not "ar b".

This sounds like a great job for BigQuery, which is in preview right now with a wait list to get access.


Recently I ran into the same problem and made my own model manager with an overriden filter function that also understands __contains. I'm assuming you're using Django.

class UserFeedManager(models.Manager):    
    def filter(self, *args, **kwargs):
        keywordargs = {}
        for (arg, val) in kwargs.items():
            if '__contains' in arg:
                contains.append((arg.split('__')[0], val))
            else:
                keywordargs[arg] = val

        result = []
        if len(contains) > 0:
            for entry in super(UserFeedManager, self).filter(*args, **keywordargs):
                if all(map(lambda (attr, val): val in entry.__getattribute__(attr),
                           contains)):
                    result.append(entry)
        else:
            result = super(UserFeedManager, self).filter(*args, **keywordargs)

        return result

Essentially it builds a list of arguments that have a __contains, then fetches the whole result and filters it to only hold the results that pass all criteria.


querySet = Entity.objects.all()
ids = []
for i,obj in enumerate(querySet.iterator()):
    str_to_search =obj.name
    if str_to_search.find('blahblah') != -1:
        ids.append(obj.id)
querySet = querySet.filter(id__in = ids)
0

精彩评论

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