What is a good pattern for inexact queries in the

2020-06-23 09:27发布

The Google App Engine Datastore querying language (gql) does not offer inexact operators like "LIKE" or even case insensitivity. One can get around the case sensitive issue by storing a lower-case version of a field. But what if I want to search for a person but I'm not sure of the spelling of the name? Is there an accepted pattern for dealing with this scenario?

2条回答
smile是对你的礼貌
2楼-- · 2020-06-23 09:48

Quoting from the documentation:

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.

http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html

Another option is the SearchableModel, however, i dont believe it supports partial matches.

http://billkatz.com/2008/8/A-SearchableModel-for-App-Engine

查看更多
Ridiculous、
3楼-- · 2020-06-23 09:56

You could store a soundex http://effbot.org/librarybook/soundex.htm version of the name in the datastore. Then, to query a name, soundex the query, and look that up.

查看更多
登录 后发表回答