JDOQL (Appengine) - 使用列表作为参数查询所有/一个项目?

发布于 2024-10-18 10:45:54 字数 658 浏览 3 评论 0原文

我正在尝试创建一个查询,以便给定列表(参数)的所有项目都包含在表的列(这也是一个列表)中。我还需要一个查询,以便给定列表(参数)中的至少一项包含在表的列中。例如:

JDO:

Table: User
| ID | Name | Interests <List of Strings> |

查询:

List <String> gifts; // Item to query with
  1. 如何查询兴趣与所有礼物匹配的所有用户?即所有礼物都应该是兴趣的子集。

  2. 如何查询兴趣与某些(至少一项)礼物相匹配的所有用户?即至少一件礼物是兴趣的子集。

  3. 如何查询所有兴趣都与礼物匹配的所有用户?即所有兴趣都应该是礼物的子集。

  4. 如何查询某些(至少一项)兴趣与礼物匹配的所有用户?即至少一种兴趣是礼物的子集。

这些查询可以吗?如果是这样那怎么办?我可以使用 .contains() 关键字来执行这些查询吗?如果是这样,那又如何呢?谁能分享一些例子吗?任何帮助将不胜感激。

谢谢。

I'm trying to create a query so that all items of a given list (parameter) are contained in a a table's column (which is also a list). I also need a query so that at least one item of a given list (parameter) are contained in a table's column. For example:

JDO:

Table: User
| ID | Name | Interests <List of Strings> |

Query:

List <String> gifts; // Item to query with
  1. How can I query for all users whose interests match ALL gifts? i.e. ALL of gifts should be a subset of Interests.

  2. How can I query for all users whose interests match SOME (at least one) gift? i.e. at least one gift is a subset of the interests.

  3. How can I query for all users whose ALL interests match gifts? i.e. ALL of interests should be a subset of gifts.

  4. How can I query for all users whose SOME (at least one) interests match gifts? i.e. at least one interest is a subset of the gifts.

Are these queries possible? If so then how? Can I use the .contains() keyword to do these queries? If so, then how? Can anyone share some examples? Any help would be highly appreciated.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ゝ杯具 2024-10-25 10:45:54

为了了解此类查询如何工作(或不工作),了解数据存储如何索引实体非常重要。当您插入具有列表属性的实体时,数据存储会将每个列表条目分解为单独的索引行。例如,以下实体:

Entity(User):
  id=15
  name="Jim"
  interests=["Drinking", "Banjos"]

将在自动索引中产生以下索引条目:

(User, "id", 15, Key("User", 1))
(User, "name", "Jim", Key("User", 1))
(User, "interests", "Drinking", Key("User", 1))
(User, "interests", "Banjos", Key("User", 1))

如果您还定义了(名称,兴趣)的复合索引,则其中的条目将如下所示:

("Jim", "Drinking", Key("User", 1))
("Jim", "Banjos", Key("User", 1))

建立后,我们可以按顺序处理您的特定查询:

  1. 这是不可能的,因为每个条目都位于其自己的索引行上。您可以通过为每个兴趣子集创建一个包含一个条目的列表来对此进行索引,并对其进行相等查询,但随着兴趣数量的增加,兴趣会迅速增长,因此如果您希望用户拥有超过,比如说,5 个利益,或者除非你可以对问题施加更多限制。
  2. 您可以使用“IN”过滤器 - 例如,“SELECT * FROM User WHERE Interests IN ["Drinking", "Banjos"]” - 这将匹配至少具有“Drinking”和“Banjos”之一作为兴趣的任何记录。请注意,这将被 SDK 分解为多个相等子查询,因此它相当于执行与查询列表中的条目一样多的查询。
  3. 这是 1 的反面。同样,这并不实际,除非您将完整的(排序的)兴趣列表存储为字符串,并且准备对礼物列表的每个子集执行单独的查询。
  4. 如果我没记错的话,这与 2 相同 - 您正在寻找两个集合之间的任何交集,这是一个对称操作。

对于数字 1 和 3,您可以通过进行更粗略的过滤(例如,搜索一两个兴趣)并过滤内存中的结果以获取精确匹配来完成。

In order to understand how these sort of queries work (or don't), it's important to understand how the datastore indexes entities. When you insert an entity with a list property, the datastore breaks out each list entry into a separate index row. For example, the following entity:

Entity(User):
  id=15
  name="Jim"
  interests=["Drinking", "Banjos"]

Will result in the following index entries in the automatic indexes:

(User, "id", 15, Key("User", 1))
(User, "name", "Jim", Key("User", 1))
(User, "interests", "Drinking", Key("User", 1))
(User, "interests", "Banjos", Key("User", 1))

If you've also defined a composite index on (name, interests), the entries in that will look something like this:

("Jim", "Drinking", Key("User", 1))
("Jim", "Banjos", Key("User", 1))

With that established, we can address your specific querying queries, in order:

  1. This isn't possible, because each entry is on its own index row. You could index this by creating a list with one entry for every subset of interests, and do an equality query on that but this grows rapidly as the number of interests increases, so it's probably a bad idea if you expect the user to have more than, say, 5 interests, or unless you can put more constraints on the problem.
  2. You can use an "IN" filter - eg, 'SELECT * FROM User WHERE Interests IN ["Drinking", "Banjos"]' - this will match any record that has at least one of "Drinking" and "Banjos" as interests. Note that this will be broken out into multiple equality subqueries by the SDK, so it's equivalent to executing as many queries as you have entries in the query list.
  3. This is the inverse of 1. Again, it's not really practical, unless you've stored the complete (sorted) list of interests as a string, and you're prepared to execute a separate query for every subset of your list of gifts.
  4. If I'm not mistaken, this is the same as 2 - you're looking for any intersection between the two sets, which is a symmetrical operation.

For numbers 1 and 3, you may be able to get by by doing a coarser filter - for instance, searching on one or two interests - and filtering the results in memory for exact matches.

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