Skip to content

Commit e319efa

Browse files
authored
docs: adds OR filter sample (#1032)
* docs: adds OR filter sample
1 parent 13a48f4 commit e319efa

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-datastore/tre
268268
| Count Aggregation With Order By | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithOrderBy.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithOrderBy.java) |
269269
| Count Aggregation With Property Filter | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithPropertyFilter.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithPropertyFilter.java) |
270270
| Count Aggregation With Stale Read | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithStaleRead.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithStaleRead.java) |
271+
| Create a union between two filters | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/filters/OrFilterQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/filters/OrFilterQuery.java) |
271272
| Task List | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/google/datastore/snippets/TaskList.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/google/datastore/snippets/TaskList.java) |
272273

273274

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore.filters;
18+
19+
// sample-metadata:
20+
// title: Create a union between two filters
21+
// description: Create a union between two filters (logical OR operator)
22+
23+
// [START datastore_query_filter_or]
24+
import com.google.cloud.datastore.Datastore;
25+
import com.google.cloud.datastore.DatastoreOptions;
26+
import com.google.cloud.datastore.Entity;
27+
import com.google.cloud.datastore.Query;
28+
import com.google.cloud.datastore.QueryResults;
29+
import com.google.cloud.datastore.StructuredQuery.CompositeFilter;
30+
import com.google.cloud.datastore.StructuredQuery.Filter;
31+
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
32+
33+
public class OrFilterQuery {
34+
public static void invoke() throws Exception {
35+
36+
// Instantiates a client
37+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
38+
String propertyName = "description";
39+
40+
// Create the two filters
41+
Filter orFilter =
42+
CompositeFilter.or(
43+
PropertyFilter.eq(propertyName, "Feed cats"),
44+
PropertyFilter.eq(propertyName, "Buy milk"));
45+
46+
// Build the query
47+
Query<Entity> query = Query.newEntityQueryBuilder().setKind("Task").setFilter(orFilter).build();
48+
49+
// Get the results back from Datastore
50+
QueryResults<Entity> results = datastore.run(query);
51+
52+
if (!results.hasNext()) {
53+
throw new Exception("query yielded no results");
54+
}
55+
56+
while (results.hasNext()) {
57+
Entity entity = results.next();
58+
System.out.printf("Entity: %s%n", entity);
59+
}
60+
}
61+
}
62+
// [END datastore_query_filter_or]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datastore;
18+
19+
import com.example.datastore.filters.OrFilterQuery;
20+
import com.google.cloud.datastore.Datastore;
21+
import com.google.cloud.datastore.DatastoreOptions;
22+
import com.google.cloud.datastore.Entity;
23+
import com.google.cloud.datastore.Key;
24+
import com.rule.SystemsOutRule;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Rule;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
@RunWith(JUnit4.class)
33+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
34+
public class OrFilterQuerySampleIT {
35+
36+
private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
37+
private final String fieldName = "description";
38+
39+
private Key taskKey1;
40+
private Key taskKey2;
41+
42+
@Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule();
43+
44+
@Before
45+
public void setUp() {
46+
taskKey1 = datastore.newKeyFactory().setKind("Task").newKey("sampleTask");
47+
Entity task1 = Entity.newBuilder(taskKey1).set(fieldName, "Buy milk").build();
48+
49+
taskKey2 = datastore.newKeyFactory().setKind("Task").newKey("sampleTask2");
50+
Entity task2 = Entity.newBuilder(taskKey2).set(fieldName, "Feed cats").build();
51+
52+
datastore.put(task1);
53+
datastore.put(task2);
54+
}
55+
56+
@After
57+
public void tearDown() {
58+
datastore.delete(taskKey1);
59+
datastore.delete(taskKey2);
60+
}
61+
62+
@Test
63+
public void testOrFilterQuery() throws Exception {
64+
// Act
65+
OrFilterQuery.invoke();
66+
67+
// Assert
68+
systemsOutRule.assertContains("Entity");
69+
}
70+
}

0 commit comments

Comments
 (0)