Search in sources :

Example 11 with Filter

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

the class IndexesTest method propertyFilterExample_returnsMatchingEntities.

@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]
    assertThat(results).named("query results").containsExactly(tom);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) Filter(com.google.appengine.api.datastore.Query.Filter) 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 12 with Filter

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

the class MetadataNamespacesTest method getNamespaces.

// [START namespace_query_example]
List<String> getNamespaces(DatastoreService ds, String start, String end) {
    // Start with unrestricted namespace query
    Query q = new Query(Entities.NAMESPACE_METADATA_KIND);
    List<Filter> subFilters = new ArrayList();
    // Limit to specified range, if any
    if (start != null) {
        subFilters.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createNamespaceKey(start)));
    }
    if (end != null) {
        subFilters.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, Entities.createNamespaceKey(end)));
    }
    q.setFilter(CompositeFilterOperator.and(subFilters));
    // Initialize result list
    List<String> results = new ArrayList<String>();
    // Build list of query results
    for (Entity e : ds.prepare(q).asIterable()) {
        results.add(Entities.getNamespaceFromNamespaceKey(e.getKey()));
    }
    // Return result list
    return results;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) Filter(com.google.appengine.api.datastore.Query.Filter) ArrayList(java.util.ArrayList) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate)

Example 13 with Filter

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

the class MetadataKindsTest method printLowercaseKinds.

// [START kind_query_example]
void printLowercaseKinds(DatastoreService ds, PrintWriter writer) {
    // Start with unrestricted kind query
    Query q = new Query(Entities.KIND_METADATA_KIND);
    List<Filter> subFils = new ArrayList();
    // Limit to lowercase initial letters
    subFils.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createKindKey("a")));
    // Character after 'z'
    String endChar = Character.toString((char) ('z' + 1));
    subFils.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN, Entities.createKindKey(endChar)));
    q.setFilter(CompositeFilterOperator.and(subFils));
    // Print heading
    writer.println("Lowercase kinds:");
    // Print query results
    for (Entity e : ds.prepare(q).asIterable()) {
        writer.println("  " + e.getKey().getName());
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) Filter(com.google.appengine.api.datastore.Query.Filter) ArrayList(java.util.ArrayList) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate)

Example 14 with Filter

use of com.google.appengine.api.datastore.Query.Filter in project Cached-Datastore by Emperorlou.

the class QueryHelper method getFilteredList_Keys.

public List<Key> getFilteredList_Keys(String kind, int limit, String fieldName, FilterOperator operator, Object equalToValue, String fieldName2, FilterOperator operator2, Object equalToValue2) {
    FilterPredicate f1 = new FilterPredicate(fieldName, operator, equalToValue);
    FilterPredicate f2 = new FilterPredicate(fieldName2, operator2, equalToValue2);
    Filter filter = CompositeFilterOperator.and(f1, f2);
    return ds.fetchAsList_Keys(kind, filter, 1000);
}
Also used : CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) Filter(com.google.appengine.api.datastore.Query.Filter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate)

Example 15 with Filter

use of com.google.appengine.api.datastore.Query.Filter in project Cached-Datastore by Emperorlou.

the class QueryHelper method getFilteredORList.

public List<CachedEntity> getFilteredORList(Cursor cursor, String kind, String fieldName, Object equalToValue, String fieldName2, Object equalToValue2, String fieldName3, Object equalToValue3) {
    FilterPredicate f1 = new FilterPredicate(fieldName, FilterOperator.EQUAL, equalToValue);
    FilterPredicate f2 = new FilterPredicate(fieldName2, FilterOperator.EQUAL, equalToValue2);
    FilterPredicate f3 = new FilterPredicate(fieldName3, FilterOperator.EQUAL, equalToValue3);
    Filter filter = CompositeFilterOperator.or(f1, f2, f3);
    Query q = new Query(kind);
    q.setFilter(filter);
    return ds.fetchAsList(q, 1000, cursor);
}
Also used : Query(com.google.appengine.api.datastore.Query) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) Filter(com.google.appengine.api.datastore.Query.Filter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate)

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