Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions snippets/firestore/firestore_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# from google.cloud.firestore import Query
# from google.cloud.firestore_v1.pipeline import Pipeline
# from google.cloud.firestore_v1.pipeline_source import PipelineSource
# from google.cloud.firestore_v1.pipeline_expressions import (
# AggregateFunction,
# Constant,
# Expression,
# Field,
# Count,
# )
# from google.cloud.firestore_v1.pipeline_expressions import (
# And,
# Conditional,
# Or,
# Not,
# Xor,
# )
# from google.cloud.firestore_v1.pipeline_stages import (
# Aggregate,
# FindNearestOptions,
# SampleOptions,
# UnnestOptions,
# )
# from google.cloud.firestore_v1.base_vector_query import DistanceMeasure
# from google.cloud.firestore_v1.vector import Vector
# from google.cloud.firestore_v1.client import Client

import firebase_admin
from firebase_admin import firestore

Expand All @@ -47,6 +20,19 @@

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have an async version of everything.

Often, we re-write the samples for both sync and async, and show both options in the language selection pickers. I'm not sure how you want to handle that here.

Maybe a couple samples for pipeline_initialization_async and basic_read_async is enough to show it exists, since the API is almost identical. Or maybe samples for async aren't needed for preview

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to get to the async versions after Java and if I don't get to it by launch day it'll just be a follow-on.


# pylint: disable=invalid-name

def query_explain():
# [START query_explain]
from google.cloud.firestore import Query, FieldFilter, ExplainOptions

results = client.collection("cities") \
.where(filter=FieldFilter("capital", "==", True)) \
.execute(explain_options=ExplainOptions(analyze=False))
metrics = results.explain_metrics()
summary = metrics.plan_summary()
# [END query_explain]


def pipeline_concepts():
# [START pipeline_concepts]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we'd want all region tags to start with the product prefix, so firestore_pipeline_concepts. But are other samples already written with these tags?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I omitted firestore_ where the filepath contained firestore somewhere. The snippets tool and the includecode tool both reference the filepath when importing statements so it'll still be findable as a Firestore snippet.

from google.cloud.firestore_v1.pipeline_expressions import Field
Expand Down Expand Up @@ -1764,7 +1750,7 @@ def vector_length_function():
def stages_expressions_example():
# [START stages_expressions_example]
from google.cloud.firestore_v1.pipeline_expressions import Field, Constant
from firebase_admin import firestore
from google.cloud import firestore

trailing_30_days = (
Constant.of(firestore.SERVER_TIMESTAMP)
Expand Down Expand Up @@ -1995,9 +1981,10 @@ def unnest_empty_array_example():
def unnest_preserve_empty_array_example():
# [START unnest_preserve_empty_array]
from google.cloud.firestore_v1.pipeline_expressions import (
Field,
Array,
Conditional,
Expression,
Field,
)
from google.cloud.firestore_v1.pipeline_stages import UnnestOptions

Expand All @@ -2006,8 +1993,8 @@ def unnest_preserve_empty_array_example():
.collection("users")
.unnest(
Conditional(
Field.of("scores").equal(Expression.array([])),
Expression.array([Field.of("scores")]),
Field.of("scores").equal(Array([])),
Array([Field.of("scores")]),
Field.of("scores"),
).as_("userScore"),
options=UnnestOptions(index_field="attempt"),
Expand Down
Loading