use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatchedEntities.
@Test
public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatchedEntities() throws Exception {
Entity a = new Entity("Widget", "a");
a.setProperty("x", ImmutableList.<Long>of(1L, 2L));
Entity b = new Entity("Widget", "b");
b.setProperty("x", ImmutableList.<Long>of(1L, 2L, 3L));
datastore.put(ImmutableList.<Entity>of(a, b));
// [START surprising_behavior_example_4]
Query q = new Query("Widget").setFilter(CompositeFilterOperator.and(new FilterPredicate("x", FilterOperator.NOT_EQUAL, 1), new FilterPredicate("x", FilterOperator.NOT_EQUAL, 2)));
// [END surprising_behavior_example_4]
// The two NOT_EQUAL filters in the query become like the combination of queries:
// x < 1 OR (x > 1 AND x < 2) OR x > 2
//
// Only "b" has some value which matches the "x > 2" portion of this query.
//
// See the documentation for more details:
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(b);
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities.
@Test
public void ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities() throws Exception {
// [START kindless_ancestor_query_example]
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity tom = new Entity("Person", "Tom");
Key tomKey = tom.getKey();
datastore.put(tom);
Entity weddingPhoto = new Entity("Photo", tomKey);
weddingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/wedding_photo.jpg");
Entity weddingVideo = new Entity("Video", tomKey);
weddingVideo.setProperty("videoURL", "http://domain.com/some/path/to/wedding_video.avi");
List<Entity> mediaList = Arrays.asList(weddingPhoto, weddingVideo);
datastore.put(mediaList);
// By default, ancestor queries include the specified ancestor itself.
// The following filter excludes the ancestor from the query results.
Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, tomKey);
Query mediaQuery = new Query().setAncestor(tomKey).setFilter(keyFilter);
// Returns both weddingPhoto and weddingVideo,
// even though they are of different entity kinds
List<Entity> results = datastore.prepare(mediaQuery).asList(FetchOptions.Builder.withDefaults());
// [END kindless_ancestor_query_example]
assertThat(results).named("query result keys").containsExactly(weddingPhoto, weddingVideo);
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryRestrictions_surprisingMultipleValuesEquals_returnsMatchedEntities.
@Test
public void queryRestrictions_surprisingMultipleValuesEquals_returnsMatchedEntities() throws Exception {
Entity a = new Entity("Widget", "a");
a.setProperty("x", ImmutableList.<Long>of(1L, 2L));
Entity b = new Entity("Widget", "b");
b.setProperty("x", ImmutableList.<Long>of(1L, 3L));
Entity c = new Entity("Widget", "c");
c.setProperty("x", ImmutableList.<Long>of(-6L, 2L));
Entity d = new Entity("Widget", "d");
d.setProperty("x", ImmutableList.<Long>of(-6L, 4L));
Entity e = new Entity("Widget", "e");
e.setProperty("x", ImmutableList.<Long>of(1L, 2L, 3L));
datastore.put(ImmutableList.<Entity>of(a, b, c, d, e));
// [START surprising_behavior_example_2]
Query q = new Query("Widget").setFilter(CompositeFilterOperator.and(new FilterPredicate("x", FilterOperator.EQUAL, 1), new FilterPredicate("x", FilterOperator.EQUAL, 2)));
// [END surprising_behavior_example_2]
// Only "a" and "e" have both 1 and 2 in the "x" array-valued property.
// See the documentation for more details:
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(a, e);
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryRestrictions_surprisingMultipleValuesAllMustMatch_returnsNoEntities.
@Test
public void queryRestrictions_surprisingMultipleValuesAllMustMatch_returnsNoEntities() throws Exception {
Entity a = new Entity("Widget", "a");
List<Long> xs = Arrays.asList(1L, 2L);
a.setProperty("x", xs);
datastore.put(a);
// [START surprising_behavior_example_1]
Query q = new Query("Widget").setFilter(CompositeFilterOperator.and(new FilterPredicate("x", FilterOperator.GREATER_THAN, 1), new FilterPredicate("x", FilterOperator.LESS_THAN, 2)));
// [END surprising_behavior_example_1]
// Entity "a" will not match because no individual value matches all filters.
// See the documentation for more details:
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").isEmpty();
}
use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryInterface_singleFilter_returnsMatchedEntities.
@Test
public void queryInterface_singleFilter_returnsMatchedEntities() throws Exception {
// Arrange
Entity a = new Entity("Person", "a");
a.setProperty("height", 100);
Entity b = new Entity("Person", "b");
b.setProperty("height", 150);
Entity c = new Entity("Person", "c");
c.setProperty("height", 300);
datastore.put(ImmutableList.<Entity>of(a, b, c));
// Act
long minHeight = 150;
// [START interface_2]
Filter heightMinFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
Query q = new Query("Person").setFilter(heightMinFilter);
// [END interface_2]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(b, c);
}
Aggregations