From d80edca6e7bee31c7bdce38aa76097a6f633f388 Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:50:41 -0700 Subject: [PATCH 01/10] feat: add sample code for multiple inequalities indexing consideration query --- .../filters/IndexingConsiderationQuery.java | 68 +++++++++++++++++ .../IndexingConsiderationQuerySampleIT.java | 74 +++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java create mode 100644 samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java new file mode 100644 index 000000000..c8c6930c7 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java @@ -0,0 +1,68 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datastore.filters; + +// sample-metadata: +// title: Queries with indexing considerations +// description: The following query produces a result set that is ordered according to the index definition. + +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Query; +import com.google.cloud.datastore.QueryResults; +import com.google.cloud.datastore.StructuredQuery.CompositeFilter; +import com.google.cloud.datastore.StructuredQuery.Filter; +import com.google.cloud.datastore.StructuredQuery.PropertyFilter; + +public class IndexingConsiderationQuery { + public static void invoke() throws Exception { + + // Instantiates a client + Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + + // Build a query with multi inequal filters and optimized index order of index properties. + // [START datastore_query_indexing_considerations] + Query query = + Query.newEntityQueryBuilder() + .setKind("employees") + .setFilter( + CompositeFilter.and( + PropertyFilter.gt("salary", 100000), PropertyFilter.gt("experience", 0))) + .setOrderBy(OrderBy("salary"), OrderBy("experience")) + .build(); + // [END datastore_query_indexing_considerations] + + // Get the results back from Datastore + QueryResults results = datastore.run(query); + + if (!results.hasNext()) { + throw new Exception("query yielded no results"); + } + + while (results.hasNext()) { + Entity entity = results.next(); + System.out.printf("Entity: %s%n", entity); + } + + AggregationResult customer1SalesAvgQueryResult = + Iterables.getOnlyElement(datastore.runAggregation(customer1SalesAvg)); + + System.out.printf( + "Customer 1 sales avg is %d", customer1SalesAvgQueryResult.getLong("total_sales")); // 92 + } +} diff --git a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java new file mode 100644 index 000000000..477ed516e --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java @@ -0,0 +1,74 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datastore.filters; + +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; +import com.rule.SystemsOutRule; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class IndexingConsiderationQuerySampleIT { + + private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + + private Key employeeKey1; + private Key employeeKey2; + private Key employeeKey3; + + @Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule(); + + @Before + public void setUp() { + employeeKey1 = datastore.newKeyFactory().setKind("Employee").newKey("employee1"); + Entity employee1 = Entity.newBuilder(employeeKey1).set("name", "Alice").set("salary", 100001).set("experience", 10).build(); + + employeeKey2 = datastore.newKeyFactory().setKind("Employee").newKey("employee2"); + Entity employee2 = Entity.newBuilder(employeeKey2).set("name", "Bob").set("salary", 90000).set("experience", 5).build(); + + employeeKey3 = datastore.newKeyFactory().setKind("Employee").newKey("employee3"); + Entity employee3 = Entity.newBuilder(employeeKey3).set("name", "Jay").set("salary", 120000).set("experience", 15).build(); + + datastore.put(employee1); + datastore.put(employee2); + datastore.put(employee3); + } + + @After + public void tearDown() { + datastore.delete(employee1); + datastore.delete(employee2); + datastore.delete(employee3); + } + + @Test + public void testIndexingConsiderationQuery() throws Exception { + // Act + IndexingConsiderationQuery.invoke(); + + // Assert + systemsOutRule.assertContains("Entity"); + } +} From ed828b5a2a9c43ad8d48941c79ba0293ac75487c Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Tue, 17 Sep 2024 16:12:33 -0700 Subject: [PATCH 02/10] fix formatting --- .../filters/IndexingConsiderationQuery.java | 19 +++--- .../IndexingConsiderationQuerySampleIT.java | 61 ++++++++++--------- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java index c8c6930c7..68409814e 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java @@ -37,14 +37,11 @@ public static void invoke() throws Exception { // Build a query with multi inequal filters and optimized index order of index properties. // [START datastore_query_indexing_considerations] - Query query = - Query.newEntityQueryBuilder() - .setKind("employees") - .setFilter( - CompositeFilter.and( - PropertyFilter.gt("salary", 100000), PropertyFilter.gt("experience", 0))) - .setOrderBy(OrderBy("salary"), OrderBy("experience")) - .build(); + Query query = Query.newEntityQueryBuilder() + .setKind("employees") + .setFilter(CompositeFilter.and(PropertyFilter.gt("salary", 100000), PropertyFilter.gt("experience", 0))) + .setOrderBy(OrderBy("salary"), OrderBy("experience")) + .build(); // [END datastore_query_indexing_considerations] // Get the results back from Datastore @@ -59,10 +56,8 @@ public static void invoke() throws Exception { System.out.printf("Entity: %s%n", entity); } - AggregationResult customer1SalesAvgQueryResult = - Iterables.getOnlyElement(datastore.runAggregation(customer1SalesAvg)); + AggregationResult customer1SalesAvgQueryResult = Iterables.getOnlyElement(datastore.runAggregation(customer1SalesAvg)); - System.out.printf( - "Customer 1 sales avg is %d", customer1SalesAvgQueryResult.getLong("total_sales")); // 92 + System.out.printf("Customer 1 sales avg is %d", customer1SalesAvgQueryResult.getLong("total_sales")); // 92 } } diff --git a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java index 477ed516e..e75096c50 100644 --- a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java @@ -32,43 +32,44 @@ @SuppressWarnings("checkstyle:abbreviationaswordinname") public class IndexingConsiderationQuerySampleIT { - private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); - private Key employeeKey1; - private Key employeeKey2; - private Key employeeKey3; + private Key employeeKey1; + private Key employeeKey2; + private Key employeeKey3; - @Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule(); + @Rule + public final SystemsOutRule systemsOutRule = new SystemsOutRule(); - @Before - public void setUp() { - employeeKey1 = datastore.newKeyFactory().setKind("Employee").newKey("employee1"); - Entity employee1 = Entity.newBuilder(employeeKey1).set("name", "Alice").set("salary", 100001).set("experience", 10).build(); + @Before + public void setUp() { + employeeKey1 = datastore.newKeyFactory().setKind("Employee").newKey("employee1"); + Entity employee1 = Entity.newBuilder(employeeKey1).set("name", "Alice").set("salary", 100001).set("experience", 10).build(); - employeeKey2 = datastore.newKeyFactory().setKind("Employee").newKey("employee2"); - Entity employee2 = Entity.newBuilder(employeeKey2).set("name", "Bob").set("salary", 90000).set("experience", 5).build(); + employeeKey2 = datastore.newKeyFactory().setKind("Employee").newKey("employee2"); + Entity employee2 = Entity.newBuilder(employeeKey2).set("name", "Bob").set("salary", 90000).set("experience", 5).build(); - employeeKey3 = datastore.newKeyFactory().setKind("Employee").newKey("employee3"); - Entity employee3 = Entity.newBuilder(employeeKey3).set("name", "Jay").set("salary", 120000).set("experience", 15).build(); + employeeKey3 = datastore.newKeyFactory().setKind("Employee").newKey("employee3"); + Entity employee3 = Entity.newBuilder(employeeKey3).set("name", "Jay").set("salary", 120000).set("experience", 15).build(); - datastore.put(employee1); - datastore.put(employee2); - datastore.put(employee3); - } + datastore.put(employee1); + datastore.put(employee2); + datastore.put(employee3); + } - @After - public void tearDown() { - datastore.delete(employee1); - datastore.delete(employee2); - datastore.delete(employee3); - } + @After + public void tearDown() { + datastore.delete(employee1); + datastore.delete(employee2); + datastore.delete(employee3); + } - @Test - public void testIndexingConsiderationQuery() throws Exception { - // Act - IndexingConsiderationQuery.invoke(); + @Test + public void testIndexingConsiderationQuery() throws Exception { + // Act + IndexingConsiderationQuery.invoke(); - // Assert - systemsOutRule.assertContains("Entity"); - } + // Assert + systemsOutRule.assertContains("Entity"); + } } From fa81c2a8acc8f96fd2abfd1c9975166f6d3d4af5 Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Wed, 18 Sep 2024 10:21:58 -0700 Subject: [PATCH 03/10] fix formatting --- .../filters/IndexingConsiderationQuery.java | 49 +++++++------ .../IndexingConsiderationQuerySampleIT.java | 73 +++++++++++-------- 2 files changed, 67 insertions(+), 55 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java index 68409814e..170cd8591 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java @@ -18,7 +18,8 @@ // sample-metadata: // title: Queries with indexing considerations -// description: The following query produces a result set that is ordered according to the index definition. +// description: The following query produces a result set +// that is ordered according to the index definition. import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; @@ -30,34 +31,34 @@ import com.google.cloud.datastore.StructuredQuery.PropertyFilter; public class IndexingConsiderationQuery { - public static void invoke() throws Exception { + public static void invoke() throws Exception { - // Instantiates a client - Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + // Instantiates a client + Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); - // Build a query with multi inequal filters and optimized index order of index properties. - // [START datastore_query_indexing_considerations] - Query query = Query.newEntityQueryBuilder() - .setKind("employees") - .setFilter(CompositeFilter.and(PropertyFilter.gt("salary", 100000), PropertyFilter.gt("experience", 0))) - .setOrderBy(OrderBy("salary"), OrderBy("experience")) - .build(); - // [END datastore_query_indexing_considerations] + // Build a query with multi inequal filters and optimized index order of index properties. + // [START datastore_query_indexing_considerations] + Query query = Query.newEntityQueryBuilder() + .setKind("employees") + .setFilter(CompositeFilter.and(PropertyFilter.gt("salary", 100000), PropertyFilter.gt("experience", 0))) + .setOrderBy(OrderBy("salary"), OrderBy("experience")) + .build(); + // [END datastore_query_indexing_considerations] - // Get the results back from Datastore - QueryResults results = datastore.run(query); + // Get the results back from Datastore + QueryResults results = datastore.run(query); - if (!results.hasNext()) { - throw new Exception("query yielded no results"); - } + if (!results.hasNext()) { + throw new Exception("query yielded no results"); + } - while (results.hasNext()) { - Entity entity = results.next(); - System.out.printf("Entity: %s%n", entity); - } + while (results.hasNext()) { + Entity entity = results.next(); + System.out.printf("Entity: %s%n", entity); + } - AggregationResult customer1SalesAvgQueryResult = Iterables.getOnlyElement(datastore.runAggregation(customer1SalesAvg)); + AggregationResult customer1SalesAvgQueryResult = Iterables.getOnlyElement(datastore.runAggregation(customer1SalesAvg)); - System.out.printf("Customer 1 sales avg is %d", customer1SalesAvgQueryResult.getLong("total_sales")); // 92 - } + System.out.printf("Customer 1 sales avg is %d", customer1SalesAvgQueryResult.getLong("total_sales")); // 92 + } } diff --git a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java index e75096c50..6ddffb3bc 100644 --- a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java @@ -32,44 +32,55 @@ @SuppressWarnings("checkstyle:abbreviationaswordinname") public class IndexingConsiderationQuerySampleIT { - private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); - private Key employeeKey1; - private Key employeeKey2; - private Key employeeKey3; + private Key employeeKey1; + private Key employeeKey2; + private Key employeeKey3; - @Rule - public final SystemsOutRule systemsOutRule = new SystemsOutRule(); + @Rule + public final SystemsOutRule systemsOutRule = new SystemsOutRule(); - @Before - public void setUp() { - employeeKey1 = datastore.newKeyFactory().setKind("Employee").newKey("employee1"); - Entity employee1 = Entity.newBuilder(employeeKey1).set("name", "Alice").set("salary", 100001).set("experience", 10).build(); + @Before + public void setUp() { + employeeKey1 = datastore.newKeyFactory().setKind("Employee").newKey("employee1"); + Entity employee1 = Entity.newBuilder(employeeKey1) + .set("name", "Alice") + .set("salary", 100001) + .set("experience", 10) + .build(); - employeeKey2 = datastore.newKeyFactory().setKind("Employee").newKey("employee2"); - Entity employee2 = Entity.newBuilder(employeeKey2).set("name", "Bob").set("salary", 90000).set("experience", 5).build(); + employeeKey2 = datastore.newKeyFactory().setKind("Employee").newKey("employee2"); + Entity employee2 = Entity.newBuilder(employeeKey2) + .set("name", "Bob") + .set("salary", 90000) + .set("experience", 5) + .build(); - employeeKey3 = datastore.newKeyFactory().setKind("Employee").newKey("employee3"); - Entity employee3 = Entity.newBuilder(employeeKey3).set("name", "Jay").set("salary", 120000).set("experience", 15).build(); + employeeKey3 = datastore.newKeyFactory().setKind("Employee").newKey("employee3"); + Entity employee3 = Entity.newBuilder(employeeKey3) + .set("name", "Jay") + .set("salary", 120000) + .set("experience", 15).build(); - datastore.put(employee1); - datastore.put(employee2); - datastore.put(employee3); - } + datastore.put(employee1); + datastore.put(employee2); + datastore.put(employee3); + } - @After - public void tearDown() { - datastore.delete(employee1); - datastore.delete(employee2); - datastore.delete(employee3); - } + @After + public void tearDown() { + datastore.delete(employee1); + datastore.delete(employee2); + datastore.delete(employee3); + } - @Test - public void testIndexingConsiderationQuery() throws Exception { - // Act - IndexingConsiderationQuery.invoke(); + @Test + public void testIndexingConsiderationQuery() throws Exception { + // Act + IndexingConsiderationQuery.invoke(); - // Assert - systemsOutRule.assertContains("Entity"); - } + // Assert + systemsOutRule.assertContains("Entity"); + } } From 5f4a3837e3f470afd79dcc9486892897a11b0198 Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Wed, 18 Sep 2024 11:43:40 -0700 Subject: [PATCH 04/10] fix formatting --- .../datastore/filters/IndexingConsiderationQuery.java | 11 +++++------ .../filters/IndexingConsiderationQuerySampleIT.java | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java index 170cd8591..712021f73 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java @@ -29,6 +29,7 @@ import com.google.cloud.datastore.StructuredQuery.CompositeFilter; import com.google.cloud.datastore.StructuredQuery.Filter; import com.google.cloud.datastore.StructuredQuery.PropertyFilter; +import com.google.cloud.datastore.StructuredQuery.OrderBy; public class IndexingConsiderationQuery { public static void invoke() throws Exception { @@ -40,8 +41,10 @@ public static void invoke() throws Exception { // [START datastore_query_indexing_considerations] Query query = Query.newEntityQueryBuilder() .setKind("employees") - .setFilter(CompositeFilter.and(PropertyFilter.gt("salary", 100000), PropertyFilter.gt("experience", 0))) - .setOrderBy(OrderBy("salary"), OrderBy("experience")) + .setFilter(CompositeFilter.and( + PropertyFilter.gt("salary", 100000), + PropertyFilter.gt("experience", 0))) + .setOrderBy(OrderBy.asc("salary"), OrderBy.asc("experience")) .build(); // [END datastore_query_indexing_considerations] @@ -56,9 +59,5 @@ public static void invoke() throws Exception { Entity entity = results.next(); System.out.printf("Entity: %s%n", entity); } - - AggregationResult customer1SalesAvgQueryResult = Iterables.getOnlyElement(datastore.runAggregation(customer1SalesAvg)); - - System.out.printf("Customer 1 sales avg is %d", customer1SalesAvgQueryResult.getLong("total_sales")); // 92 } } diff --git a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java index 6ddffb3bc..7b2e390b2 100644 --- a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java @@ -70,9 +70,9 @@ public void setUp() { @After public void tearDown() { - datastore.delete(employee1); - datastore.delete(employee2); - datastore.delete(employee3); + datastore.delete(employeeKey1); + datastore.delete(employeeKey2); + datastore.delete(employeeKey3); } @Test From 4ff789f929be2ec6afdd0c77a12d91321bf2791d Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Wed, 18 Sep 2024 11:47:28 -0700 Subject: [PATCH 05/10] fix formatting --- .../example/datastore/filters/IndexingConsiderationQuery.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java index 712021f73..d8d2a66fe 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java @@ -28,8 +28,8 @@ import com.google.cloud.datastore.QueryResults; import com.google.cloud.datastore.StructuredQuery.CompositeFilter; import com.google.cloud.datastore.StructuredQuery.Filter; -import com.google.cloud.datastore.StructuredQuery.PropertyFilter; import com.google.cloud.datastore.StructuredQuery.OrderBy; +import com.google.cloud.datastore.StructuredQuery.PropertyFilter; public class IndexingConsiderationQuery { public static void invoke() throws Exception { From 0c3111c1e81b7332c08a074f69d262e2e9a75a61 Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:53:47 -0700 Subject: [PATCH 06/10] Add index --- samples/snippets/src/test/resources/index.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/samples/snippets/src/test/resources/index.yaml b/samples/snippets/src/test/resources/index.yaml index 8652a0ee0..ca5afd4aa 100644 --- a/samples/snippets/src/test/resources/index.yaml +++ b/samples/snippets/src/test/resources/index.yaml @@ -23,3 +23,7 @@ indexes: properties: - name: tag - name: tag +- kind: employees + properties: + - name: salary + - name: experience From c23fae4af139be97d02e2b0f77ae67ff6fbb7a9f Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:51:17 -0700 Subject: [PATCH 07/10] Correct indexes --- .../example/datastore/filters/IndexingConsiderationQuery.java | 2 +- .../datastore/filters/IndexingConsiderationQuerySampleIT.java | 3 ++- samples/snippets/src/test/resources/index.yaml | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java index d8d2a66fe..95f75d4cd 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java @@ -40,7 +40,7 @@ public static void invoke() throws Exception { // Build a query with multi inequal filters and optimized index order of index properties. // [START datastore_query_indexing_considerations] Query query = Query.newEntityQueryBuilder() - .setKind("employees") + .setKind("Employee") .setFilter(CompositeFilter.and( PropertyFilter.gt("salary", 100000), PropertyFilter.gt("experience", 0))) diff --git a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java index 7b2e390b2..cf6cf509c 100644 --- a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java @@ -61,7 +61,8 @@ public void setUp() { Entity employee3 = Entity.newBuilder(employeeKey3) .set("name", "Jay") .set("salary", 120000) - .set("experience", 15).build(); + .set("experience", 15) + .build(); datastore.put(employee1); datastore.put(employee2); diff --git a/samples/snippets/src/test/resources/index.yaml b/samples/snippets/src/test/resources/index.yaml index ca5afd4aa..4d6e56b8b 100644 --- a/samples/snippets/src/test/resources/index.yaml +++ b/samples/snippets/src/test/resources/index.yaml @@ -23,7 +23,7 @@ indexes: properties: - name: tag - name: tag -- kind: employees +- kind: Employee properties: - name: salary - - name: experience + - name: experience \ No newline at end of file From eb7bd5b1639e40f32441ba04e085c86dffa157a7 Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:37:10 -0700 Subject: [PATCH 08/10] Add orderfileds query --- .../filters/IndexingConsiderationQuery.java | 2 +- .../datastore/filters/OrderFieldsQuery.java | 62 +++++++++++++++++++ ...pleIT.java => MultiIneqQuerySampleIT.java} | 17 +++-- .../snippets/src/test/resources/index.yaml | 2 +- 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java rename samples/snippets/src/test/java/com/example/datastore/filters/{IndexingConsiderationQuerySampleIT.java => MultiIneqQuerySampleIT.java} (83%) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java index 95f75d4cd..d8d2a66fe 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java @@ -40,7 +40,7 @@ public static void invoke() throws Exception { // Build a query with multi inequal filters and optimized index order of index properties. // [START datastore_query_indexing_considerations] Query query = Query.newEntityQueryBuilder() - .setKind("Employee") + .setKind("employees") .setFilter(CompositeFilter.and( PropertyFilter.gt("salary", 100000), PropertyFilter.gt("experience", 0))) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java new file mode 100644 index 000000000..41c2d9b80 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java @@ -0,0 +1,62 @@ +/* + * Copyright 2024 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datastore.filters; + +// sample-metadata: +// title: Queries with order fileds +// description: The following query order properties +// in the decreasing order of query constraint selectivity. + +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Query; +import com.google.cloud.datastore.QueryResults; +import com.google.cloud.datastore.StructuredQuery.Filter; +import com.google.cloud.datastore.StructuredQuery.OrderBy; +import com.google.cloud.datastore.StructuredQuery.PropertyFilter; + +public class OrderFieldsQuery { + public static void invoke() throws Exception { + + // Instantiates a client + Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + + // Build a query with order properties in the decreasing order of query constraint selectivity. + // [START datastore_query_order_fields] + Query query = + Query.newEntityQueryBuilder() + .setKind("employees") + .setFilter(PropertyFilter.gt("salary", 100000)) + .setOrderBy(OrderBy("salary")) + .build(); + + // Get the results back from Datastore + QueryResults results = datastore.run(query); + // Order results by `experience` + // [END datastore_query_order_fields] + + if (!results.hasNext()) { + throw new Exception("query yielded no results"); + } + + while (results.hasNext()) { + Entity entity = results.next(); + System.out.printf("Entity: %s%n", entity); + } + } +} diff --git a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java b/samples/snippets/src/test/java/com/example/datastore/filters/MultiIneqQuerySampleIT.java similarity index 83% rename from samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java rename to samples/snippets/src/test/java/com/example/datastore/filters/MultiIneqQuerySampleIT.java index cf6cf509c..6fee2a806 100644 --- a/samples/snippets/src/test/java/com/example/datastore/filters/IndexingConsiderationQuerySampleIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/filters/MultiIneqQuerySampleIT.java @@ -30,7 +30,7 @@ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class IndexingConsiderationQuerySampleIT { +public class MultiIneqQuerySampleIT { private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); @@ -43,21 +43,21 @@ public class IndexingConsiderationQuerySampleIT { @Before public void setUp() { - employeeKey1 = datastore.newKeyFactory().setKind("Employee").newKey("employee1"); + employeeKey1 = datastore.newKeyFactory().setKind("employees").newKey("employee1"); Entity employee1 = Entity.newBuilder(employeeKey1) .set("name", "Alice") .set("salary", 100001) .set("experience", 10) .build(); - employeeKey2 = datastore.newKeyFactory().setKind("Employee").newKey("employee2"); + employeeKey2 = datastore.newKeyFactory().setKind("employees").newKey("employee2"); Entity employee2 = Entity.newBuilder(employeeKey2) .set("name", "Bob") .set("salary", 90000) .set("experience", 5) .build(); - employeeKey3 = datastore.newKeyFactory().setKind("Employee").newKey("employee3"); + employeeKey3 = datastore.newKeyFactory().setKind("employees").newKey("employee3"); Entity employee3 = Entity.newBuilder(employeeKey3) .set("name", "Jay") .set("salary", 120000) @@ -84,4 +84,13 @@ public void testIndexingConsiderationQuery() throws Exception { // Assert systemsOutRule.assertContains("Entity"); } + + @Test + public void testOrderFieldsQuery() throws Exception { + // Act + OrderFieldsQuery.invoke(); + + // Assert + systemsOutRule.assertContains("Entity"); + } } diff --git a/samples/snippets/src/test/resources/index.yaml b/samples/snippets/src/test/resources/index.yaml index 4d6e56b8b..5f2f0c74a 100644 --- a/samples/snippets/src/test/resources/index.yaml +++ b/samples/snippets/src/test/resources/index.yaml @@ -23,7 +23,7 @@ indexes: properties: - name: tag - name: tag -- kind: Employee +- kind: employees properties: - name: salary - name: experience \ No newline at end of file From e0f54f3195dd4e2ef40d943b4b4262fe45cd4e1a Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:44:55 -0700 Subject: [PATCH 09/10] fix orderby asc --- .../java/com/example/datastore/filters/OrderFieldsQuery.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java index 41c2d9b80..6173d452a 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java @@ -42,7 +42,7 @@ public static void invoke() throws Exception { Query.newEntityQueryBuilder() .setKind("employees") .setFilter(PropertyFilter.gt("salary", 100000)) - .setOrderBy(OrderBy("salary")) + .setOrderBy(OrderBy.asc("salary")) .build(); // Get the results back from Datastore From db8d766a4cf4414042bdf3c9078028a9f71fe510 Mon Sep 17 00:00:00 2001 From: Cindy Peng <148148319+cindy-peng@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:52:46 -0700 Subject: [PATCH 10/10] Move region tag to include import statements --- .../datastore/filters/IndexingConsiderationQuery.java | 5 +++-- .../java/com/example/datastore/filters/OrderFieldsQuery.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java index d8d2a66fe..f0c9cd5da 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/IndexingConsiderationQuery.java @@ -21,6 +21,8 @@ // description: The following query produces a result set // that is ordered according to the index definition. +// [START datastore_query_indexing_considerations] + import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.datastore.Entity; @@ -38,7 +40,6 @@ public static void invoke() throws Exception { Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); // Build a query with multi inequal filters and optimized index order of index properties. - // [START datastore_query_indexing_considerations] Query query = Query.newEntityQueryBuilder() .setKind("employees") .setFilter(CompositeFilter.and( @@ -46,7 +47,6 @@ public static void invoke() throws Exception { PropertyFilter.gt("experience", 0))) .setOrderBy(OrderBy.asc("salary"), OrderBy.asc("experience")) .build(); - // [END datastore_query_indexing_considerations] // Get the results back from Datastore QueryResults results = datastore.run(query); @@ -61,3 +61,4 @@ public static void invoke() throws Exception { } } } +// [END datastore_query_indexing_considerations] diff --git a/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java b/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java index 6173d452a..1739dbade 100644 --- a/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java +++ b/samples/snippets/src/main/java/com/example/datastore/filters/OrderFieldsQuery.java @@ -21,6 +21,8 @@ // description: The following query order properties // in the decreasing order of query constraint selectivity. +// [START datastore_query_order_fields] + import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.datastore.Entity; @@ -37,7 +39,6 @@ public static void invoke() throws Exception { Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); // Build a query with order properties in the decreasing order of query constraint selectivity. - // [START datastore_query_order_fields] Query query = Query.newEntityQueryBuilder() .setKind("employees") @@ -48,7 +49,6 @@ public static void invoke() throws Exception { // Get the results back from Datastore QueryResults results = datastore.run(query); // Order results by `experience` - // [END datastore_query_order_fields] if (!results.hasNext()) { throw new Exception("query yielded no results"); @@ -60,3 +60,4 @@ public static void invoke() throws Exception { } } } +// [END datastore_query_order_fields]