使用 objectify 在实体中搜索子字符串
我有一个名为“lastName”的实体,其值为“Benjamin”。有没有一种方法可以客观化,如果用户输入“Ben”或“jam”或“Benja”。我仍然可以使用 query.filter() 找到这个实体。我必须使用查询,因为我正在检查其他搜索条件。
我在“Obgaektify”中看到了一些称为“开头为”运算符的内容。但它不起作用。任何建议将不胜感激。谢谢
I have an entity called lastName with value "Benjamin". Is there a way in objectify that if the user put "Ben" or "jam" or "Benja". I still be able to find this entity using query.filter(). I must use the query as there are other search criteria iam checking.
I saw something in "Obgaektify" called "starts with" operator. but it isnot working. Any suggestions would be appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
子字符串没有“LIKE”类型查询,但是可以通过利用
> 和
< 来模拟区分大小写“开头为”。索引上的
运算符。该查询将在
name
属性中“搜索”以“Ben”开头且小于“Ben\ufffd”
的项目,其中\ufffd 是最高可能的 unicode 字符。
There's no "LIKE" type queries for sub-string, however a case sensitive "starts with" could be simulated by taking advantage of the
>
and<
operators on indexes.The query will 'search' the
name
property for items that begining with "Ben", and are less than"Ben\ufffd"
, where\ufffd
is the highest possible unicode character.对于类似包含的查询,没有标准的现有索引。顺便说一句,您随时可以介绍自己的。在这种情况下,您可以执行以下操作:
String[] lastNameIndex
@PrePersist
的方法,该方法将使用所有内容填充lastNameIndex
字段可用组合query.filter('lastNameIndex =', val)
There is no standard existing index for contains-like queries. Btw, you can always introduce your own. At this case you can do:
String[] lastNameIndex
@PrePersist
that will filllastNameIndex
field with all available combinationsquery.filter('lastNameIndex =', val)
将 Chris 的答案和 Nick 的评论放在一起,以下是为 objectify V4 构建查询过滤器的代码:
Putting the answer from Chris and the comment from Nick together, here is the code to build a query filter for objectify V4:
我使用了标记化方法。下面是 Java 代码:
这允许定义可索引字段,然后处理标准 Ofy 查询和 SearchService。
I have used the tokenization method. Here is the code in Java:
This allows to define an indexable field, then process standard Ofy queries and SearchService.