Örnek

Açıklama

Önceki aşamanın sonuçlarından rastgele bir örnek döndürür.

Desteklenen iki mod vardır:

  • DOCUMENTS modu, belirli sayıda dokümanın örneklenmesine olanak tanır.
    • Bu mod, GoogleSQL.RESERVOIR moduna benzer. Bu modda, boyutu n olan bir örnek çıktı olarak verilir. Boyutu n olan herhangi bir örnek eşit olasılığa sahiptir.
  • PERCENT modu, belgelerin belirli bir yüzdesinin örneklenmesine olanak tanır.
    • Bu mod, her dokümanın eşit percent olasılıkla bağımsız olarak seçilmesi bakımından GoogleSQL.BERNOULLI moduna benzer. Bu durumda, ortalama #documents * percent / 100 doküman döndürülür.

Söz dizimi

Node.js

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

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

Davranış

Belgeler Modu

Belgeler modu, belirtilen sayıda belgeyi rastgele sırayla alır. Belirtilen sayı, negatif olmayan bir INT64 değeri olmalıdır.

Örneğin, aşağıdaki koleksiyon için:

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

Belge modundaki örnekleme aşaması, bu koleksiyondaki sonuçların deterministik olmayan bir alt kümesini almak için kullanılabilir.

Node.js

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

Bu örnekte, yalnızca rastgele 1 belge döndürülür.

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

Sağlanan sayı, döndürülen toplam belge sayısından büyükse tüm belgeler rastgele bir sırayla döndürülür.

Node.js

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

Bu işlem sonucunda aşağıdaki belgeler oluşturulur:

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

Müşteri örnekleri

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

Yüzde Modu

Yüzde modunda, her belgenin döndürülme percent şansı vardır. Dokümanlar modundan farklı olarak, buradaki sıralama rastgele değildir ve mevcut doküman sırasını korur. Bu yüzde girişi, 0.0 ile 1.0 arasında bir çift değer olmalıdır.

Her belge bağımsız olarak seçildiğinden çıkış deterministik değildir ve ortalama olarak #documents * percent / 100 belge döndürülür.

Örneğin, aşağıdaki koleksiyon için:

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

Yüzde modundaki örnekleme aşaması, koleksiyon aşamasındaki dokümanların (ortalama olarak) %50'sini almak için kullanılabilir.

Node.js

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

Bu işlem, cities koleksiyonundaki dokümanların% 50'sinin (ortalama) belirlenimci olmayan bir örneğini oluşturur. Aşağıda olası bir çıkış verilmiştir.

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

Yüzde modunda, her belgenin seçilme olasılığı aynı olduğundan hiçbir belgenin veya tüm belgelerin döndürülmemesi mümkündür.

Müşteri örnekleri

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