Java Code Examples for com.google.appengine.api.datastore.Query.FilterOperator#GREATER_THAN

The following examples show how to use com.google.appengine.api.datastore.Query.FilterOperator#GREATER_THAN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 9 votes vote down vote up
@Test
public void queryInterface_orFilter_printsMatchedEntities() 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", 200);
  datastore.put(ImmutableList.<Entity>of(a, b, c));

  StringWriter buf = new StringWriter();
  PrintWriter out = new PrintWriter(buf);
  long minHeight = 125;
  long maxHeight = 175;

  // Act
  // [START gae_java8_datastore_interface_3]
  Filter tooShortFilter = new FilterPredicate("height", FilterOperator.LESS_THAN, minHeight);

  Filter tooTallFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN, maxHeight);

  Filter heightOutOfRangeFilter = CompositeFilterOperator.or(tooShortFilter, tooTallFilter);

  Query q = new Query("Person").setFilter(heightOutOfRangeFilter);
  // [END gae_java8_datastore_interface_3]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(a, c);
}
 
Example 2
Source File: IndexesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyFilterExample_returnsMatchingEntities() throws Exception {
  // [START unindexed_properties_1]
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

  Key acmeKey = KeyFactory.createKey("Company", "Acme");

  Entity tom = new Entity("Person", "Tom", acmeKey);
  tom.setProperty("name", "Tom");
  tom.setProperty("age", 32);
  datastore.put(tom);

  Entity lucy = new Entity("Person", "Lucy", acmeKey);
  lucy.setProperty("name", "Lucy");
  lucy.setUnindexedProperty("age", 29);
  datastore.put(lucy);

  Filter ageFilter = new FilterPredicate("age", FilterOperator.GREATER_THAN, 25);

  Query q = new Query("Person").setAncestor(acmeKey).setFilter(ageFilter);

  // Returns tom but not lucy, because her age is unindexed
  List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
  // [END unindexed_properties_1]

  assertWithMessage("query results").that(results).containsExactly(tom);
}
 
Example 3
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void keyFilterExample_returnsMatchingEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Person", "a");
  Entity b = new Entity("Person", "b");
  Entity c = new Entity("Person", "c");
  Entity aa = new Entity("Person", "aa", b.getKey());
  Entity bb = new Entity("Person", "bb", b.getKey());
  Entity aaa = new Entity("Person", "aaa", bb.getKey());
  Entity bbb = new Entity("Person", "bbb", bb.getKey());
  datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb));

  // Act
  Key lastSeenKey = bb.getKey();
  // [START gae_java8_datastore_key_filter]
  Filter keyFilter =
      new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
  Query q = new Query("Person").setFilter(keyFilter);
  // [END gae_java8_datastore_key_filter]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results")
      .that(results)
      .containsExactly(
          aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb".
          bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb".
          c); // Key name identifier "c" is greater than b.
}
 
Example 4
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Child", "a");
  Entity b = new Entity("Child", "b");
  Entity c = new Entity("Child", "c");
  Entity aa = new Entity("Child", "aa", b.getKey());
  Entity bb = new Entity("Child", "bb", b.getKey());
  Entity aaa = new Entity("Child", "aaa", bb.getKey());
  Entity bbb = new Entity("Child", "bbb", bb.getKey());
  Entity adult = new Entity("Adult", "a");
  Entity zooAnimal = new Entity("ZooAnimal", "a");
  datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb, adult, zooAnimal));

  // Act
  Key lastSeenKey = bb.getKey();
  // [START gae_java8_datastore_kindless_query]
  Filter keyFilter =
      new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
  Query q = new Query().setFilter(keyFilter);
  // [END gae_java8_datastore_kindless_query]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results")
      .that(results)
      .containsExactly(
          aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb".
          bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb".
          zooAnimal, // Kind "ZooAnimal" is greater than "Child"
          c); // Key name identifier "c" is greater than b.
}
 
Example 5
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Grandparent", "a");
  Entity b = new Entity("Grandparent", "b");
  Entity c = new Entity("Grandparent", "c");
  Entity aa = new Entity("Parent", "aa", a.getKey());
  Entity ba = new Entity("Parent", "ba", b.getKey());
  Entity bb = new Entity("Parent", "bb", b.getKey());
  Entity bc = new Entity("Parent", "bc", b.getKey());
  Entity cc = new Entity("Parent", "cc", c.getKey());
  Entity aaa = new Entity("Child", "aaa", aa.getKey());
  Entity bbb = new Entity("Child", "bbb", bb.getKey());
  datastore.put(ImmutableList.<Entity>of(a, b, c, aa, ba, bb, bc, cc, aaa, bbb));

  // Act
  Key ancestorKey = b.getKey();
  Key lastSeenKey = bb.getKey();
  // [START gae_java8_datastore_kindless_ancestor_key_query]
  Filter keyFilter =
      new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
  Query q = new Query().setAncestor(ancestorKey).setFilter(keyFilter);
  // [END gae_java8_datastore_kindless_ancestor_key_query]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(bc, bbb);
}
 
Example 6
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities()
    throws Exception {
  // [START gae_java8_datastore_kindless_ancestor_query]
  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 gae_java8_datastore_kindless_ancestor_query]

  assertWithMessage("query result keys")
      .that(results)
      .containsExactly(weddingPhoto, weddingVideo);
}