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
How can I query for all users whose interests match ALL gifts? i.e. ALL of gifts should be a subset of Interests.
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.
How can I query for all users whose ALL interests match gifts? i.e. ALL of interests should be a subset of gifts.
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.
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:
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.