Wildcard search on Appengine in python

2019-02-10 04:57发布

I'm just starting with Python on Google App Engine building a contact database. What is the best way to implement wildcard search?

For example can I do query('name=', %ewman%)?

2条回答
贼婆χ
2楼-- · 2019-02-10 05:48

App Engine can't do 'like' queries, because it can't do them efficiently. Nor can your SQL database, though: A 'foo LIKE "%bar%"' query can only be executed by doing a sequential scan over the entire table.

What you need is an inverted index. Basic fulltext search is available in App Engine with SearchableModel. Bill Katz has written an enhanced version here, and there's a commercial solution for App Engine (with a free version) available here.

查看更多
不美不萌又怎样
3楼-- · 2019-02-10 05:55

Unfortunately, Google app engine can't do partial text matches

From the docs:

Tip: 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.

查看更多
登录 后发表回答