使用 objectify 在实体中搜索子字符串

发布于 2024-11-28 16:04:27 字数 194 浏览 4 评论 0原文

我有一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拒绝两难 2024-12-05 16:04:27

子字符串没有“LIKE”类型查询,但是可以通过利用 > 和 < 来模拟区分大小写“开头为”。索引上的 运算符。

// The start string
String searchStr = "Ben";

// emulate a "starts with" query
Query q = new Query("MyEntity")
q.addFilter("name", Query.FilterOperator.GREATER_THAN_OR_EQUAL, searchStr);
q.addFilter("name", Query.FilterOperator.LESS_THAN, searchStr + "\ufffd");

该查询将在 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 start string
String searchStr = "Ben";

// emulate a "starts with" query
Query q = new Query("MyEntity")
q.addFilter("name", Query.FilterOperator.GREATER_THAN_OR_EQUAL, searchStr);
q.addFilter("name", Query.FilterOperator.LESS_THAN, searchStr + "\ufffd");

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.

菩提树下叶撕阳。 2024-12-05 16:04:27

对于类似包含的查询,没有标准的现有索引。顺便说一句,您随时可以介绍自己的。在这种情况下,您可以执行以下操作:

  1. 添加合成字段,例如 String[] lastNameIndex
  2. 添加标记为 @PrePersist 的方法,该方法将使用所有内容填充 lastNameIndex 字段可用组合
  3. 当您想使用此索引查找实体时,请执行 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:

  1. add and synthetic field like String[] lastNameIndex
  2. add method marked as @PrePersist that will fill lastNameIndex field with all available combinations
  3. When you want to find entities using this index do query.filter('lastNameIndex =', val)
热风软妹 2024-12-05 16:04:27

将 Chris 的答案和 Nick 的评论放在一起,以下是为 objectify V4 构建查询过滤器的代码:

public <T> Query<T> fieldStartsWith(Query<T> query, String field, String search){
    query = query.filter(field + " >=", search);
    return query.filter(field + " <", searchStr+"\ufffd");
}

Putting the answer from Chris and the comment from Nick together, here is the code to build a query filter for objectify V4:

public <T> Query<T> fieldStartsWith(Query<T> query, String field, String search){
    query = query.filter(field + " >=", search);
    return query.filter(field + " <", searchStr+"\ufffd");
}
呆头 2024-12-05 16:04:27

我使用了标记化方法。下面是 Java 代码:

private String tokenize(String phrase) {
StringBuilder tokens = new StringBuilder();
try {
  for (String word : phrase.split(" ")) {
    if (word.length() < 1) {
      continue;
    }
    int j = 1;
    while (true) {
      for (int i = 0; i < word.length() - j + 1; i++) {
        tokens.append(word.substring(i, i + j)).append(" ");
      }
      if (j == word.length()) {
        break;
      }
      j++;
    }
  }
} catch (Throwable t) {
  t.printStackTrace();
}
return tokens.toString();}

这允许定义可索引字段,然后处理标准 Ofy 查询和 SearchService。

I have used the tokenization method. Here is the code in Java:

private String tokenize(String phrase) {
StringBuilder tokens = new StringBuilder();
try {
  for (String word : phrase.split(" ")) {
    if (word.length() < 1) {
      continue;
    }
    int j = 1;
    while (true) {
      for (int i = 0; i < word.length() - j + 1; i++) {
        tokens.append(word.substring(i, i + j)).append(" ");
      }
      if (j == word.length()) {
        break;
      }
      j++;
    }
  }
} catch (Throwable t) {
  t.printStackTrace();
}
return tokens.toString();}

This allows to define an indexable field, then process standard Ofy queries and SearchService.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文