Beispiel

Beschreibung

Gibt eine nicht deterministische Stichprobe aus den Ergebnissen der vorherigen Phase zurück.

Es gibt zwei unterstützte Modi:

  • Im DOCUMENTS-Modus kann eine bestimmte Anzahl von Dokumenten als Stichprobe ausgewählt werden.
    • Dieser Modus ähnelt GoogleSQL.RESERVOIR, da er eine Stichprobe der Größe n ausgibt, wobei jede Stichprobe der Größe n gleich wahrscheinlich ist.
  • Im PERCENT-Modus kann ein Prozentsatz von Dokumenten als Stichprobe verwendet werden.
    • Dieser Modus ähnelt GoogleSQL.BERNOULLI, da jedes Dokument unabhängig mit einer gleichen Wahrscheinlichkeit von percent ausgewählt wird. Im Durchschnitt werden #documents * percent / 100 Dokumente zurückgegeben.

Syntax

Node.js

  const sampled = await db.pipeline()
    .database()
    .sample(50)
    .execute();

  const sampled = await db.pipeline()
    .database()
    .sample({ percent: 0.5 })
    .execute();

Verhalten

Dokumentmodus

Im Dokumentmodus wird eine bestimmte Anzahl von Dokumenten in zufälliger Reihenfolge abgerufen. Die angegebene Zahl muss ein nicht negativer INT64-Wert sein.

Beispiel:

Node.js

await db.collection('cities').doc('SF').set({name: 'San Francsico', state: 'California'});
await db.collection('cities').doc('NYC').set({name: 'New York City', state: 'New York'});
await db.collection('cities').doc('CHI').set({name: 'Chicago', state: 'Illinois'});

Mit der Beispielphase im Dokumentmodus kann eine nicht deterministische Teilmenge von Ergebnissen aus dieser Sammlung abgerufen werden.

Node.js

const sampled = await db.pipeline()
    .collection("/cities")
    .sample(1)
    .execute();

In diesem Beispiel wird nur ein zufälliges Dokument zurückgegeben.

  {name: 'New York City', state: 'New York'}

Wenn die angegebene Zahl größer ist als die Gesamtzahl der zurückgegebenen Dokumente, werden alle Dokumente in zufälliger Reihenfolge zurückgegeben.

Node.js

const sampled = await db.pipeline()
    .collection("/cities")
    .sample(5)
    .execute();

Daraus ergeben sich die folgenden Dokumente:

  {name: 'New York City', state: 'New York'}
  {name: 'Chicago', state: 'Illinois'}
  {name: 'San Francisco', state: 'California'}

Kundenbeispiele

Web

let results;

// Get a sample of 100 documents in a database
results = await execute(db.pipeline()
  .database()
  .sample(100)
);

// Randomly shuffle a list of 3 documents
results = await execute(db.pipeline()
  .documents([
    doc(db, "cities", "SF"),
    doc(db, "cities", "NY"),
    doc(db, "cities", "DC"),
  ])
  .sample(3)
);
Swift
var results: Pipeline.Snapshot

// Get a sample of 100 documents in a database
results = try await db.pipeline()
  .database()
  .sample(count: 100)
  .execute()

// Randomly shuffle a list of 3 documents
results = try await db.pipeline()
  .documents([
    db.collection("cities").document("SF"),
    db.collection("cities").document("NY"),
    db.collection("cities").document("DC"),
  ])
  .sample(count: 3)
  .execute()

Kotlin

var results: Task<Pipeline.Snapshot>

// Get a sample of 100 documents in a database
results = db.pipeline()
    .database()
    .sample(100)
    .execute()

// Randomly shuffle a list of 3 documents
results = db.pipeline()
    .documents(
        db.collection("cities").document("SF"),
        db.collection("cities").document("NY"),
        db.collection("cities").document("DC")
    )
    .sample(3)
    .execute()

Java

Task<Pipeline.Snapshot> results;

// Get a sample of 100 documents in a database
results = db.pipeline()
    .database()
    .sample(100)
    .execute();

