Skip to content

Commit 1286792

Browse files
authoredSep 25, 2024··
feat: add sample code for multiple inequalities indexing consideration query (#1579)
* feat: add sample code for multiple inequalities indexing consideration query * fix formatting * fix formatting * fix formatting * fix formatting * Add index * Correct indexes * Add orderfileds query * fix orderby asc * Move region tag to include import statements
1 parent 5d078a4 commit 1286792

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2024 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: Queries with indexing considerations
21+
// description: The following query produces a result set
22+
// that is ordered according to the index definition.
23+
24+
// [START datastore_query_indexing_considerations]
25+
26+
import com.google.cloud.datastore.Datastore;
27+
import com.google.cloud.datastore.DatastoreOptions;
28+
import com.google.cloud.datastore.Entity;
29+
import com.google.cloud.datastore.Query;
30+
import com.google.cloud.datastore.QueryResults;
31+
import com.google.cloud.datastore.StructuredQuery.CompositeFilter;
32+
import com.google.cloud.datastore.StructuredQuery.Filter;
33+
import com.google.cloud.datastore.StructuredQuery.OrderBy;
34+
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
35+
36+
public class IndexingConsiderationQuery {
37+
public static void invoke() throws Exception {
38+
39+
// Instantiates a client
40+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
41+
42+
// Build a query with multi inequal filters and optimized index order of index properties.
43+
Query<Entity> query = Query.newEntityQueryBuilder()
44+
.setKind("employees")
45+
.setFilter(CompositeFilter.and(
46+
PropertyFilter.gt("salary", 100000),
47+
PropertyFilter.gt("experience", 0)))
48+
.setOrderBy(OrderBy.asc("salary"), OrderBy.asc("experience"))
49+
.build();
50+
51+
// Get the results back from Datastore
52+
QueryResults<Entity> results = datastore.run(query);
53+
54+
if (!results.hasNext()) {
55+
throw new Exception("query yielded no results");
56+
}
57+
58+
while (results.hasNext()) {
59+
Entity entity = results.next();
60+
System.out.printf("Entity: %s%n", entity);
61+
}
62+
}
63+
}
64+
// [END datastore_query_indexing_considerations]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 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: Queries with order fileds
21+
// description: The following query order properties
22+
// in the decreasing order of query constraint selectivity.
23+
24+
// [START datastore_query_order_fields]
25+
26+
import com.google.cloud.datastore.Datastore;
27+
import com.google.cloud.datastore.DatastoreOptions;
28+
import com.google.cloud.datastore.Entity;
29+
import com.google.cloud.datastore.Query;
30+
import com.google.cloud.datastore.QueryResults;
31+
import com.google.cloud.datastore.StructuredQuery.Filter;
32+
import com.google.cloud.datastore.StructuredQuery.OrderBy;
33+
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
34+
35+
public class OrderFieldsQuery {
36+
public static void invoke() throws Exception {
37+
38+
// Instantiates a client
39+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
40+
41+
// Build a query with order properties in the decreasing order of query constraint selectivity.
42+
Query<Entity> query =
43+
Query.newEntityQueryBuilder()
44+
.setKind("employees")
45+
.setFilter(PropertyFilter.gt("salary", 100000))
46+
.setOrderBy(OrderBy.asc("salary"))
47+
.build();
48+
49+
// Get the results back from Datastore
50+
QueryResults<Entity> results = datastore.run(query);
51+
// Order results by `experience`
52+
53+
if (!results.hasNext()) {
54+
throw new Exception("query yielded no results");
55+
}
56+
57+
while (results.hasNext()) {
58+
Entity entity = results.next();
59+
System.out.printf("Entity: %s%n", entity);
60+
}
61+
}
62+
}
63+
// [END datastore_query_order_fields]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2024 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+
import com.google.cloud.datastore.Datastore;
20+
import com.google.cloud.datastore.DatastoreOptions;
21+
import com.google.cloud.datastore.Entity;
22+
import com.google.cloud.datastore.Key;
23+
import com.rule.SystemsOutRule;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
@RunWith(JUnit4.class)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class MultiIneqQuerySampleIT {
34+
35+
private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
36+
37+
private Key employeeKey1;
38+
private Key employeeKey2;
39+
private Key employeeKey3;
40+
41+
@Rule
42+
public final SystemsOutRule systemsOutRule = new SystemsOutRule();
43+
44+
@Before
45+
public void setUp() {
46+
employeeKey1 = datastore.newKeyFactory().setKind("employees").newKey("employee1");
47+
Entity employee1 = Entity.newBuilder(employeeKey1)
48+
.set("name", "Alice")
49+
.set("salary", 100001)
50+
.set("experience", 10)
51+
.build();
52+
53+
employeeKey2 = datastore.newKeyFactory().setKind("employees").newKey("employee2");
54+
Entity employee2 = Entity.newBuilder(employeeKey2)
55+
.set("name", "Bob")
56+
.set("salary", 90000)
57+
.set("experience", 5)
58+
.build();
59+
60+
employeeKey3 = datastore.newKeyFactory().setKind("employees").newKey("employee3");
61+
Entity employee3 = Entity.newBuilder(employeeKey3)
62+
.set("name", "Jay")
63+
.set("salary", 120000)
64+
.set("experience", 15)
65+
.build();
66+
67+
datastore.put(employee1);
68+
datastore.put(employee2);
69+
datastore.put(employee3);
70+
}
71+
72+
@After
73+
public void tearDown() {
74+
datastore.delete(employeeKey1);
75+
datastore.delete(employeeKey2);
76+
datastore.delete(employeeKey3);
77+
}
78+
79+
@Test
80+
public void testIndexingConsiderationQuery() throws Exception {
81+
// Act
82+
IndexingConsiderationQuery.invoke();
83+
84+
// Assert
85+
systemsOutRule.assertContains("Entity");
86+
}
87+
88+
@Test
89+
public void testOrderFieldsQuery() throws Exception {
90+
// Act
91+
OrderFieldsQuery.invoke();
92+
93+
// Assert
94+
systemsOutRule.assertContains("Entity");
95+
}
96+
}

‎samples/snippets/src/test/resources/index.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ indexes:
2323
properties:
2424
- name: tag
2525
- name: tag
26+
- kind: employees
27+
properties:
28+
- name: salary
29+
- name: experience

0 commit comments

Comments
 (0)
Please sign in to comment.