Przykład

Opis

Zwraca niedeterministyczną próbkę z wyników poprzedniego etapu.

Obsługiwane są 2 tryby:

  • Tryb DOCUMENTS umożliwia próbkowanie określonej liczby dokumentów.
    • Ten tryb jest podobny do trybu GoogleSQL.RESERVOIR, ponieważ generuje próbkę o rozmiarze n, w której każda próbka o rozmiarze n jest równie prawdopodobna.
  • PERCENT umożliwia próbkowanie określonego odsetka dokumentów.
    • Ten tryb jest podobny do trybu GoogleSQL.BERNOULLI, ponieważ każdy dokument jest wybierany niezależnie z równym prawdopodobieństwem percent. W rezultacie zwracanych jest średnio #documents * percent / 100 dokumentów.

Składnia

Node.js

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

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

Zachowanie

Tryb Dokumenty

W trybie dokumentów pobierana jest określona liczba dokumentów w losowej kolejności. Podana liczba musi być nieujemną wartością INT64.

Na przykład w przypadku tej kolekcji:

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'});

Etap próbkowania w trybie dokumentu może służyć do pobierania niedeterministycznego podzbioru wyników z tej kolekcji.

Node.js

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

W tym przykładzie zwracany jest tylko 1 losowy dokument.

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

Jeśli podana liczba jest większa niż łączna liczba zwróconych dokumentów, wszystkie dokumenty są zwracane w losowej kolejności.

Node.js

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

W wyniku tego otrzymasz te dokumenty:

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

Przykłady klientów

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();

Tryb procentowy

W trybie procentowym każdy dokument ma określone percent szanse na zwrócenie. W przeciwieństwie do trybu dokumentów kolejność nie jest tu losowa, ale zachowuje dotychczasową kolejność dokumentów. Wartość procentowa musi być liczbą zmiennoprzecinkową z zakresu od 0.0 do 1.0.

Ponieważ każdy dokument jest wybierany niezależnie, dane wyjściowe są niedeterministyczne, a średnio zwracanych jest #documents * percent / 100 dokumentów.

Na przykład w przypadku tej kolekcji:

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'});

Etap próbkowania w trybie procentowym może służyć do pobierania (średnio) 50% dokumentów z etapu zbierania.

Node.js

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

Spowoduje to utworzenie niedeterministycznej próbki (średnio) 50% dokumentów z kolekcji cities. Oto jeden z możliwych wyników.

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

W trybie procentowym każdy dokument ma takie samo prawdopodobieństwo wybrania, więc może się zdarzyć, że nie zostanie zwrócony żaden dokument lub zostaną zwrócone wszystkie dokumenty.

Przykłady klientów

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();