// Randomly shuffle a list of 3 documents
results = db.pipeline()
    .documents(
        db.collection("cities").document("SF"),
        db.collection("cities").document("NY"),
        db.collection("cities").document("DC")
    )
    .sample(3)
    .execute();
Python
# Get a sample of 100 documents in a database
results = client.pipeline().database().sample(100).execute()

# Randomly shuffle a list of 3 documents
results = (
    client.pipeline()
    .documents(
        client.collection("cities").document("SF"),
        client.collection("cities").document("NY"),
        client.collection("cities").document("DC"),
    )
    .sample(3)
    .execute()
)
Java
// Get a sample of 100 documents in a database
Pipeline.Snapshot results1 = firestore.pipeline().database().sample(100).execute().get();

// Randomly shuffle a list of 3 documents
Pipeline.Snapshot results2 =
    firestore
        .pipeline()
        .documents(
            firestore.collection("cities").document("SF"),
            firestore.collection("cities").document("NY"),
            firestore.collection("cities").document("DC"))
        .sample(3)
        .execute()
        .get();

Prozentmodus

Im Prozentmodus hat jedes Dokument eine bestimmte percent-Wahrscheinlichkeit, zurückgegeben zu werden. Im Gegensatz zum Dokumentmodus ist die Reihenfolge hier nicht zufällig, sondern entspricht der vorhandenen Dokumentreihenfolge. Diese Prozentangabe muss ein Double-Wert zwischen 0.0 und 1.0 sein.

Da jedes Dokument unabhängig ausgewählt wird, ist die Ausgabe nicht deterministisch. Im Durchschnitt werden #documents * percent / 100 Dokumente zurückgegeben.

Beispiel:

Node.js

await db.collection('cities').doc('SF').set({name: 'San Francsico', state: 'California'});
await db.collection('cities').doc('NYC').set({name: 'New York City', state: 'New York'});
await db.collection('cities').doc('CHI').set({name: 'Chicago', state: 'Illinois'});
await db.collection('cities').doc('ATL').set({name: 'Atlanta', state: 'Georgia'});

Mit der Stichprobenphase im Prozentmodus können Sie (im Durchschnitt) 50% der Dokumente aus der Erfassungsphase abrufen.

Node.js

  const sampled = await db.pipeline()
    .collection("/cities")
    .sample({ percent: 0.5 })
    .execute();

Dies führt zu einer nicht deterministischen Stichprobe von (durchschnittlich) 50% der Dokumente aus der Sammlung cities. Das ist eine mögliche Ausgabe.

  {name: 'New York City', state: 'New York'}
  {name: 'Chicago', state: 'Illinois'}

Im Prozentmodus hat jedes Dokument die gleiche Wahrscheinlichkeit, ausgewählt zu werden. Daher kann es vorkommen, dass keine oder alle Dokumente zurückgegeben werden.

Kundenbeispiele

Web

// Get a sample of on average 50% of the documents in the database
const results = await execute(db.pipeline()
  .database()
  .sample({ percentage: 0.5 })
);
Swift
// Get a sample of on average 50% of the documents in the database
let results = try await db.pipeline()
  .database()
  .sample(percentage: 0.5)
  .execute()

Kotlin

// Get a sample of on average 50% of the documents in the database
val results = db.pipeline()
    .database()
    .sample(SampleStage.withPercentage(0.5))
    .execute()

Java

// Get a sample of on average 50% of the documents in the database
Task<Pipeline.Snapshot> results = db.pipeline()
    .database()
    .sample(SampleStage.withPercentage(0.5))
    .execute();
Python
from google.cloud.firestore_v1.pipeline_stages import SampleOptions

# Get a sample of on average 50% of the documents in the database
results = (
    client.pipeline().database().sample(SampleOptions.percentage(0.5)).execute()
)
Java
// Get a sample of on average 50% of the documents in the database
Pipeline.Snapshot results =
    firestore.pipeline().database().sample(Sample.withPercentage(0.5)).execute().get();