5

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%)?

tew
  • 2,723
  • 5
  • 23
  • 35
Peter Newman
  • 117
  • 2
  • 7
  • 1
    For partial match using GAE Search API: http://stackoverflow.com/questions/12899083/partial-matching-gae-search-api/13171181#13171181 – Desmond Lua Nov 01 '12 at 05:01

2 Answers2

11

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.

seth
  • 36,759
  • 7
  • 60
  • 57
  • Thanks seth. That seems a major limitation. Wonder how they implement partial matches in contacts.google.com? Maybe have to stick with a normal SQL database. – Peter Newman Sep 10 '09 at 07:12
  • I agree. There seems to be a few plugins that try and do a fulltext search but I have not tried them. This one 'seems' the most popular: http://code.google.com/p/app-engine-patch/ – seth Sep 10 '09 at 16:09
  • Sticking with a 'normal SQL database' doesn't help, because an SQL DB does a full table scan to execute that query - not scalable at all! See my answer for details. – Nick Johnson Oct 29 '09 at 10:02
  • App Engine has implemented full text search recently. – Jon Jan 15 '13 at 20:36
3

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.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198