Search in sources :

Example 1 with Filter

use of com.google.appengine.api.datastore.Query.Filter 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);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) DatastoreService(com.google.appengine.api.datastore.DatastoreService) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 2 with Filter

use of com.google.appengine.api.datastore.Query.Filter 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);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 3 with Filter

use of com.google.appengine.api.datastore.Query.Filter in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method keyFilterExample_returnsMatchingEntities.

@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 key_filter_example]
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
    Query q = new Query("Person").setFilter(keyFilter);
    // [END key_filter_example]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(// Ancestor path "b/bb/aaa" is greater than "b/bb".
    aaa, // Ancestor path "b/bb/bbb" is greater than "b/bb".
    bbb, // Key name identifier "c" is greater than b.
    c);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 4 with Filter

use of com.google.appengine.api.datastore.Query.Filter in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryInterface_orFilter_printsMatchedEntities.

@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 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 interface_3]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(a, c);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) StringWriter(java.io.StringWriter) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 5 with Filter

use of com.google.appengine.api.datastore.Query.Filter in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryRestrictions_missingSortOnInequality_isInvalid.

@Test
public void queryRestrictions_missingSortOnInequality_isInvalid() throws Exception {
    long minBirthYear = 1940;
    // [START inequality_filters_sort_orders_invalid_example_1]
    Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
    // Not valid. Missing sort on birthYear.
    Query q = new Query("Person").setFilter(birthYearMinFilter).addSort("lastName", SortDirection.ASCENDING);
// [END inequality_filters_sort_orders_invalid_example_1]
// Note: The local devserver behavior is different than the production
// version of Cloud Datastore, so there aren't any assertions we can make
// in this test.  The query appears to work with the local test runner,
// but will fail in production.
}
Also used : Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Aggregations

Filter (com.google.appengine.api.datastore.Query.Filter)24 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)24 Query (com.google.appengine.api.datastore.Query)20 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)20 Test (org.junit.Test)16 Entity (com.google.appengine.api.datastore.Entity)15 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)14 Key (com.google.appengine.api.datastore.Key)5 DatastoreService (com.google.appengine.api.datastore.DatastoreService)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2