-
Notifications
You must be signed in to change notification settings - Fork 75
feat: Add additional Firestore query snippets #1790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4d6aa8b
firestore sample for multiple inequality
kevkim-codes c9a7e69
fix typo
kevkim-codes 3ca4567
format code
kevkim-codes 1dbe7db
change samples to be in own file
kevkim-codes eba4543
Merge branch 'googleapis:main' into firestore-samples
kevkim-codes 2cc7ba8
Create maven.yml
kevkim-codes a2afcbb
Delete .github/workflows/maven.yml
kevkim-codes aaf53a5
change query to lowercase to pass test case
kevkim-codes 59175d0
fix checkstyle lint error
kevkim-codes a1cf16d
update copyright dates
kevkim-codes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
.../src/main/java/com/example/firestore/snippets/MultipleRangeInequalityFiltersSnippets.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /* | ||
| * Copyright 2024 Google Inc. | ||
| * | ||
| * 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.firestore.snippets; | ||
|
|
||
| import com.example.firestore.snippets.model.City; | ||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.core.ApiFutures; | ||
| import com.google.cloud.firestore.CollectionReference; | ||
| import com.google.cloud.firestore.DocumentSnapshot; | ||
| import com.google.cloud.firestore.Firestore; | ||
| import com.google.cloud.firestore.Query; | ||
| import com.google.cloud.firestore.Query.Direction; | ||
| import com.google.cloud.firestore.QueryDocumentSnapshot; | ||
| import com.google.cloud.firestore.QuerySnapshot; | ||
| import com.google.cloud.firestore.WriteResult; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| /** | ||
| * Snippets to support firestore querying data documentation. | ||
| */ | ||
| class MultipleRangeInequalityFiltersSnippets { | ||
|
|
||
| private final Firestore db; | ||
|
|
||
| MultipleRangeInequalityFiltersSnippets(Firestore db) { | ||
| this.db = db; | ||
| } | ||
|
|
||
| /** | ||
| * Creates cities collection and add sample documents to test queries. | ||
| */ | ||
| void prepareExamples() throws Exception { | ||
| // Data for each city | ||
| Map<String, Object> sfData = new HashMap<>(); | ||
| sfData.put("name", "San Francisco"); | ||
| sfData.put("state", "CA"); | ||
| sfData.put("country", "USA"); | ||
| sfData.put("capital", false); | ||
| sfData.put("population", 860000L); | ||
| sfData.put("density", 18000L); | ||
| sfData.put("regions", Arrays.asList("west_coast", "norcal")); | ||
|
|
||
| Map<String, Object> laData = new HashMap<>(); | ||
| laData.put("name", "Los Angeles"); | ||
| laData.put("state", "CA"); | ||
| laData.put("country", "USA"); | ||
| laData.put("capital", false); | ||
| laData.put("population", 3900000L); | ||
| laData.put("density", 8300L); | ||
| laData.put("regions", Arrays.asList("west_coast", "socal")); | ||
|
|
||
| Map<String, Object> dcData = new HashMap<>(); | ||
| dcData.put("name", "Washington D.C."); | ||
| dcData.put("state", null); | ||
| dcData.put("country", "USA"); | ||
| dcData.put("capital", true); | ||
| dcData.put("population", 680000L); | ||
| dcData.put("density", 11300L); | ||
| dcData.put("regions", Arrays.asList("east_coast")); | ||
|
|
||
| Map<String, Object> tokData = new HashMap<>(); | ||
| tokData.put("name", "Tokyo"); | ||
| tokData.put("state", null); | ||
| tokData.put("country", "Japan"); | ||
| tokData.put("capital", true); | ||
| tokData.put("population", 9000000L); | ||
| tokData.put("density", 16000L); | ||
| tokData.put("regions", Arrays.asList("kanto", "honshu")); | ||
|
|
||
| Map<String, Object> bjData = new HashMap<>(); | ||
| bjData.put("name", "Beijing"); | ||
| bjData.put("state", null); | ||
| bjData.put("country", "China"); | ||
| bjData.put("capital", true); | ||
| bjData.put("population", 21500000L); | ||
| bjData.put("density", 3500L); | ||
| bjData.put("regions", Arrays.asList("jingjinji", "hebei")); | ||
|
|
||
| CollectionReference citiesCollection = db.collection("cities"); | ||
|
|
||
| // Add each city document | ||
| addCityDocument(citiesCollection, "SF", sfData); | ||
| addCityDocument(citiesCollection, "LA", laData); | ||
| addCityDocument(citiesCollection, "DC", dcData); | ||
| addCityDocument(citiesCollection, "TOK", tokData); | ||
| addCityDocument(citiesCollection, "BJ", bjData); | ||
|
|
||
| System.out.println("Data added successfully!"); | ||
| } | ||
|
|
||
| private static void addCityDocument(CollectionReference citiesRef, String documentId, | ||
| Map<String, Object> data) { | ||
| ApiFuture<WriteResult> future = citiesRef.document(documentId).set(data); | ||
| try { | ||
| WriteResult result = future.get(); | ||
| System.out.println("Update time : " + result.getUpdateTime()); | ||
| } catch (Exception e) { | ||
| System.err.println("Error adding document: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /* Example of Query with range and inequality filters. */ | ||
| Query compoundMultiInequalities() { | ||
| // [START firestore_query_filter_compound_multi_ineq] | ||
| Query query = db.collection("cities") | ||
| .whereGreaterThan("population", 1000000) | ||
| .whereLessThan("density", 10000); | ||
| // [END firestore_query_filter_compound_multi_ineq] | ||
| return query; | ||
| } | ||
|
|
||
| /* Example of filtering indexes considering performance reasons. */ | ||
| void indexingConsiderations() { | ||
| // [START firestore_query_indexing_considerations] | ||
| db.collection("employees") | ||
| .whereGreaterThan("salary", 100000) | ||
| .whereGreaterThan("experience", 0) | ||
| .orderBy("salary") | ||
| .orderBy("experience"); | ||
| // [END firestore_query_indexing_considerations] | ||
| } | ||
|
|
||
| /** | ||
| * Closes the gRPC channels associated with this instance and frees up their resources. | ||
| */ | ||
| void close() throws Exception { | ||
| db.close(); | ||
| } | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
...rc/test/java/com/example/firestore/snippets/MultipleRangeInequalityFiltersSnippetsIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Copyright 2024 Google Inc. | ||
| * | ||
| * 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.firestore.snippets; | ||
|
|
||
| import static com.google.common.collect.Sets.newHashSet; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import com.example.firestore.BaseIntegrationTest; | ||
| import com.google.api.core.ApiFuture; | ||
| import com.google.cloud.firestore.DocumentSnapshot; | ||
| import com.google.cloud.firestore.Query; | ||
| import com.google.cloud.firestore.QueryDocumentSnapshot; | ||
| import com.google.cloud.firestore.QuerySnapshot; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| @SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
| public class MultipleRangeInequalityFiltersSnippetsIT extends BaseIntegrationTest { | ||
|
|
||
| private static MultipleRangeInequalityFiltersSnippets multipleRangeInequalityFiltersSnippets; | ||
|
|
||
| @BeforeClass | ||
| public static void setUpBeforeClass() throws Exception { | ||
| multipleRangeInequalityFiltersSnippets = new MultipleRangeInequalityFiltersSnippets(db); | ||
| multipleRangeInequalityFiltersSnippets.prepareExamples(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRangeMultipleInequalityFilter() throws Exception { | ||
| Query query = multipleRangeInequalityFiltersSnippets.compoundMultiInequalities(); | ||
|
|
||
| Set<String> expected = newHashSet("BJ", "LA"); | ||
| Set<String> actual = getResultsAsSet(query); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| private Set<String> getResultsAsSet(Query query) throws Exception { | ||
| List<String> docIds = getResults(query); | ||
| return new HashSet<>(docIds); | ||
| } | ||
|
|
||
| private List<String> getResults(Query query) throws Exception { | ||
| // asynchronously retrieve query results | ||
| ApiFuture<QuerySnapshot> future = query.get(); | ||
| // block on response | ||
| QuerySnapshot querySnapshot = future.get(); | ||
|
|
||
| List<String> docIds = new ArrayList<>(); | ||
| for (DocumentSnapshot document : querySnapshot.getDocuments()) { | ||
| docIds.add(document.getId()); | ||
| } | ||
| return docIds; | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDown() throws Exception { | ||
| deleteAllDocuments(db); | ||
| multipleRangeInequalityFiltersSnippets.close(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.