Google App Engin (JAVA) でぶち当たった問題 その6

前回の全文検索に関連して、Compassを利用するまでも無いけどSQLのLike検索を行いたい場合には以下で試すことが可能。

Queries and IndexesPythonでの回避方法が書かれています。
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.

なのでJavaでちょっと動かして見ました。

Tで始まるアカウント名を検索する場合

char t = 'T';
char ch = '\ufffd';

Query query = new Query(Account.class.getSimpleName())
                    .addFilter("accountl", Query.FilterOperator.GREATER_THAN_OR_EQUAL, new String("T"))
                    .addFilter("account", Query.FilterOperator.LESS_THAN, t+ch);

となります。
Low-level APIを叩いて検索を行っています。Jdo、JPAで動くのかどうかは試していません。